我希望在中使用AngelSix的自定义附加属性Code。NET Framework 4.7.2类库。但是,当我尝试将附加属性添加到Xaml文件中的对象时,我从Visual Studio中的Intellisense得到的是local:BaseAttachedProperty`2.value
,而不是像local:IsBusyProperty.Value
那样的所需附加属性。当我手动键入local:IsBusyProperty.Value
时,应用程序崩溃,错误消息The method or operation is not implemented
指向附加的属性。我理解`2
的意思是一个泛型类型,有两个泛型参数——暗指基类有两个通用参数:
public abstract class BaseAttachedProperty<Parent, Property>
where Parent : new(){}
但是,我如何获得正确的附加属性,即用泛型参数而不是这个神秘的local:BaseAttachedProperty`2.value
消息实现基类的类?
如果我在不继承BaseAttachedProperty的情况下创建了一个AttachedProperty,那么一切都很好。
德国论坛上的一位用户也遇到了同样的问题,他写道他可以使用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>
{
}
这不是一个你应该填写的exmaple,它实际上是一个你可以使用的功能齐全的附加属性。之所以它是空的,是因为您需要的所有代码都已经在基类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
,Visual Studio应该会告诉您正确的值是多少。fas
只是我从库名称的前3个字母中选择的一个结构名称,但只要您是一致的,您就可以使用任何名称。有关xmlns
的更多信息,请查看MSDN文章。
如果您想使用BaseAttachedProperty
创建自己的附加属性,您可以执行以下操作:
public class SomeOtherProperty : BaseAttachedProperty<SomeOtherProperty, TypeOfProperty>
{
}
然后你会这样使用它:
<Button yourns:SomeOtherProperty.Value="{Binding Something}"/>
其中yourns
是在.中定义的SomeOtherProperty
的任何命名空间的xmlns