C# 对基类和泛型使用自定义附加属性

C# 对基类和泛型使用自定义附加属性,c#,wpf,generics,base-class,attached-properties,C#,Wpf,Generics,Base Class,Attached Properties,我希望在.NETFramework 4.7.2类库中使用AngelSix的自定义附加属性。但是,当我尝试向Xaml文件中的对象添加附加属性时,我从Visual Studio中的Intellisense获得的是local:BaseAttachedProperty`2.value,而不是所需的附加属性,如local:IsBusyProperty.value。当我手动键入local:IsBusyProperty.Value时,应用程序崩溃,错误消息指向附加属性的方法或操作未实现。 我理解`2表示一个泛

我希望在.NETFramework 4.7.2类库中使用AngelSix的自定义附加属性。但是,当我尝试向Xaml文件中的对象添加附加属性时,我从Visual Studio中的Intellisense获得的是
local:BaseAttachedProperty`2.value
,而不是所需的附加属性,如
local:IsBusyProperty.value
。当我手动键入
local:IsBusyProperty.Value
时,应用程序崩溃,错误消息
指向附加属性的方法或操作未实现。
我理解
`2
表示一个泛型类型,具有两个泛型参数-暗指具有两个泛型参数的基类:

public abstract class BaseAttachedProperty<Parent, Property>
        where Parent : new(){}
以下是我的代码:

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);
        }
    }
代码的使用方式如下(此处我的代码未被使用):


我从未使用过您链接到的代码,但从我看到的情况来看,我认为您误解了它的用途。让我把您的注意力带回您引用的代码:

public class IsBusyProperty : BaseAttachedProperty<IsBusyProperty, bool>
{
}
我不能100%确定
assembly=Fasetto.Word
部分是否正确,但是如果您开始键入
xmlns:fas,Visual Studio应该告诉您正确的值是多少=“clr命名空间:Fasetto.Word
fas
只是我从库名的前三个字母中选择的一个简略名称,但只要你们愿意,你们可以使用任何东西。有关
xmlns
的更多信息,请查看


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

public class SomeOtherProperty : BaseAttachedProperty<SomeOtherProperty, TypeOfProperty>
{
}
<Button yourns:SomeOtherProperty.Value="{Binding Something}"/>
公共类SomeOtherProperty:BaseAttachedProperty
{
}
然后你会这样使用它:

public class SomeOtherProperty : BaseAttachedProperty<SomeOtherProperty, TypeOfProperty>
{
}
<Button yourns:SomeOtherProperty.Value="{Binding Something}"/>


其中,
yourns
是您在中定义的
SomeOtherProperty
的名称空间的
xmlns

请在使用
BaseAttachedProperty
@KeithStein的地方包含您的C代码。我已经用更多细节更新了这个问题谢谢您的回答。我非常理解你写的东西。事实上,我确实做到了。也就是说,BaseAttachedProperty在我的本地名称空间中,我创建了一个从BaseAttachedProperty继承的类。在我的xaml页面中,我包含了本地名称空间xmlns:local=“clr namespace:my.Assembly;但intellisense没有显示。我尝试在没有BaseAttachedProperty类的情况下创建自己的AP-intellisense显示了自己的AP,我可以在xaml中使用它。我想知道是否需要更改
所有者类型:typeof(BaseAttachedProperty)
要使
APs
在XAML中可见,需要做一些不同的事情。@Dorogzz-Huh,我自己做了一个测试,我确实看到了同样的事情,IntelliSense只建议
BaseAttachedProperty`2
。如果手动键入
yourns:SomeOtherProperty.Value
,您仍然可以按预期使用该属性,但它不会显示在Inte中这很烦人也很奇怪,但我不确定我能帮你做那个部分。
public class SomeOtherProperty : BaseAttachedProperty<SomeOtherProperty, TypeOfProperty>
{
}
<Button yourns:SomeOtherProperty.Value="{Binding Something}"/>