System.Type 作为转换器的 属性 - 仅适用于隐藏代码中未使用的 属性
System.Type as property of converter - only works with unused property in code behind
我有一个 IValueConverter,它有一个 System.Type 属性 设置在 XAML.
转换器:
internal class EnumTypeConverter : IValueConverter
{
public Type TypeToDisplay { get; set; }
public object Convert(object value, Type targetType, object parameter, string language)
{
return TypeToDisplay?.FullName;
}
public object ConvertBack(object value, Type targetType, object parameter, string language)
{
throw new NotImplementedException();
}
}
XAML:
<Page
x:Class="UWPSystemTypeConverterTest.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:converter="using:UWPSystemTypeConverterTest.Converter"
xmlns:enums="using:UWPSystemTypeConverterTest.Enum"
mc:Ignorable="d">
<Page.Resources>
<converter:EnumTypeConverter x:Key="Converter" TypeToDisplay="enums:CustomEnum" />
</Page.Resources>
<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
<TextBlock Text="{Binding Converter={StaticResource Converter}}" />
</Grid>
</Page>
当我 运行 应用程序时,出现以下错误:
Windows.UI.Xaml.Markup.XamlParseException: 'The text associated with
this error code could not be found.
Failed to create a
'UWPSystemTypeConverterTest.Converter.EnumTypeConverter' from the text
'enums:CustomEnum'. [Line: 14 Position: 56]'
如果我将类型为 CustomEnum 的 属性 添加到从未使用过的代码隐藏文件中,则应用程序可以运行。
更改后的代码- 文件:
public sealed partial class MainPage : Page
{
public CustomEnum WithThisPropertyTheAppWorks { get; set; }
public MainPage()
{
InitializeComponent();
this.DataContext = this;
}
}
复制的完整项目在这里:https://github.com/SabotageAndi/UWPSystemTypeConverterTest
我怀疑是 UWP 的优化器导致了这个问题。
真的是这样吗?
如果代码隐藏文件中没有未使用的 属性,我该如何修复错误?
来自 MVP 邮件列表中 MSFT 员工的信息:
此行为是 UWP 的当前限制。
XAML 编译器和运行时不支持 System.Type- 类型的属性。因此没有生成所需的元数据,运行时无法将字符串转换为类型。
但是由于代码隐藏的 public 属性,编译器现在生成所需的元数据。我对周围的工作不太满意,但它比其他解决方案更好(例如字符串 属性 与类型的全名)。
针对 UWP Build 10240,一个可行的解决方法是在实例化转换器之前在页面的静态资源中添加目标枚举的虚拟实例。
<Page.Resources>
<enums:CustomEnum x:Key="WorkAround">CustomEnumValue</enums:CustomEnum>
<converter:EnumTypeConverter x:Key="Converter" TypeToDisplay="enums:CustomEnum" />
</Page.Resources>
我有一个 IValueConverter,它有一个 System.Type 属性 设置在 XAML.
转换器:
internal class EnumTypeConverter : IValueConverter
{
public Type TypeToDisplay { get; set; }
public object Convert(object value, Type targetType, object parameter, string language)
{
return TypeToDisplay?.FullName;
}
public object ConvertBack(object value, Type targetType, object parameter, string language)
{
throw new NotImplementedException();
}
}
XAML:
<Page
x:Class="UWPSystemTypeConverterTest.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:converter="using:UWPSystemTypeConverterTest.Converter"
xmlns:enums="using:UWPSystemTypeConverterTest.Enum"
mc:Ignorable="d">
<Page.Resources>
<converter:EnumTypeConverter x:Key="Converter" TypeToDisplay="enums:CustomEnum" />
</Page.Resources>
<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
<TextBlock Text="{Binding Converter={StaticResource Converter}}" />
</Grid>
</Page>
当我 运行 应用程序时,出现以下错误:
Windows.UI.Xaml.Markup.XamlParseException: 'The text associated with this error code could not be found.
Failed to create a 'UWPSystemTypeConverterTest.Converter.EnumTypeConverter' from the text 'enums:CustomEnum'. [Line: 14 Position: 56]'
如果我将类型为 CustomEnum 的 属性 添加到从未使用过的代码隐藏文件中,则应用程序可以运行。
更改后的代码- 文件:
public sealed partial class MainPage : Page
{
public CustomEnum WithThisPropertyTheAppWorks { get; set; }
public MainPage()
{
InitializeComponent();
this.DataContext = this;
}
}
复制的完整项目在这里:https://github.com/SabotageAndi/UWPSystemTypeConverterTest
我怀疑是 UWP 的优化器导致了这个问题。 真的是这样吗? 如果代码隐藏文件中没有未使用的 属性,我该如何修复错误?
来自 MVP 邮件列表中 MSFT 员工的信息:
此行为是 UWP 的当前限制。
XAML 编译器和运行时不支持 System.Type- 类型的属性。因此没有生成所需的元数据,运行时无法将字符串转换为类型。
但是由于代码隐藏的 public 属性,编译器现在生成所需的元数据。我对周围的工作不太满意,但它比其他解决方案更好(例如字符串 属性 与类型的全名)。
针对 UWP Build 10240,一个可行的解决方法是在实例化转换器之前在页面的静态资源中添加目标枚举的虚拟实例。
<Page.Resources>
<enums:CustomEnum x:Key="WorkAround">CustomEnumValue</enums:CustomEnum>
<converter:EnumTypeConverter x:Key="Converter" TypeToDisplay="enums:CustomEnum" />
</Page.Resources>