我想在DependencyProperty
的扩展类中创建一个FrameworkElement
,并将xaml属性绑定到它。
这个想法来自Windows社区工具包的源代码:https://github.com/windows-toolkit/WindowsCommunityToolkit
他们使用扩展将DependencyProperty
添加到FrameworkElementExtensions.ActualSize.cs文件中的FrameworkElement
。
我可以毫无问题地使用他们的扩展,但是当我尝试自己做同样的事情时,我得到了一个Windows.UI.Xaml.Markup.XamlParseException: 'XAML parsing failed.'
我甚至尝试复制/粘贴他们的扩展类,问题仍然存在。
我的项目是一个UWP项目。
这是一些简单的代码来测试它:
<UserControl
xmlns:myExtensions="using:MyProject.Extensions"
xmlns:toolkitExtensions="using:Microsoft.Toolkit.Uwp.UI.Extensions"
>
<Grid>
<Grid x:Name="TestElementName"
myExtensions:FrameworkElementExtensions.CustomProperty="0.5"
toolkitExtensions:FrameworkElementExtensions.EnableActualSizeBinding="true"
/>
<Grid Width="{Binding ElementName=TestElementName, Path=(toolkitExtensions:FrameworkElementExtensions.ActualWidth)}"
Height="{Binding ElementName=TestElementName, Path=(myExtensions:FrameworkElementExtensions.ActualHeight)}"
Opacity="{Binding ElementName=TestElementName, Path=(myExtensions:FrameworkElementExtensions.CustomProperty)}"
/>
</Grid>
</UserControl>
namespace MyProject.Extensions
{
public static partial class FrameworkElementExtensions
{
public static readonly DependencyProperty ActualHeightProperty = DependencyProperty.RegisterAttached("ActualHeight", typeof(double), typeof(FrameworkElement), new PropertyMetadata(double.NaN));
public static double GetActualHeight(FrameworkElement obj)
{
return (double)obj.GetValue(ActualHeightProperty);
}
public static void SetActualHeight(FrameworkElement obj, double value)
{
obj.SetValue(ActualHeightProperty, value);
}
public static readonly DependencyProperty CustomPropertyProperty = DependencyProperty.RegisterAttached("CustomProperty", typeof(double), typeof(FrameworkElement), new PropertyMetadata(1.0));
public static double GetCustomProperty(FrameworkElement obj)
{
return (double)obj.GetValue(CustomPropertyProperty);
}
public static void SetCustomProperty(FrameworkElement obj, double value)
{
obj.SetValue(CustomPropertyProperty, value);
}
}
}
ActualHeight属性是从Windows Community Toolkit复制/粘贴的
CustomProperty属性是我的
第一个网格中的分配正常工作,但是第二个网格中的高度和不透明度的绑定会引发异常。
从Community Toolkit命名空间而不是我的命名空间使用时,我看不出它是如何工作的。
例外细节:
类型:Windows.UI.Xaml.Markup.XamlParseException
消息:XAML parsing failed.
堆栈跟踪:
at Windows.UI.Xaml.Application.LoadComponent(Object component, Uri resourceLocator, ComponentResourceLocation componentResourceLocation)
at MyProject.Controls.MyControl.InitializeComponent()
at MyProject.Controls.MyControl..ctor()
at MyProject.MyProject_XamlTypeInfo.XamlTypeInfoProvider.Activate_271_MyControl()
at MyProject.MyProject_XamlTypeInfo.XamlUserType.ActivateInstance()
InnerException为null。
谁能帮我这个?
谢谢。
由于canton7的洞察力,我修复了它。
问题来自DependencyProperty
宣言。
更换
public static readonly DependencyProperty CustomPropertyProperty = DependencyProperty.RegisterAttached("CustomProperty", typeof(double), typeof(FrameworkElement), new PropertyMetadata(1.0));
同
public static readonly DependencyProperty CustomPropertyProperty = DependencyProperty.RegisterAttached("CustomProperty", typeof(double), typeof(FrameworkElementExtensions), new PropertyMetadata(1.0));
这里的关键是来自ownerType
的DependencyProperty.RegisterAttached()
参数。它必须是扩展类型,而不是扩展类型。