使用Fody [ImplementPropertyChanged]时出错

时间:2017-06-10 06:30:57

标签: c# wpf mvvm fody

我正在使用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]不应该在这一点上出错,教师成功地实现了它,那么我的代码中是否有任何遗漏? 错误说:

  

严重级代码描述项目文件行抑制状态   错误CS0619'EtrumentPropertyChangedAttribute'已废弃:'这   配置选项已被弃用。使用此属性   是将INotifyPropertyChanged添加到具有关联事件的类中   定义。之后所有实现的类   INotifyPropertyChanged有他们的属性编织,他们有天气   至少是ImplementPropertyChangedAttribute。这个属性经常出现   被错误地解释为具有属性的选择方法   编织,这从来就不是意图,也不是它的运作方式。这个   属性已被替换   AddINotifyPropertyChangedInterfaceAttribute“。 GProject_MVVM c:\ users \ ahmed   hussainy \ documents \ visual studio   2017 \ Projects \ GProject_MVVM \ GProject_MVVM \ ViewModel \ BaseViewModel.cs 9 Active

2 个答案:

答案 0 :(得分:12)

异常已经说明了答案。

  

ImplementPropertyChangedAttribute'已过时:'此配置   选项已被弃用。使用此属性是为了添加   INotifyProperty更改为具有关联事件的类   定义。之后所有实现的类   INotifyPropertyChanged有他们的属性编织,他们有天气   至少是ImplementPropertyChangedAttribute。

使用新版本的Fody.PropertyChanged,您不再需要添加属性。只需将您想要编织的类INotifyPropertyChanged编成一个类,它就能正常工作。

所以基本上只需删除/删除[ImplementPropertyChanged] ,它就会编译和编织(如果编织器出现在FodyWeavers.xml中)

答案 1 :(得分:0)

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

这样 Fody 就会将 INotifyPropertyChanged 接口添加到您的类中,然后织布工将正确实现它。