我想在扩展类中为FrameworkElement
创建一个DependencyProperty
,并将xaml属性绑定到它。
这个想法来自Windows社区工具包的源代码: https://github.com/windows-toolkit/WindowsCommunityToolkit
他们使用扩展将DependencyProperty
添加到FrameworkElement
中,在 FrameworkElementExtensions.ActualSize.cs 文件中。
我可以毫无问题地使用它们的扩展,但是当我尝试自己做同样的事情时,我会得到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);
}
}
}
属性是从 Windows 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));
这里的关键是来自DependencyProperty.RegisterAttached()
的ownerType
参数。它必须是扩展类型,而不是扩展类型。