0

我正在开发一个带有 GridView 的 UWP 项目,我希望根据数据动态填充不同的项目模板。

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; }
}

问题是,当我尝试运行应用程序时,出现以下错误:

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

1 回答 1

2

您应该在使用前定义资源。尝试在页面资源中的 ItemTemplateSelector 之前定义 Template1 和 2。并且资源应该在使用前定义。

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

我在https://github.com/lindexi/lindexi_gd/commit/ca49c76909fca81fb6518247a732219bb5f0d9a6中编写代码

于 2019-11-27T23:56:23.917 回答