You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Xamarin.Forms ListView在UWP显示正常但Android不显示的问题求助

Hey there! Let's figure out why your ListView isn't showing up on Android while working perfectly on UWP. I've spotted a couple of key issues in your code that might be causing this:

1. ObservableCollection Reassignment Isn't Triggering View Updates

Right now, you're replacing the entire Assignments collection with a brand new ObservableCollection instance in LoadAssignmentsAsync. Even with PropertyChanged.Fody, if the View doesn't get a PropertyChanged notification for the Assignments property, it won't know to refresh the binding.

Instead of reassigning the collection, modify the existing one—this uses the ObservableCollection's built-in CollectionChanged event to automatically notify the View:

private async Task LoadAssignmentsAsync()
{
    var fetchedAssignments = await assignmentDataService.GetAssignments();
    
    // Clear existing items first
    Assignments.Clear();
    
    // Add all new items to the existing collection
    foreach (var assignment in fetchedAssignments)
    {
        Assignments.Add(assignment);
    }
}

2. Async Method Call in Constructor is "Fire-and-Forget"

Calling an async Task method directly from the constructor means the code runs without waiting for completion. This can lead to the View binding to an empty collection before your data finishes loading, and if the reassignment doesn't trigger a notification, the View never updates.

Fix this by moving the data load to the page's OnAppearing event, where you can properly await the task:

ViewModel Update: Make LoadAssignmentsAsync public and remove the call from the constructor:

public class AssignmentsViewModel : INotifyPropertyChanged 
{ 
    private readonly IAssignmentDataService assignmentDataService; 
    public ObservableCollection<Assignment> Assignments { get; set; } = new ObservableCollection<Assignment>(); 

    public AssignmentsViewModel() 
    { 
        assignmentDataService = App.IoCContainer.Get<IAssignmentDataService>();
        // Remove LoadAssignmentsAsync() call from here
    } 

    public async Task LoadAssignmentsAsync() 
    { 
        var fetchedAssignments = await assignmentDataService.GetAssignments();
        Assignments.Clear();
        foreach (var assignment in fetchedAssignments)
        {
            Assignments.Add(assignment);
        }
    } 

    public event PropertyChangedEventHandler PropertyChanged; 
}

MainPage Code-Behind Update:

public partial class MainPage : ContentPage 
{ 
    private AssignmentsViewModel _viewModel;

    public MainPage() 
    { 
        InitializeComponent(); 
        _viewModel = new AssignmentsViewModel();
        BindingContext = _viewModel; 
    } 

    protected override async void OnAppearing()
    {
        base.OnAppearing();
        await _viewModel.LoadAssignmentsAsync();
    }
}

3. Android Layout Constraints Might Be Hiding the ListView

On Android, scrollable controls like ListView sometimes need explicit layout options to fill available space. Update your XAML to ensure the ListView expands properly:

<Grid>
    <ListView ItemsSource="{Binding Assignments}"
              VerticalOptions="FillAndExpand"
              HorizontalOptions="FillAndExpand">
        <ListView.ItemTemplate>
            <DataTemplate>
                <TextCell Text="{Binding Content}" TextColor="Black"/>
            </DataTemplate>
        </ListView.ItemTemplate>
    </ListView>
</Grid>

4. Double-Check PropertyChanged.Fody Setup

Make sure you have both the Fody and PropertyChanged.Fody packages installed, and that your FodyWeavers.xml file includes the <PropertyChanged/> node. If Fody isn't weaving correctly, the Assignments property won't trigger PropertyChanged notifications when reassigned. The first fix (modifying the existing collection) avoids relying on this, but it's still good to verify your setup.

Start with the first two fixes—they're the most likely culprits. Let me know if you still run into issues after trying these!

内容的提问来源于stack exchange,提问作者Tong

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.29 06:46:53