首页 > 解决方案 > 将自定义附加属性与基类和泛型一起使用

问题描述

我希望在 .NET Framework 4.7.2 类库中使用 AngelSix 的自定义附加属性代码。但是,当我尝试将附加属性添加到 Xaml 文件中的对象时,我从 Visual Studio 中的 Intellisense 得到的local:BaseAttachedProperty`2.value不是所需的附加属性,例如local:IsBusyProperty.Value. 当我手动输入时,应用程序崩溃并出现指向附加属性local:IsBusyProperty.Value的错误消息。The method or operation is not implemented我理解这 `2意味着一个泛型类型,带有 2 个泛型参数 - 暗指带有两个泛型参数的基类:

public abstract class BaseAttachedProperty<Parent, Property>
        where Parent : new(){}

但是我如何获得正确的附加属性,即使用泛型参数实现基类而不是这个神秘local:BaseAttachedProperty`2.value消息的类?

如果我创建一个附加属性而不从 BaseAttachedProperty 继承,一切正常。

德国论坛上的一位用户也遇到了同样的问题,并写道他可以xmlns:用来获取附加属性,但这对我不起作用!

这是 AngelSix 提供的示例。它对我不起作用。

public class IsBusyProperty : BaseAttachedProperty<IsBusyProperty, bool>
{
}

这是我的有效代码:

public class IsBusyProperty
    {
        public static readonly DependencyProperty ValueProperty =
            DependencyProperty.RegisterAttached("Value", typeof(bool), typeof(IsBusyProperty), new PropertyMetadata(false));
        public static bool GetValue(DependencyObject obj)
        {
            return (bool)obj.GetValue(ValueProperty);
        }
        public static void SetValue(DependencyObject obj, bool value)
        {
            obj.SetValue(ValueProperty, value);
        }
    }

代码使用如下(这里我的代码没有使用):

<Button Content="Login" 
                                    IsDefault="True"
                                    local:BaseAttachedProperty`2.Value="{Binding LoginIsRunning}" <-- Here is the trouble area
                                    Command="{Binding LoginCommand}"
                                    CommandParameter="{Binding ElementName=Page}" 
                                    HorizontalAlignment="Center" />

如果附加的属性有效,正确的代码应该是

<Button Content="Login" 
                                    IsDefault="True"
                                    local:IsBusyProperty.Value="{Binding LoginIsRunning}"
                                    Command="{Binding LoginCommand}"
                                    CommandParameter="{Binding ElementName=Page}" 
                                    HorizontalAlignment="Center" />

标签: c#wpfgenericsbase-classattached-properties

解决方案


我从未使用过您链接到的代码,但据我所知,我认为您误解了它的用途。让我把你的注意力带回到你引用的这段代码上:

public class IsBusyProperty : BaseAttachedProperty<IsBusyProperty, bool>
{
}

这不是您应该填写的示例,它实际上是您可以使用的功能齐全的附加属性。它为空的原因是因为您需要的所有代码都已经在基类BaseAttachedProperty中。

确实需要导入正确的xmlns(XML 命名空间)才能使用它,这将IsBusyProperty是定义类的命名空间。如果您想使用已在 Fasetto.Word 库中定义的代码,代码可能是:

xmlns:fas="clr-namespace:Fasetto.Word;assembly=Fasetto.Word"

...

<Button fas:IsBusyProperty.Value="{Binding LoginIsRunning}"/>

我不能 100% 确定该assembly=Fasetto.Word部分是否正确,但如果您开始键入xmlns:fas="clr-namespace:Fasetto.Word. fas只是我从库名称的前 3 个字母中选择的一个缩写名称,但只要您坚持使用,您就可以使用任何名称。有关详细信息xmlns,请查看MSDN 文章


如果您想使用创建自己的附加属性BaseAttachedProperty,您可以执行以下操作:

public class SomeOtherProperty : BaseAttachedProperty<SomeOtherProperty, TypeOfProperty>
{
}

然后你会像这样使用它:

<Button yourns:SomeOtherProperty.Value="{Binding Something}"/>

您在其中定义yournsxmlns任何命名空间在哪里SomeOtherProperty


推荐阅读