【问题标题】:How to detect property changes in Windows Runtime如何检测 Windows 运行时中的属性更改
【发布时间】:2014-05-05 19:16:30
【问题描述】:

以前在Windows Phone Silverlight 应用程序中,我们可以实现INotifyPropertyChanged 并提高它:

    public int Id
    {
        get { return id; }
        set
        {
            if (value != id)
            {
                id = value;
                NotifyPropertyChanged("Id");
            }
        }
    }

目前,在Windows Runtime App的默认集线器模板中,我看到我们使用

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

它只会检测是否添加或删除了一个集合项目,但是如何更改现有项目的属性? 我更改了一个项目的属性,但它没有生效。怎么了?谢谢。

更新:我知道我们仍然可以做到这一点并且有效。但这是在 Windows 运行时应用程序中推荐的方法吗?

【问题讨论】:

  • 还是和 WPF 一样。
  • 模板只是一个模板。它不是任何东西的全功能实现。

标签: c# xaml windows-phone-8 windows-runtime windows-phone


【解决方案1】:

在您的 Item 类中,您希望在该类中实现 INotifyPropertyChanged。您想要修改现有项目属性的任何内容都将通过调用RaisePropertyChanged() 事件反映到 UI。

public class Item : INotifyPropertyChanged
{
  // Omitted implementation of INPC

  private string _name;
  public string Name { get { return name; } set { _name = value; RaisePropertyChanged(); } }

  public void Update()
  {
     Name = "NewValue"; // This will show up in the UI if it is bound to a property (DataBinding)
  }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-06-13
    • 1970-01-01
    • 2018-01-13
    • 1970-01-01
    • 2019-08-18
    • 1970-01-01
    • 1970-01-01
    • 2021-08-05
    相关资源
    最近更新 更多