Xaml Xamarin表单-对ObservableCollection的更改不会更新视图

Xaml Xamarin表单-对ObservableCollection的更改不会更新视图,xaml,xamarin,xamarin.forms,Xaml,Xamarin,Xamarin.forms,我有一个ViewModel,当执行命令时,它从服务中获取项目,并将这些项目添加到ObservableCollection。 ObservableCollection绑定到ListView 我的问题是,向ObservableCollection添加项不会自动更新视图-ListView保持为空。如果我调用了OnPropertyChanged(nameof(ItemList)),那么ListView会被更新,但是ObservableCollection不应该处理这个问题吗 我使用的是最新的Xamari

我有一个ViewModel,当执行命令时,它从服务中获取项目,并将这些项目添加到ObservableCollection。 ObservableCollection绑定到ListView

我的问题是,向ObservableCollection添加项不会自动更新视图-ListView保持为空。如果我调用了OnPropertyChanged(nameof(ItemList)),那么ListView会被更新,但是ObservableCollection不应该处理这个问题吗

我使用的是最新的Xamarin.Forms(2.3.4.247)。这个问题只会影响Android——如果我在iOS上运行,它就会正常工作

实现INotifyPropertyChanged的视图模型:

public class ListViewModel : ViewModelBase
{
    private readonly IService m_Service;

    public ListViewModel(IService service)
    {
        m_Service = service;
        ItemList = new ObservableCollection<Item>();
        RefreshCommand = new Command(async () => await RefreshData());
    }

    public ICommand RefreshCommand {get;set;}
    public ObservableCollection<Item> ItemList { get; private set; }

    private async Task RefreshData()
    {
        ItemList.Clear();
        var newItems = await m_Service.GetItems();

        if(newItems != null) {
            foreach(var n in newItems)
            {
                ItemList.Add(new ItemViewModel() { Title = n.Title });
            }               
    }
}
public class ItemViewModel : ViewModelBase
{
    private string m_JobTitle;

    public ItemViewModel()
    {
    } 

    public string Title
    {
        get { return m_Title; }
        set
        {
            m_Title = value;
            OnPropertyChanged(nameof(Title));
        }
    }        
}
“列表”视图:

<ContentView xmlns="http://xamarin.com/schemas/2014/forms" 
         xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
         xmlns:ui="clr-namespace:Test.Mobile.UI;assembly=Test.Mobile.UI"
         x:Class="Test.Mobile.UI.MyListView">
  <ContentView.Content>
        <StackLayout Orientation="Vertical">      
            <ListView ItemsSource="{Binding ItemList}">
                <ListView.ItemTemplate>
              <DataTemplate>
                  <ViewCell>
                      <ui:ItemView></ui:ItemView>
                  </ViewCell>
                </DataTemplate>
          </ListView.ItemTemplate>
        </ListView>
  </StackLayout>
  </ContentView.Content>
</ContentView>

“项目”视图:

<?xml version="1.0" encoding="UTF-8"?>
<ContentView xmlns="http://xamarin.com/schemas/2014/forms" 
         xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
         xmlns:enums="clr-namespace:Test.Mobile.Models.Enums;assembly=Test.Mobile.Models"
         x:Class="Test.Mobile.UI.JobItemView">
    <ContentView.Content>
       <Label Text={Binding Title}/>
    </ContentView.Content>
</ContentView>

您使用的是什么版本的Xamarin表单

  • 将项目更新至最新版本
  • 如果仍然存在问题,请在添加所有元素后尝试以下操作: ItemList=新的ObservableCollection(新项目)
  • 如果这不起作用,则可能是ViewModelBase中的一个问题。 对于Handle Property Changed,我使用这个漂亮的包

您在哪个平台上运行?有时有些问题只需要设置ItemList=newobservetecollection(newItems);在RefreshData方法中,ViewModelBase使用什么软件包?MvvmLight?我在Android上遇到了问题。我刚刚测试过,iOS运行正常。我还尝试了ItemList=newobservetecollection(newItems);,但除非调用PropertyChanged,否则视图仍然不会更新。我想知道这是否只是一个奇怪的Xamarin/Android问题?您正试图将ItemViewModel类型的对象添加到ObservableCollection ItemList中。这不是错误,但我想知道你想做什么。Add(new ItemViewModel()我使用的是最新版本(2.3.4.247)。我不认为这是我的ViewModelBase的问题,因为我的所有其他绑定属性都会正确更新,特别是引发PropertyChanged事件确实会更新视图。我认为可能存在一些奇怪的怪癖,或者我在某个我看不到的地方犯了一个愚蠢的错误。@charlin我分离了
observedcollection模块
propertyPageModel类上的ty。我将PageModel初始化为继承ViewModelBase(Prism)的ViewModel类上的Model。Module有一个属性
isLocked
。如果我更新
Model.Modules[0]。isLocked=true
视图不会更新。即使我调用
RaisePropertyChanged(nameof(Model.Modules))
,视图仍然没有更新。但是如果我将
可观察的收集模块
从PageModel移动到ViewModel,并调用
RaisePropertyChanged(模块名称))
,它会更新视图。我做错了什么?