ReactiveUI RaiseAndSetIfChanged()支持字段可访问性

时间:2013-09-22 20:31:48

标签: c# silverlight properties system.reactive reactiveui

问题:无法将属性的支持字段设置为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:

    public string _Name;

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

1 个答案:

答案 0 :(得分:2)

请使用新的重载,除非您没有使用> = VS2012:

this.RaiseAndSetIfChanged(ref theBackingField, value);
相关问题