MVVM INotifyPropertyChanged的,具有自动属性名称执行属性、名称、MVVM、INotifyPropertyChanged

2023-09-06 16:12:05 作者:回懵一笑百眉生

这是我的理解,我们可以用INofityProperty在MVVM风格的应用程序与code类似于以下

From my understanding, we can use the INofityProperty in a MVVM style application with code similar to the following

    object _SelectedPerson;
    public object SelectedPerson
    {
        get
        {
            return _SelectedPerson;
        }
        set
        {
            if (_SelectedPerson != value)
            {
                _SelectedPerson = value;
                RaisePropertyChanged("SelectedPerson");
            }
        }
    }

现在,我已经看到了约什 - 史密斯的很好的例子的他实现额外的code捕捉到会发生什么,如果在属性名开发类型而无法识别,如输入错误!

Now, I've seen Josh Smith's excellent example where he implements extra code to capture what happens if the developer types in a Property name which isn't recognized, such as a typo!

请告诉我,如果你恨这一点,但有一种方式来获得从堆栈跟踪的方法名。因此,我们就可以实现,而不是像

Please tell me if you hate this, but there is a way to get the method name from the stack trace. So, we could instead implement something like

    object _SelectedPerson;
    public object SelectedPerson
    {
        get
        {
            return _SelectedPerson;
        }
        set
        {
            if (_SelectedPerson != value)
            {
                _SelectedPerson = value;
                RaisePropertyChanged(Current.Method);
            }
        }
    }

static class Current
{
    public static string Method()
    {
        StackTrace st = new StackTrace();
        return (st.GetFrame(1).GetMethod().Name.Split('_')[1]);            
    }
}

我只能假设这永远是可行的,因为在RaisePropertyChanged事件总是发生在二传手(如果我错了,请大家指正)。

I can only assume this will always work since the the RaisePropertyChanged event always occurs in the Setter (and if I'm wrong, please do correct me).

现在请注意,我不是在一个位置,真正尝试这么做,可以在工作(在那里我可以工作在更大的项目),我仍然对.NET 2.0,因此WPF / MVVM是一个很长的路要走在未来,但我正在学习在我自己的时间。

Now please note, I'm not in a position to really try this, in that at work (where I can work on bigger projects) I'm still on .NET 2.0 and so WPF/MVVM is a long way off in the future but I'm learning in my own time.

所以,我的问题是,那些谁使用它,是不是真的好有它提醒错误的用户相比,去掉选项错误的方法(或者你觉得我已经错过理解的东西);事实是,约什 - 史密斯是公认的,在这方面的专家,所以如果他认为这种做法通常然后我会盲从但在这种情况下,我不禁测验,并觉得有必要来了解更多。

So, my question is from those who have used it, is it really better to have an approach which alerts the user of the error compared to removing the option for the error (or do you feel I've miss-understood something); the thing is, Josh Smith is recognized, an expert in this field, and so if he suggests that approach then normally I'd follow blindly but in this case I can't help but quiz it and feel the need to understand more.

推荐答案

使用的堆栈跟踪的问题是,这是不正确填充在发布版本。为了解决这个问题有几种方法来解决它,使提高PropertyChanged事件更容易开发。

The Problem with using the StackTrace is, that it is not properly populated in release builds. To overcome this issue there are several approaches to fix it and make raising the PropertyChanged event easier for developers.

请参阅这个问题:Implementing INotifyPropertyChanged的 - 没有一种更好的方式存在并选择适合的解决方案,你:)

See this question: Implementing INotifyPropertyChanged - does a better way exist? and pick a solution that suits you :)

我个人preFER如下:

Personally I prefer the following:

// Helper method
public static PropertyChangedEventArgs CreateArguments<TOwner>(Expression<Func<TOwner, object>> Expression) {
    // determine the Name of the property using the Expression
}

// Within the view-model implementations:
private static readonly PropertyChangedEventArgs TitleProperty = CreateArguments<MyViewModel>(m => m.Title);

private string title;

public string Title {
    get { return this.title; }
    set {
        if (!string.Equals(this.title, value) {
            this.title = value;
            this.OnPropertyChanged(TitleProperty);
        }
    }
}

通过使用一个静态成员,以pre-产生PropertyChangedEventArgs,通过检查前pression树引入的开销是有限的。该解决方案是重系数安全的,所以你不要有什么神奇的字符串。

By using a static member to pre-generate the PropertyChangedEventArgs, the overhead introduced by inspecting the expression tree is limited. This solution is re-factor safe, so you don't have any magic strings.

我也很喜欢使用CallerMemberNameAttribute的.NET 4.5的做法,但它似乎并没有在便携类图书馆工作。

I also like the .NET 4.5 Approach using the CallerMemberNameAttribute, but It seems that it doesn't work in Portable Class Libraries.