【问题标题】:Observable collection Add object doesn't refresh view可观察的集合添加对象不刷新视图
【发布时间】:2016-11-21 18:18:37
【问题描述】:

这是我的代码,当我按下与 Clicked 属性 addische 绑定的按钮时,新项目被添加到 ListView 但我在列表中看不到它。

public partial class GS : ContentPage
{
    private GSViewModel _viewModel;

    public GS()
    {
        InitializeComponent();
        BindingContext = _viewModel = new GSViewModel();
    }

    private void addische(object sender, EventArgs e)
    {
        _viewModel.newItemAdded();
    }

    public class GSViewModel : INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;

        public ObservableCollection<schItem> _scheListItem;
        public ObservableCollection<schItem> Items { get { return _scheListItem; }
            private set
            {
                if (_scheListItem != value)
                {
                    _scheListItem = value;
                    OnPropertyChanged();
                }
            }
        }

        public void newItemAdded()
        {
            Items.Add(new schItem
            {
                realm_id = 133,
                list_id = 33
            });
        }

        public GSViewModel()
        {
            Items = new ObservableCollection<schItem>();
            initListView();
        }

        public void initListView()
        {
           //get data
        }

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

    public class schItem
    {
        public int realm_id { get; set; }
        public int list_id { get; set; }
        }
    }

也许我错过了 INotifyPropertyChanged 类的东西。

我正在 Android 设备中调试

【问题讨论】:

标签: android listview xamarin observablecollection inotifypropertychanged


【解决方案1】:

您可能看不到任何东西的几个原因。

  1. 你的绑定是错误的,所以你不会看到任何文字。修复更改

          <Label Text = "{Binding name}" HeightRequest="20" FontAttributes="Bold"/>
          <Label Text = "{Binding data, StringFormat='a {0:F0}'}" />
    

      <Label Text = "{Binding realm_id}" HeightRequest="20" FontAttributes="Bold"/>
      <Label Text = "{Binding list_id}" />
  1. 检查“delete.png”是否存在于正确的位置,而不是“delete.jpg”

  2. 修复后删除 ListView IsEnabled="False"

【讨论】:

  • 1) 对不起!为了简化,我从代码中删除了名称和数据; 2) delete.png 存在,我看到了; 3)我不会在listview上触摸监听器。问题不是初始视图,而是插入新项目后的更新视图。
  • 在我修复所有答案后,我可以看到 AFTER 我添加了一个项目。如果您需要帮助,您可以安装 teamviewer,我们可以从聊天室进行远程会话
  • 有 StackOverflow ;) 也许这个问题对其他人有用。如果您需要更多代码,请提出要求,但我认为这就足够了。让我知道任何建议
  • 我不再需要任何代码。修复我告诉你的,这会奏效。祝你好运
  • 好的,现在可以了,问题是关于isEnabled="False",但我不能让列表视图禁用并动态添加项目?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-05-27
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多