3

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?

Andreas Willich
  • 5,665
  • 3
  • 15
  • 22
  • 1
    I've run your sample code and confirmed your findings! Unfortunately, this sounds a lot like a bug on the UWP platform... – Pedro Lamas Jul 05 '17 at 17:56
  • Are you just trying to show FullName of your Enum's in TextBlock? If yes, try [this](https://stackoverflow.com/a/39210611/4585476) – AVK Jul 05 '17 at 21:00
  • @AVK: No, this is only a small repo for the issue. We are generating via Converter an ItemsSource for a ListBox. We know we can use different work arounds to achieve the requirements, but we would want to know, what goes wrong here, in case we have the problem in the future again. – Andreas Willich Jul 05 '17 at 21:06
  • Would you consider `x:Bind`? This sounds like a bug to me. – Justin XL Jul 05 '17 at 21:52
  • @JustinXL x:Bind is sadly no option for us, as we have german umlauts in our binding path which don't work in the Windows 10 SDK we have to target. – Andreas Willich Jul 06 '17 at 07:00
  • 2
    I think the neatest workaround is to define all the enums in `App.xaml`. Like ` Value1` At least you don't need to spread properties across different pages. You could also try reporting it to MSFT. – Justin XL Jul 06 '17 at 12:39

2 Answers2

4

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>
2

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).

Andreas Willich
  • 5,665
  • 3
  • 15
  • 22