我有viewmodel(使用Fody INPC):
public sealed class ItemsViewModel : MvxViewModel, IMvxNotifyPropertyChanged
{
private readonly IItemsService itemsService;
public MvxObservableCollection<Item> ItemsCollection { get; private set; }
public IMvxCommand GetItemsCommand { get; private set; }
private async void GetItemsAsync()
{
var items = await itemsService.GetItemsAsync();
ItemsCollection.Clear();
ItemsCollection.AddRange(items);
}
public ItemsViewModel(IItemsService itemsService)
{
this.itemsService = itemsService;
ItemsCollection = new MvxObservableCollection<Item>();
GetItemsCommand = new MvxCommand(() => GetItemsAsync());
}
}
AddRange(items)工作正常。稍后,我为此viewmodel添加了视图:
<views:MvxWpfView x:Class="MyApp.ItemsView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:views="clr-namespace:MvvmCross.Platforms.Wpf.Views;assembly=MvvmCross.Platforms.Wpf">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition/>
</Grid.RowDefinitions>
<Button Content="Get Items" Command="{Binding GetItemsCommand}"/>
<ListView Grid.Row="1" ItemsSource="{Binding ItemsCollection}"/>
</Grid>
它背后的代码:
[MvxViewFor(typeof(ItemsViewModel))]
partial class ItemsView
{
public DocumentTypeEditorView()
{
InitializeComponent();
}
}
现在,当我点击按钮时,我收到错误“不支持范围操作”。当我从xaml中删除ListView时,一切正常。 我可以将ListView更改为DataGrid或其他列表控件 - 错误将是相同的!
我想知道,如何将我的视图绑定到MvxObservableCollection?
答案 0 :(得分:0)
如果您自己逐个将项目添加到源集合中,该怎么办?:
private async void GetItemsAsync()
{
var items = await itemsService.GetItemsAsync();
ItemsCollection.Clear();
foreach (var item in items)
ItemsCollection.Add(item);
}
...或者将source属性重新设置为新集合:
private async void GetItemsAsync()
{
var items = await itemsService.GetItemsAsync();
ItemsCollection = new MvxObservableCollection<Item>(items);
}
显然&#34;不支持范围操作&#34;对于数据绑定MvxObservableCollection
。