繁体   English   中英

将自定义附加属性与基础 Class 和 Generics 一起使用

[英]Using Custom Attached Properties with Base Class and Generics

我希望在 .NET Framework 4.7.2 class 库中使用 AngelSix 的自定义附加属性代码 但是,当我尝试将附加属性添加到 Xaml 文件中的 object 时,我从 Visual Studio 中的 Intellisense 得到的是local:BaseAttachedProperty`2.value而不是所需的附加属性,如local:IsBusyProperty.Value 当我手动键入local:IsBusyProperty.Value时,应用程序崩溃并显示错误消息The method or operation is not implemented指向附加属性。 我了解`2表示具有 2 个泛型参数的泛型类型 - 暗指带有两个泛型参数的 Base Class:

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

但是如何获得正确的附加属性,即使用通用参数实现基本 class 而不是这个神秘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" />

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

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

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

确实需要导入正确的xmlns (XML 命名空间)才能使用它,这将是定义IsBusyProperty class 的命名空间。 如果您想使用已在 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 Visual Studio 应该会告诉您正确的值是什么。fas 只是一个abritrary name 我从库名称的前 3 个字母中选择,但您可以使用任何名称,只要您是一致的。有关xmlns的更多信息,请查看MSDN 文章


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

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

然后你会像这样使用它:

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

其中yourns是您在其中定义SomeOtherProperty的任何命名空间的xmlns

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM