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

Viewed 238

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?

2 Answers
Related