【问题标题】:Want to get data to be shown in a listview conditionally想要有条件地在列表视图中显示数据
【发布时间】:2017-04-17 00:00:56
【问题描述】:

在 Xamarin Forms 中,我有一个列表视图,可以显示项目表的所有项目。我只想显示“状态”已获批准的项目。我怎样才能做到这一点?我在哪里通过 'where status = 'approved'' 条件。

这是我的 XAML 代码:

    <ViewModels:ItemViewModel/>

</ContentPage.BindingContext>

<ListView ItemsSource="{Binding ItemsList}"

          HasUnevenRows="True" 
          x:Name="lstItems"
         ItemTapped="LstItems_OnItemTapped">





    <ListView.ItemTemplate>

        <DataTemplate>

            <ViewCell>

                <StackLayout Orientation="Horizontal">

                    <Label Text="{Binding ItemCode}" TextColor="Blue"/>
                <Label Text="{Binding ItemDesc}"  TextColor="Blue"/>


                </StackLayout>

            </ViewCell>

        </DataTemplate>


    </ListView.ItemTemplate>

</ListView>

我的视图模型如下:

命名空间 MyFirstDbApp.ViewModels { 公共类 ItemViewModel: INotifyPropertyChanged { 私有列表_itemsList;

    public List<Item> ItemsList



    {
        get { return _itemsList; }

        set
        {
            _itemsList = value;
            OnPropertyChanged();
        }
    }

    public ItemViewModel()
    {

        InitializeDataAsync();
    }


    private async Task InitializeDataAsync()
    {

        var ItemServices = new ItemServices();

        ItemsList = await ItemServices.GetItemsAsync();

    }


    public event PropertyChangedEventHandler PropertyChanged;

    protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
    {
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
    }
}

}

实现我的新代码时出错:

var 结果 = 等待 ItemServices.GetItemsAsync().ConfigureAwait(); ItemsList = result.Where(x => x.Status == "已批准"

Line:1 - 非静态字段、方法或属性 ItemServices.GetItemsAsync 需要对象引用

Line:1-“ConfigureAwait”方法没有重载需要 0 个参数

行:2-无法将 SourceType 'Systems.Collection.Generic.IEnumerable' 转换为目标类型 'System. 集合.GenericList '

【问题讨论】:

  • 只需根据您的条件过滤列表。你试过什么?向我们展示您的代码。
  • 我已在问题本身中添加了我的 xaml 代码。这会将我的 sql 中的所有项目返回到列表视图。我只想有条件地在列表视图中显示几个项目。我如何以及在哪里通过条件?

标签: xamarin


【解决方案1】:

应该在 ViewModel 中的 ItemsList 上进行过滤,而不是在 XAML 中。

_viewModel.ItemsList.Where(x => x.Status == "Approved");

祝你好运:-)

【讨论】:

  • 你好!我对 xamarin 开发非常陌生。我也在问题中添加了我的 ViewModel 代码。你能告诉我在我的视图模型中添加你的代码吗?非常感谢...
  • in InitializeDataAsync.. var result = await ItemServices.GetItemsAsync().ConfigureAwait(); ItemsList = result.Where(x => x.Status == "Approved");
  • 我的代码出现错误。在我的问题中提到。你能解释一下如何处理它。会有很大帮助。请。
  • var 结果 = 等待 ItemServices.GetItemsAsync().ConfigureAwait(false); ItemsList = result.Where(x => x.Status == "Approved").ToList();这是非常基本的 c#
  • 不!如果对您有帮助,请采纳答案!
猜你喜欢
  • 2015-12-22
  • 1970-01-01
  • 2015-06-15
  • 1970-01-01
  • 2016-02-12
  • 1970-01-01
  • 2019-01-23
  • 2013-12-23
  • 1970-01-01
相关资源
最近更新 更多