使用 Fody [ImplementPropertyChanged] 时出错

Error on using Fody [ImplementPropertyChanged]

我使用的是 VS 2017 社区版 我正在创建 MVVM 模式。安装 fody 后,我的代码出现错误,而教程的讲师在 vs 2015 上实现了它 这是代码:

using PropertyChanged;
using System.ComponentModel;

namespace GProject_MVVM.ViewModel
{
    /// <summary>
    /// A base view model that fires Property Changed events as needed
    /// </summary>
    [ImplementPropertyChanged] // **I got error here**
    public class BaseViewModel : INotifyPropertyChanged
    {
        /// <summary>
        /// The event that is fired when any child property changes its value
        /// </summary>
        public event PropertyChangedEventHandler PropertyChanged = (sender, e) => { };
        /// <summary>
        /// Call this to fire <see cref="PropertyChanged"/> event
        /// </summary>
        /// <param name="name"></param>
        public void OnPropertyChanged(string name)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(name));

        }
    }
}

[ImplementPropertyChanged] 在这一点上应该不会出错,讲师成功实施了它,那么我的代码中是否缺少任何内容? 错误说:

Severity Code Description Project File Line Suppression State Error CS0619 'ImplementPropertyChangedAttribute' is obsolete: 'This configuration option has been deprecated. The use of this attribute was to add INotifyPropertyChanged to a class with its associated event definition. After that all classes that implement INotifyPropertyChanged have their properties weaved, weather they have the ImplementPropertyChangedAttribute or not. This attribute was often incorrectly interpreted as an opt in approach to having properties weaved, which was never the intent nor how it ever operated. This attribute has been replaced by AddINotifyPropertyChangedInterfaceAttribute.' GProject_MVVM c:\users\ahmed hussainy\documents\visual studio 2017\Projects\GProject_MVVM\GProject_MVVM\ViewModel\BaseViewModel.cs 9 Active

异常已经说明了答案。

ImplementPropertyChangedAttribute' is obsolete: 'This configuration option has been deprecated. The use of this attribute was to add INotifyPropertyChanged to a class with its associated event definition. After that all classes that implement INotifyPropertyChanged have their properties weaved, weather they have the ImplementPropertyChangedAttribute or not.

使用新版本的 Fody.PropertyChanged,您不再需要添加该属性。只需制作 class 你想要编织的工具 INotifyPropertyChanged 就可以了。

所以基本上只是 删除/删除 [ImplementPropertyChanged] 它会编译和编织(如果编织器存在于 FodyWeavers.xml

如果您最初完全按照预期的方式使用此属性,则应将其替换为 [AddINotifyPropertyChangedInterface]

这样 Fody 会把 INotifyPropertyChanged 接口添加到你的 class,然后 weaver 会正确实现它。