MvxObservableCollection 添加范围
MvxObservableCollection AddRange
我有视图模型(使用 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) 工作正常。稍后,我为此视图模型添加视图:
<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();
}
}
现在,当我单击按钮时,出现错误 "Range actions are not supported." 当我从 xaml 中删除 ListView 时,一切正常。
我可以将 ListView 更改为 DataGrid 或其他列表控件 - 错误将是相同的!
我想知道,如何将我的视图绑定到 MvxObservableCollection?
如果您自己将项目一项一项添加到源集合中会怎么样?:
private async void GetItemsAsync()
{
var items = await itemsService.GetItemsAsync();
ItemsCollection.Clear();
foreach (var item in items)
ItemsCollection.Add(item);
}
...或将源 属性 重新设置为新集合:
private async void GetItemsAsync()
{
var items = await itemsService.GetItemsAsync();
ItemsCollection = new MvxObservableCollection<Item>(items);
}
显然 "range actions are not supported" 用于数据绑定 MvxObservableCollection
。
我有视图模型(使用 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) 工作正常。稍后,我为此视图模型添加视图:
<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();
}
}
现在,当我单击按钮时,出现错误 "Range actions are not supported." 当我从 xaml 中删除 ListView 时,一切正常。 我可以将 ListView 更改为 DataGrid 或其他列表控件 - 错误将是相同的!
我想知道,如何将我的视图绑定到 MvxObservableCollection?
如果您自己将项目一项一项添加到源集合中会怎么样?:
private async void GetItemsAsync()
{
var items = await itemsService.GetItemsAsync();
ItemsCollection.Clear();
foreach (var item in items)
ItemsCollection.Add(item);
}
...或将源 属性 重新设置为新集合:
private async void GetItemsAsync()
{
var items = await itemsService.GetItemsAsync();
ItemsCollection = new MvxObservableCollection<Item>(items);
}
显然 "range actions are not supported" 用于数据绑定 MvxObservableCollection
。