首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >MvxObservableCollection AddRange

MvxObservableCollection AddRange
EN

Stack Overflow用户
提问于 2018-06-18 08:56:10
回答 1查看 763关注 0票数 0

我有视图模型(与Fody INPC一起使用):

代码语言:javascript
复制
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(项目)工作正常。稍后,我为这个视图模型添加视图:

代码语言:javascript
复制
<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>

这是暗号:

代码语言:javascript
复制
[MvxViewFor(typeof(ItemsViewModel))]
partial class ItemsView
{
    public DocumentTypeEditorView()
    {
        InitializeComponent();
    }
}

现在,当我点击按钮,我得到错误“范围操作是不支持的。”当我从xaml中删除ListView时,所有操作都很好。我可以将ListView更改为DataGrid或其他列表控件--错误将相同!

我想知道,我如何将我的观点绑定到MvxObservableCollection?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-06-18 09:03:13

如果您自己一个一个地将这些项添加到源集合中,该怎么办?:

代码语言:javascript
复制
private async void GetItemsAsync()
{
    var items = await itemsService.GetItemsAsync();
    ItemsCollection.Clear();
    foreach (var item in items)
        ItemsCollection.Add(item);
}

...or将源属性重新设置为新集合:

代码语言:javascript
复制
private async void GetItemsAsync()
{
    var items = await itemsService.GetItemsAsync();
    ItemsCollection = new MvxObservableCollection<Item>(items);
}

显然,数据绑定MvxObservableCollection不支持“范围操作”。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50905848

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档