I am trying to build my application for UWP and am currently stuck in design exceptions while trying to use a DataTemplate with x: Bind in the resource dictionary.
I created the resource dictionary "ItemTemplates.xaml" with the appropriate code (to ensure initialization of x: Bind). The file contains only one template:
<DataTemplate x:Key="HomeViewCategoryListItemTemplate" x:DataType="models:Category"> <Button Background="#88333333" Height="110" VerticalContentAlignment="Top" Padding="10" HorizontalAlignment="Stretch"> <Grid> <Grid.RowDefinitions> <RowDefinition Height="Auto" /> <RowDefinition Height="Auto" /> </Grid.RowDefinitions> <TextBlock FontWeight="Light" HorizontalAlignment="Center" Text="{x:Bind Name}" FontSize="{ThemeResource TextStyleExtraLargeFontSize}" /> <TextBlock Foreground="{ThemeResource ToolTipForegroundThemeBrush}" HorizontalAlignment="Center" Margin="0,10,0,0" Text="{x:Bind Description}" Grid.Row="1" TextAlignment="Center" TextWrapping="Wrap" /> </Grid> </Button> </DataTemplate>
Then I added this resource dictionary to App.xaml as follows:
<Application.Resources> <ResourceDictionary> <ResourceDictionary.MergedDictionaries> <ResourceDictionary Source="ms-appx:///Resources/Core.xaml" /> <resources:ItemTemplates /> </ResourceDictionary.MergedDictionaries> </ResourceDictionary> </Application.Resources>
Now the project is unusable because the designer throws strange exceptions, but when I clean and rebuild the project and go to the HomeView.xaml page, the designer shows only the default objects "ToString ()" (basically, the list contains only three times the text " Models.Categories ") in the ListView and the ItemTemplate property of my ListView is underlined and shows the following error:
The resource "HomeViewCategoryListItemTemplate" could not be resolved.
When I go to App.xaml, I see another underline there (the line <resources:ItemTemplates /> ), which reads:
The property 'DataType' was not found in type 'DataTemplate'.
Both errors are not sensitive, because when I actually launch the application, there are no problems, and everything works fine. The only workaround I have found so far is to enable the ResourceDictionary twice both in the classic way and in the compiled way:
<ResourceDictionary Source="ItemTemplates.xaml" /> <resoures:ItemTemplates />
This solution works, and then everything works both at development time and at runtime, but I really think this is pretty messy and there should be a safer approach or am I missing something trivial.
I am running Visual Studio 2015 Update 1 and installing the latest SDK for UWP. The goal of the project is to create 10240.
Edit: Another exception that a designer very often throws and completely crashes:
Unable to cast object of type 'System.String' to type 'Models.Data.Categories.Category'.
According to the output of StackTrace, this happens inside the ItemTemplates.xaml.cs code - in particular, the generated ProcessBindings method. Again, the project is still compiling and working fine, but the designer is not even trying to show the result.