我有视图模型(与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(项目)工作正常。稍后,我为这个视图模型添加视图:
<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?
发布于 2018-06-18 09:03:13
如果您自己一个一个地将这些项添加到源集合中,该怎么办?:
private async void GetItemsAsync()
{
var items = await itemsService.GetItemsAsync();
ItemsCollection.Clear();
foreach (var item in items)
ItemsCollection.Add(item);
}...or将源属性重新设置为新集合:
private async void GetItemsAsync()
{
var items = await itemsService.GetItemsAsync();
ItemsCollection = new MvxObservableCollection<Item>(items);
}显然,数据绑定MvxObservableCollection不支持“范围操作”。
https://stackoverflow.com/questions/50905848
复制相似问题