在附加 属性 中指定存储类型的正确方法?
Correct way to specify type for storage in attached property?
我已经实现了一个附加的 属性 保持一个 System.Type 值,它在 WPF 中工作正常但抛出一个
Windows.UI.Xaml.Markup.XamlParseException
在 WinRT 应用程序中使用时。我猜无法解析指定的类型。
这里是附件的实现属性:
using System;
using System.Diagnostics;
#if NETFX_CORE
using Windows.UI.Xaml;
#else
using System.Windows;
#endif
namespace Test
{
public class AttachedClass
{
}
public static class PropaClass
{
public static Type GetAttachedType(DependencyObject obj)
{
return (Type)obj.GetValue(AttachedTypeProperty);
}
public static void SetAttachedType(DependencyObject obj, Type value)
{
obj.SetValue(AttachedTypeProperty, value);
}
public static readonly DependencyProperty AttachedTypeProperty =
DependencyProperty.RegisterAttached("AttachedType",
typeof(Type), typeof(PropaClass), new PropertyMetadata(null, OnAttachedTypeChanged));
private static void OnAttachedTypeChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
Debug.WriteLine("Set value : {0}", e.NewValue.ToString(), null);
}
}
}
以下是它在 WPF 应用程序中的使用方式:
<Window x:Class="WpfApplication1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:test="clr-namespace:Test"
Title="MainWindow" Height="350" Width="525"
test:PropaClass.AttachedType="{x:Type test:AttachedClass}">
<Grid>
</Grid>
</Window>
以及 WinRT 应用页面:
<Page
x:Class="App1.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:App1"
xmlns:test="using:Test"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
test:PropaClass.AttachedType="test:AttachedClass">
<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
</Grid>
</Page>
我在 WinRT 应用程序中得到的异常是:
Exception:Thrown: "Det gick inte att hitta texten som associeras med den här felkoden.
Failed to create a '%1' from the text '%0'. [Line: 10 Position: 5]" (Windows.UI.Xaml.Markup.XamlParseException)
A Windows.UI.Xaml.Markup.XamlParseException was thrown: "Det gick inte att hitta texten som associeras med den här felkoden.
Failed to create a '%1' from the text '%0'. [Line: 10 Position: 5]"
WinRT information: Failed to create a '%1' from the text '%0'. [Line: 10 Position: 5]
Time: 2015-03-19 12:05:00
Thread:<No Name>[396]
所以我的问题是:我做错了什么?如果我在 WinRT 页面 XAML 中将 test:PropaClass.AttachedType="test:AttachedClass">
替换为 test:PropaClass.AttachedType="test:PropaClass">
,它就像一个魅力!
有没有办法让 XAML 解析器识别 AttachedClass?
P.S。这是使用针对 .NET Framework 4.5 和 Windows8.1
的 VS2013
编辑:
将此添加到 WinRT XAML 页面删除了异常。
<Page.Resources>
<test:AttachedClass x:Key="test"/>
</Page.Resources>
所以 "introducing" 解析器的类型似乎更正了这个问题。但是,这不是一个有效的解决方法,因为我希望能够使用不可实例化的类型。
我开始倾向于解析器错误...
另一个编辑:
我终于找到了可行的解决方法;添加一个空样式,针对您要放入附件的类型 属性.
添加下面的资源可以消除异常。
<Page.Resources>
<Style TargetType="test:AttachedClass"/>
</Page.Resources>
这个解决方案允许我指定接口或抽象基 类 作为类型也进入附加 属性。
我已经尝试了上面 "Another Edit" 中定义的解决方法,并且效果很好。我会 post 它作为答案,因为它解决了我的问题并且没有其他答案被 posted。
这个问题很可能是由于 XAML 解析器中的错误导致它无法使用提供的名称解析类型。如果以另一种方式将该类型引入解析器,它就可以很好地识别。我用来介绍类型的方法是定义一个空的、未使用的样式,以类型为目标存储在附加的 属性.
中
<Page.Resources>
<Style TargetType="test:AttachedClass"/>
</Page.Resources>
这样任何类型都可以存储在 属性.
我已经实现了一个附加的 属性 保持一个 System.Type 值,它在 WPF 中工作正常但抛出一个
Windows.UI.Xaml.Markup.XamlParseException
在 WinRT 应用程序中使用时。我猜无法解析指定的类型。
这里是附件的实现属性:
using System;
using System.Diagnostics;
#if NETFX_CORE
using Windows.UI.Xaml;
#else
using System.Windows;
#endif
namespace Test
{
public class AttachedClass
{
}
public static class PropaClass
{
public static Type GetAttachedType(DependencyObject obj)
{
return (Type)obj.GetValue(AttachedTypeProperty);
}
public static void SetAttachedType(DependencyObject obj, Type value)
{
obj.SetValue(AttachedTypeProperty, value);
}
public static readonly DependencyProperty AttachedTypeProperty =
DependencyProperty.RegisterAttached("AttachedType",
typeof(Type), typeof(PropaClass), new PropertyMetadata(null, OnAttachedTypeChanged));
private static void OnAttachedTypeChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
Debug.WriteLine("Set value : {0}", e.NewValue.ToString(), null);
}
}
}
以下是它在 WPF 应用程序中的使用方式:
<Window x:Class="WpfApplication1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:test="clr-namespace:Test"
Title="MainWindow" Height="350" Width="525"
test:PropaClass.AttachedType="{x:Type test:AttachedClass}">
<Grid>
</Grid>
</Window>
以及 WinRT 应用页面:
<Page
x:Class="App1.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:App1"
xmlns:test="using:Test"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
test:PropaClass.AttachedType="test:AttachedClass">
<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
</Grid>
</Page>
我在 WinRT 应用程序中得到的异常是:
Exception:Thrown: "Det gick inte att hitta texten som associeras med den här felkoden.
Failed to create a '%1' from the text '%0'. [Line: 10 Position: 5]" (Windows.UI.Xaml.Markup.XamlParseException)
A Windows.UI.Xaml.Markup.XamlParseException was thrown: "Det gick inte att hitta texten som associeras med den här felkoden.
Failed to create a '%1' from the text '%0'. [Line: 10 Position: 5]"
WinRT information: Failed to create a '%1' from the text '%0'. [Line: 10 Position: 5]
Time: 2015-03-19 12:05:00
Thread:<No Name>[396]
所以我的问题是:我做错了什么?如果我在 WinRT 页面 XAML 中将 test:PropaClass.AttachedType="test:AttachedClass">
替换为 test:PropaClass.AttachedType="test:PropaClass">
,它就像一个魅力!
有没有办法让 XAML 解析器识别 AttachedClass?
P.S。这是使用针对 .NET Framework 4.5 和 Windows8.1
的 VS2013编辑:
将此添加到 WinRT XAML 页面删除了异常。
<Page.Resources>
<test:AttachedClass x:Key="test"/>
</Page.Resources>
所以 "introducing" 解析器的类型似乎更正了这个问题。但是,这不是一个有效的解决方法,因为我希望能够使用不可实例化的类型。
我开始倾向于解析器错误...
另一个编辑:
我终于找到了可行的解决方法;添加一个空样式,针对您要放入附件的类型 属性.
添加下面的资源可以消除异常。
<Page.Resources>
<Style TargetType="test:AttachedClass"/>
</Page.Resources>
这个解决方案允许我指定接口或抽象基 类 作为类型也进入附加 属性。
我已经尝试了上面 "Another Edit" 中定义的解决方法,并且效果很好。我会 post 它作为答案,因为它解决了我的问题并且没有其他答案被 posted。
这个问题很可能是由于 XAML 解析器中的错误导致它无法使用提供的名称解析类型。如果以另一种方式将该类型引入解析器,它就可以很好地识别。我用来介绍类型的方法是定义一个空的、未使用的样式,以类型为目标存储在附加的 属性.
中<Page.Resources>
<Style TargetType="test:AttachedClass"/>
</Page.Resources>
这样任何类型都可以存储在 属性.