“属性'依赖关系'在此声明类型上无效。”错误

时间:2021-01-03 16:32:00

Why I receive such message?

为什么我收到这样的消息?

Attribute 'Dependency' is not valid on this declaration type. It is only valid on 'assembly' declarations.

属性“依赖关系”在此声明类型上无效。它仅对'assembly'声明有效。

public partial class MainWindow : Window
{
    private OverviewViewModel _vm;

    [Dependency]
    public OverviewViewModel VM
    {
        set
        {
            _vm = value;
            this.DataContext = _vm;
        }
    }

3 个解决方案

#1


5  

You are probably using the wrong attribute: DependencyAttribute

您可能使用了错误的属性:DependencyAttribute

Indicates when a dependency is to be loaded by the referring assembly [...]

指示何时由引用程序集加载依赖项[...]

and can only be applied to assemblies (and not to properties like you are trying), e.g.:

并且只能应用于程序集(而不是像你正在尝试的属性),例如:

[assembly: Dependency(/*...*/)]

#2


5  

Attributes are allowed to declare what they can apply to (via AttributeUsageAttribute). The default is anything, but in this case it is "assembly", meaning: you can only apply this at the assembly level, which you do via:

允许属性声明它们可以应用的内容(通过AttributeUsageAttribute)。默认值是任何值,但在这种情况下它是“assembly”,意思是:您只能在汇编级别应用它,您可以通过以下方式执行此操作:

[assembly:Dependency(...)]

If this is your own attribute, check the AttributeUsageAttribute associated with it, and ensure it includes properties (using the pipe | to apply "or").

如果这是您自己的属性,请检查与其关联的AttributeUsageAttribute,并确保它包含属性(使用管道|以应用“或”)。

If it is not your attribute, double-check the intended usage - you might be using it wrong.

如果它不是您的属性,请仔细检查预期用途 - 您可能使用错误。

#3


0  

Try including the getter:

尝试包括getter:

private OverviewViewModel _vm;

[Dependency]
public OverviewViewModel VM
{
    set
    {
        _vm = value;
        this.DataContext = _vm;
    }
    get
    {
        return _vm;
    }
}

#1


5  

You are probably using the wrong attribute: DependencyAttribute

您可能使用了错误的属性:DependencyAttribute

Indicates when a dependency is to be loaded by the referring assembly [...]

指示何时由引用程序集加载依赖项[...]

and can only be applied to assemblies (and not to properties like you are trying), e.g.:

并且只能应用于程序集(而不是像你正在尝试的属性),例如:

[assembly: Dependency(/*...*/)]

#2


5  

Attributes are allowed to declare what they can apply to (via AttributeUsageAttribute). The default is anything, but in this case it is "assembly", meaning: you can only apply this at the assembly level, which you do via:

允许属性声明它们可以应用的内容(通过AttributeUsageAttribute)。默认值是任何值,但在这种情况下它是“assembly”,意思是:您只能在汇编级别应用它,您可以通过以下方式执行此操作:

[assembly:Dependency(...)]

If this is your own attribute, check the AttributeUsageAttribute associated with it, and ensure it includes properties (using the pipe | to apply "or").

如果这是您自己的属性,请检查与其关联的AttributeUsageAttribute,并确保它包含属性(使用管道|以应用“或”)。

If it is not your attribute, double-check the intended usage - you might be using it wrong.

如果它不是您的属性,请仔细检查预期用途 - 您可能使用错误。

#3


0  

Try including the getter:

尝试包括getter:

private OverviewViewModel _vm;

[Dependency]
public OverviewViewModel VM
{
    set
    {
        _vm = value;
        this.DataContext = _vm;
    }
    get
    {
        return _vm;
    }
}