单独文件夹中的 ListView DataTemplate

mis*_*hut 3 xaml windows-10 uwp

我正在尝试学习通用 Windows 平台的编程应用程序。我目前正在使用ListView,我在 中定义了它的布局<DataTemplate>,但代码是一团糟。有没有办法<DataTemplate>在单独的文件夹中定义?我搜索了网络,但我无法找到解决方案。你能帮我解决这个问题吗?谢谢。

Fer*_*hen 5

我总是建议为这种事情创建一个 ResourceDictionary。这是一个示例设置:

创建一个文件夹 Resources > Add > New item > Resource Dictionary "Templates.xaml"

在你的 App.xaml 添加

<Application.Resources>
    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="Resources/Templates.xaml"/>
        </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>
</Application.Resources>
Run Code Online (Sandbox Code Playgroud)

在 Templates.xaml 中,您可以添加任何您想要的模板,如下所示:

<ResourceDictionary
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:thestory.Resources">


<DataTemplate x:Key="ListItemTemplate">

</DataTemplate>
Run Code Online (Sandbox Code Playgroud)

您现在可以使用 {StaticResource ListItemTemplate} 在任何需要的地方引用此模板

祝你好运!

PS:我实际上还建议对样式和其他应用程序范围的资源(如字体大小、画笔、背景等)执行相同的操作。