【问题标题】:GridView is not updating on UI after changing the item source list更改项目源列表后,GridView 未在 UI 上更新
【发布时间】:2019-09-03 12:53:31
【问题描述】:

我正在使用带有项目源属性的 GridView,它是对象列表。最初,我分配出现在 UI 上的页面上的项目列表。但是当我对按钮单击执行一些操作并更新列表中的项目时,它不会在 UI 上更新,我需要返回并再次访问。

  <grial:GridView
        x:Name="ItemList"
        Padding="10"
        ColumnSpacing="10"
        RowSpacing="10"
        ColumnCount="{
            grial:OnOrientationInt 
                PortraitPhone=2,
                LandscapePhone=2,

                PortraitTablet=2,
                LandscapeTablet=2,

                PortraitDesktop=2,
                LandscapeDesktop=2
        }"
        VerticalOptions="Start"
        ItemsSource="{ Binding Items ,Mode=TwoWay}"
        Style="{ StaticResource ResponsiveLandscapeMarginStyle }">
            <grial:GridView.ItemTemplate>
                <DataTemplate>
                    <local:RegisterItemGridV2 />
                </DataTemplate>
            </grial:GridView.ItemTemplate>
        </grial:GridView>

我的视图模型

public class LoadDistinctItemsViewModel : INotifyPropertyChanged
{
        public event PropertyChangedEventHandler PropertyChanged;

        private IList<Items> items;

        public IList<Items> Items
        {
            get => items;
            set
            {
                items = value;

                NotifyPropertyChanged();
            }
        }

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


        public void LoadData(string _loadId, int _mode = 0)
        {
            Items = App.dataManager.GetLoadDistinctItems(_loadId, _mode);
        }
 }

我正在调用每次都在更新的 loadData,项目列表中的计数在增加,但在 UI 上没有更新。

【问题讨论】:

    标签: xamarin gridview data-binding inotifypropertychanged


    【解决方案1】:

    您可以使用 ObservableCollection 代替 IList 。对我来说效果很好。

    public ObservableCollection<string> Items { get; set; }
    

    【讨论】:

    • 我使用的是标签页,有关系吗?
    • 分享你的样品,我会在我这边进行测试。
    • 我应该在哪里分享
    • 您可以将其上传到 github。不要忘记删除您的个人信息,例如密码和帐户 ID。
    • 我不能分享整个项目,你能来聊天,我可以分享内容页面和查看模型
    【解决方案2】:

    由于您经常修改列表,因此您不应该使用列表。

    如果您使用 List 并在运行时添加或删除项目,您的 ListView /Grid 将不会反映这些更改。 如果您使用 ObservableCollection,则会反映更改

    (另外,删除{ Binding}中的空格

    【讨论】:

    • 我也试过 ObservableCollection 但没有结果
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-01-26
    • 1970-01-01
    • 2012-06-12
    • 2018-05-08
    • 2021-10-23
    相关资源
    最近更新 更多