1

我有一个继承自 BaseViewModel 的 ConfigurationViewModel。BaseViewModel 放置在单独的项目中,并实现了 INotifyPropertyChanged 接口。这个单独的项目没有安装 Fody.PropertyChanged。

当我尝试构建解决方案时,我遇到了异常:

Failed to execute weaver C:\Users\Rafal\.nuget\packages\propertychanged.fody\2.6.0\build\..\netclassicweaver\PropertyChanged.Fody.dll
Type:
System.Exception
StackTrace:
   at InnerWeaver.ExecuteWeavers() in C:\projects\fody\FodyIsolated\InnerWeaver.cs:line 186
   at InnerWeaver.Execute() in C:\projects\fody\FodyIsolated\InnerWeaver.cs:line 109
Source:
FodyIsolated
TargetSite:
Void ExecuteWeavers()
Could not inject EventInvoker method on type 'Designer.VVM.ConfigurationViewModel'. It is possible you are inheriting from a base class and have not correctly set 'EventInvokerNames' or you are using a explicit PropertyChanged event and the event field is not visible to this instance. Either correct 'EventInvokerNames' or implement your own EventInvoker on this class. If you want to suppress this place a [DoNotNotifyAttribute] on Designer.VVM.ConfigurationViewModel.
Type:
Fody.WeavingException
StackTrace:
   at ModuleWeaver.InjectMethod(TypeDefinition targetType, InvokerTypes& invokerType)
   at ModuleWeaver.AddOnPropertyChangedMethod(TypeDefinition targetType)
   at ModuleWeaver.FindMethodsForNodes()
   at ModuleWeaver.Execute()
   at InnerWeaver.ExecuteWeavers() in C:\projects\fody\FodyIsolated\InnerWeaver.cs:line 182
Source:
PropertyChanged.Fody
TargetSite:
Mono.Cecil.MethodDefinition InjectMethod(Mono.Cecil.TypeDefinition, InvokerTypes ByRef)

当我将 BaseViewModel 移动到与 ConfigurationViewModel 相同的项目时,一切正常。

这是标准行为吗?它是一个错误吗?难道我做错了什么?如何在单独的项目中使用具有 BaseViewModel 的 Fody.PropertyChanged?

4

1 回答 1

1

当基础视图模型在同一个项目中时,可以将“OnPropertyChanged”方法注入到该类中。从另一个项目引用基本视图模型时,该项目必须具有与约定匹配的“OnPropertyChanged”或使用 EventInvokerNames https://github.com/Fody/PropertyChanged/wiki/EventInvokerSelectionInjection自定义方法名称

总之,尝试将此添加到您的基本视图模型

public virtual void OnPropertyChanged(string propertyName)
{
    var propertyChanged = PropertyChanged;
    if (propertyChanged != null)
    {
        propertyChanged(this, new PropertyChangedEventArgs(propertyName));
    }
}
于 2019-03-27T23:14:30.513 回答