是否可以使用 XAML 文件中的 XamlReader 加载 XAML 文本块?
我在许多控件中使用以下 DataTemplate:
<pages:BasePageManageItems x:Class="TestApp.Pages.PageManageAddresses"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:pages="clr-namespace:TestHistorierung.Pages"
xmlns:tk="http://schemas.microsoft.com/wpf/2008/toolkit"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Background="#eee"
VerticalAlignment="Stretch">
<pages:BasePageManageItems.Resources>
<DataTemplate x:Key="manageAreaCellTemplate">
<Border Padding="2">
<StackPanel Orientation="Horizontal">
<TextBlock Style="{DynamicResource ManageLinkStyle}"
Tag="{Binding Id}" Text="Delete" MouseDown="System_Delete_Click"
Margin="0 0 5 0"/>
<TextBlock Style="{DynamicResource ManageLinkStyle}"
Tag="{Binding Id}" Text="Edit" MouseDown="System_Edit_Click"
Margin="0 0 5 0"/>
<TextBlock Style="{DynamicResource ManageLinkStyle}"
Tag="{Binding Id}" Text="Add" MouseDown="System_Add_Click"
Margin="0 0 5 0"/>
<TextBlock Style="{DynamicResource ManageLinkStyle}"
Tag="{Binding Id}" Text="Copy" MouseDown="System_Copy_Click"
Margin="0 0 5 0"/>
</StackPanel>
</Border>
</DataTemplate>
</pages:BasePageManageItems.Resources>
有没有办法从 XAML 使用 XamlReader,以便我可以简单地将 DataTemplate 的文本加载到 XAML 文件中动态?我正在想象这样的事情(伪代码):
<pages:BasePageManageItems x:Class="TestApp.Pages.PageManageAddresses"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:pages="clr-namespace:TestHistorierung.Pages"
xmlns:tk="http://schemas.microsoft.com/wpf/2008/toolkit"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Background="#eee"
VerticalAlignment="Stretch">
<pages:BasePageManageItems.Resources>
<XamlReader Load="XamlBlocks/DateTemplateManageButtons.xaml"/>
</pages:BasePageManageItems.Resources>
I use the following DataTemplate in many controls:
<pages:BasePageManageItems x:Class="TestApp.Pages.PageManageAddresses"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:pages="clr-namespace:TestHistorierung.Pages"
xmlns:tk="http://schemas.microsoft.com/wpf/2008/toolkit"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Background="#eee"
VerticalAlignment="Stretch">
<pages:BasePageManageItems.Resources>
<DataTemplate x:Key="manageAreaCellTemplate">
<Border Padding="2">
<StackPanel Orientation="Horizontal">
<TextBlock Style="{DynamicResource ManageLinkStyle}"
Tag="{Binding Id}" Text="Delete" MouseDown="System_Delete_Click"
Margin="0 0 5 0"/>
<TextBlock Style="{DynamicResource ManageLinkStyle}"
Tag="{Binding Id}" Text="Edit" MouseDown="System_Edit_Click"
Margin="0 0 5 0"/>
<TextBlock Style="{DynamicResource ManageLinkStyle}"
Tag="{Binding Id}" Text="Add" MouseDown="System_Add_Click"
Margin="0 0 5 0"/>
<TextBlock Style="{DynamicResource ManageLinkStyle}"
Tag="{Binding Id}" Text="Copy" MouseDown="System_Copy_Click"
Margin="0 0 5 0"/>
</StackPanel>
</Border>
</DataTemplate>
</pages:BasePageManageItems.Resources>
Is there any way to use XamlReader from XAML so that I can simply load the text of the DataTemplate into the XAML file dynamically? I'm imagining something like this (pseudo code):
<pages:BasePageManageItems x:Class="TestApp.Pages.PageManageAddresses"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:pages="clr-namespace:TestHistorierung.Pages"
xmlns:tk="http://schemas.microsoft.com/wpf/2008/toolkit"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Background="#eee"
VerticalAlignment="Stretch">
<pages:BasePageManageItems.Resources>
<XamlReader Load="XamlBlocks/DateTemplateManageButtons.xaml"/>
</pages:BasePageManageItems.Resources>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您不应该将 XamlReader 标记放置在 Xaml 中(我什至不知道是否可能)。相反,您可以使用 XamlReader 类在代码中创建编译的 Xaml,并将其附加到父元素:
You shouldn't place the XamlReader tag in Xaml (I don't even know if it's possible). Instead you can use XamlReader class to create compiled Xaml in code, and attach it to parent element:
您可以将常见的 XAML 放入 ResourceDictionary 中:
XamlBlocks/DateTemplateManageButtons.xaml (添加到项目中,构建操作 = 页面)
XamlBlocks/DateTemplateManageButtons.xaml.cs :
在您的页面中:
如果您需要事件处理程序代码在页面而不是资源字典,您可以执行以下操作:
定义事件接口:
在使用数据模板的所有页面中实现该接口
在资源字典 cs 文件中:
You can put the common XAML in a ResourceDictionary:
XamlBlocks/DateTemplateManageButtons.xaml (added to the project, build action = Page)
XamlBlocks/DateTemplateManageButtons.xaml.cs :
And in your page:
If you need the event handler code to run in the page and not the resource dictionary you can do something like:
Define interface for the events:
Implement that interface in all pages that use the data template
In the resource dictionary cs file: