【问题标题】:ReactiveUI RaiseAndSetIfChanged() Backing Field AccesibilityReactiveUI RaiseAndSetIfChanged() 支持字段可访问性
【发布时间】:2013-09-22 20:31:48
【问题描述】:

问题:无法将属性的支持字段设置为 private,因为在将值设置为 Name 时出现以下异常。

System.ArgumentException : You must declare a 
backing field for this property named: _Name

我的代码:

public class MyVM : ReactiveObject
{
    private string _Name;
    public string Name
    {
        get { return _Name; }

        set { this.RaiseAndSetIfChanged(x => x.Name, value); }
    }
}

为了解决这个问题,我可以将 _Name 设置为公开:

    public string _Name;

这解决了问题,但为什么我必须公开支持字段?我在网上看到的示例使用私有支持字段...?

【问题讨论】:

    标签: c# silverlight properties system.reactive reactiveui


    【解决方案1】:

    改用新的重载,除非你没有使用 >= VS2012:

    this.RaiseAndSetIfChanged(ref theBackingField, value);
    

    【讨论】:

    • 谢谢!!!工作完美!在 System.Reactive 和 ReactiveUI 上做得很好!!!试过谷歌搜索,但在任何地方都找不到! :D
    猜你喜欢
    • 2019-04-03
    • 1970-01-01
    • 2018-05-12
    • 2012-02-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-03-07
    相关资源
    最近更新 更多