UWP GridView Static Resource not found

249 Views Asked by At

I'm working on a UWP project with a GridView which I would want to dynamically populate with different item templates, depending on the data.

MainPage.xaml:

<GridView HorizontalAlignment="Stretch" VerticalAlignment="Stretch"
          Margin="80, 40, 60, 40" BorderThickness="0"
          ItemsSource="{x:Bind Favorites}"
          ItemTemplateSelector="{StaticResource ItemTemplateSelector}">
    <GridView.ItemContainerStyle>
        <Style TargetType="GridViewItem">
            <Setter Property="Margin" Value="0, 0, 0, 32"/>
        </Style>
    </GridView.ItemContainerStyle>
</GridView>

<Page.Resources>
    <local:ItemTemplateSelector x:Key="ItemTemplateSelector"
                                   Template1="{StaticResource Template1}"
                                   Template2="{StaticResource Template2}">

    </local:ItemTemplateSelector>
    <DataTemplate x:Key="Template1" x:DataType="data:Test1" >
        <local:Template1 HorizontalAlignment="Stretch" VerticalAlignment="Stretch"></local:Template1>
    </DataTemplate>
    <DataTemplate x:Key="Template2" x:DataType="data:" >
        <local:Template2 HorizontalAlignment="Stretch" VerticalAlignment="Stretch"></local:Template2>
    </DataTemplate>
</Page.Resources>

ItemTemplateSelector.cs:

public class ItemTemplateSelector: DataTemplateSelector
{
    protected override DataTemplate SelectTemplateCore(object item, DependencyObject container)
    {
        if (item is Test1)
        {
            return Template1;
        }
        else if (item is Test2)
        {
            return Template2;
        }
        else
        {
            return base.SelectTemplateCore(item);
        }
    }

    public DataTemplate Template1 { get; set; }
    public DataTemplate Template2 { get; set; }
}

The issue is that when I try to run the application I get the following error:

Windows.UI.Xaml.Markup.XamlParseException: 'The text associated with this error code could not be found.
Cannot find a Resource with the Name/Key ItemTemplateSelector [Line: 66 Position: 19]'
1

There are 1 best solutions below

4
On BEST ANSWER

You should define the resource before use. Try to define the Template1 and 2 before the ItemTemplateSelector in page resources. And the resource should define before use.

<Page.Resources>
    <DataTemplate x:Key="Template1" >
        <local:Template1 HorizontalAlignment="Stretch" VerticalAlignment="Stretch"></local:Template1>
    </DataTemplate>
    <DataTemplate x:Key="Template2"  >
        <local:Template2 HorizontalAlignment="Stretch" VerticalAlignment="Stretch"></local:Template2>
    </DataTemplate>

    <local:ItemTemplateSelector x:Key="ItemTemplateSelector"
                               Template1="{StaticResource Template1}"
                               Template2="{StaticResource Template2}">

    </local:ItemTemplateSelector>
</Page.Resources>

<Grid>
    <GridView HorizontalAlignment="Stretch" VerticalAlignment="Stretch"
      Margin="80, 40, 60, 40" BorderThickness="0"
      ItemTemplateSelector="{StaticResource ItemTemplateSelector}">
        <GridView.ItemContainerStyle>
            <Style TargetType="GridViewItem">
                <Setter Property="Margin" Value="0, 0, 0, 32"/>
            </Style>
        </GridView.ItemContainerStyle>
    </GridView>
</Grid>

I write the code in https://github.com/lindexi/lindexi_gd/commit/ca49c76909fca81fb6518247a732219bb5f0d9a6