Andreas Willich
Andreas Willich

Reputation: 5835

System.Type as property of converter - only works with unused property in code behind

I have a IValueConverter that has a System.Type property which is set in XAML.

Converter:

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>

When I run the application, I get following error:

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]'

If I add a property of type CustomEnum to the code- behind file, which is never used, the application works.

the changed code- behind- File:

public sealed partial class MainPage : Page
{
        public CustomEnum WithThisPropertyTheAppWorks { get; set; }

        public MainPage()
        {
            InitializeComponent();
            this.DataContext = this;
        }
}

The complete project for reproduction is here: https://github.com/SabotageAndi/UWPSystemTypeConverterTest

Line to uncomment is https://github.com/SabotageAndi/UWPSystemTypeConverterTest/blob/master/UWPSystemTypeConverterTest/MainPage.xaml.cs#L13

I suspect that an optimiser of UWP is causing this problem. Is this really the case? How can I fix the error without the unused property in the code-behind file?

Upvotes: 3

Views: 267

Answers (2)

Targeting UWP Build 10240, a viable work around is to add a dummy instance of the targeted enum in static resources of the page before instantiating the converter.

 <Page.Resources>
    <enums:CustomEnum x:Key="WorkAround">CustomEnumValue</enums:CustomEnum>
    <converter:EnumTypeConverter x:Key="Converter" TypeToDisplay="enums:CustomEnum" />
</Page.Resources>

Upvotes: 4

Andreas Willich
Andreas Willich

Reputation: 5835

Info from a MSFT employee on a MVP mailing list:

This behaviour is a current limitation of UWP.

The XAML compiler and the runtime don't support System.Type- typed properties. So the needed metadata is not generated and the runtime can not convert the string to the type.

But because of the public properties on the code-behind, the compiler generates the needed metadata now. I am not that happy with the work around, but it is better than other solutions (e.g. a string property with the fullname to the type).

Upvotes: 2

Related Questions