单独文件夹中的 ListView DataTemplate
ListView DataTemplate in separate folder
我正在尝试学习通用 Windows 平台的应用程序编程。我目前正在使用 ListView
并在 <DataTemplate>
中定义其布局,但代码一团糟。有没有办法在单独的文件夹中定义 <DataTemplate>
?我搜索了网络,但找不到解决方案。你能帮我吗?谢谢
我总是建议为这种事情创建一个 ResourceDictionary。这是一个示例设置:
创建文件夹资源 > 添加 > 新项目 > 资源字典 "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:我实际上也建议对样式和其他应用程序范围内的资源(如字体大小、画笔、背景等)做同样的事情
在datatemplate.xaml中定义数据模板:
<ResourceDictionary
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<DataTemplate>
</DataTemplate>
</ResourceDictionary>
在用户控件中引用数据模板:
<UserControl
x:Class="<assemblyName>.Themes.MyUserControl1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:PerpetuumSoft.DataManager.UniApp.UI.Themes"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
d:DesignHeight="300"
d:DesignWidth="400">
<UserControl.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="ms-appx:///<AssemblyName>/Themes/DataTemplate.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</UserControl.Resources>
<Grid>
</Grid>
</UserControl>
我正在尝试学习通用 Windows 平台的应用程序编程。我目前正在使用 ListView
并在 <DataTemplate>
中定义其布局,但代码一团糟。有没有办法在单独的文件夹中定义 <DataTemplate>
?我搜索了网络,但找不到解决方案。你能帮我吗?谢谢
我总是建议为这种事情创建一个 ResourceDictionary。这是一个示例设置:
创建文件夹资源 > 添加 > 新项目 > 资源字典 "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:我实际上也建议对样式和其他应用程序范围内的资源(如字体大小、画笔、背景等)做同样的事情
在datatemplate.xaml中定义数据模板:
<ResourceDictionary
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<DataTemplate>
</DataTemplate>
</ResourceDictionary>
在用户控件中引用数据模板:
<UserControl
x:Class="<assemblyName>.Themes.MyUserControl1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:PerpetuumSoft.DataManager.UniApp.UI.Themes"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
d:DesignHeight="300"
d:DesignWidth="400">
<UserControl.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="ms-appx:///<AssemblyName>/Themes/DataTemplate.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</UserControl.Resources>
<Grid>
</Grid>
</UserControl>