我正在尝试学习通用 Windows 平台的编程应用程序。我目前正在与 ListView
合作我在 <DataTemplate>
中定义了它的布局,但代码一团糟。有没有办法定义 <DataTemplate>
在单独的文件夹中?我搜索了网络,但我无法找到解决方案。你能帮我解决这个问题吗?谢谢。
最佳答案
我总是建议为这种事情创建一个 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>
在 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>
您现在可以使用 {StaticResource ListItemTemplate} 在任何需要的地方引用此模板
祝你好运!
PS:我实际上还建议对样式和其他应用程序范围的资源(如字体大小、画笔、背景等)执行相同的操作。
关于xaml - 单独文件夹中的 ListView DataTemplate,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33754932/