将一个大 XAML 拆分为多个 Sub-XAML 文件 [英] Split one big XAML in number of Sub-XAML files
问题描述
在我的基于 WPF4 桌面的应用程序中,有一个带有侧边栏菜单的大块,在每个窗口中重复出现并占用大约 70 行 XAML.为了提高代码重用,我想将 XAML 文件拆分为两个文件:
In my WPF4 Desktop-based application there is a big block with sidebar menu that repeats in each window and takes about 70 lines of XAML. In order to improve code reuse, I would like to split XAML file in two files:
- 包含侧边栏菜单代码的 XAML 文件(约 70 行)
- 基本 XAML 文件,其中包含对带有侧边栏菜单代码的 XAML 文件的包含/引用"
据我了解,有两种方法可以解决我的问题:
As I understood, there are two ways to implement my problem:
- 使用
ResourceDictionary
- 使用
UserControl
/CustomControl
我的问题是:
ResourceDictionary
和UserControl
有什么区别?你能给我举个例子,我必须使用UserControl
和ResourceDictionary
吗?
What is the difference between
ResourceDictionary
andUserControl
? Could you give me examples where I have to useUserControl
and whereResourceDictionary
?
您能否给出一个完整的代码示例,如何将一个 XAML 文件的内容包含/导入到另一个文件中?
Could you give a full code example how to include/import content of one XAML-file to other?
附:这是我要导出到单独的 XAML 文件的代码示例:
P.S. Here is an example of code that I want to export to separate XAML-file:
<Border Style = "{StaticResource Body_SideBarMenu_Border_Settings}">
<StackPanel Style = "{StaticResource Body_SideBarMenu}">
<TextBlock Style = "{StaticResource Body_SideBarMenu_Title}"
Text = "{x:Static res:Resources.WinApp_SideBarMenu_Title}" />
<TextBlock x:Name = "SideBar_WinReports"
Style = "{StaticResource Body_SideBarMenu_Item}"
Text = "{x:Static res:Resources.DashListMarker}">
<Hyperlink KeyboardNavigation.TabIndex = "12"
Style = "{StaticResource Body_SideBarMenu_Item_Hyperlink}"
Click = "Call_WinReports_Click">
<TextBlock Text = "{x:Static res:Resources.WinApp_ModuleName_Reports}" />
</Hyperlink>
</TextBlock>
</StackPanel>
</Border>
推荐答案
ResourceDictionary 只是您的样式/模板等的容器.因此您确实可以选择使用样式(并通过 ResourceDictionary 引用它)或 UserControl.
ResourceDictionary is just a container for your styles/templates etc. So you really have a choice between using a style (and referencing it through a ResourceDictionary) or a UserControl.
为了区分这两者,问自己一个问题:您是在实现一些现有控件的另一种外观,还是在实现一些真正新的东西,而不仅仅是一个 ListView(或一个边框,或一个 ComboBox等等.)?在前一种情况下,使用样式;在后者中,创建一个新的 UserControl.
In order to differentiate between the two, ask yourself a question: are you implementing just another look for some existing control, or you are implementing something really new, which is more than just a ListView (or a Border, or a ComboBox etc.)? In the former case, use a style; in the latter, create a new UserControl.
特别是针对您的情况,我会选择 UserControl.
Specifically for your case, I would go for a UserControl.
代码示例(虽然不完整)
(请注意,以下代码的模板可以用 VS 的添加新用户控件"插入)
(Please note that a template for the following code can be inserted with VS's "add new UserControl")
Xaml:
<UserControl x:Class="SomeNamespace.SidebarMenu"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<UserControl.Resources> <!-- you can define local styles here -->
<Style x:Key="SidebarMenuTextblock" TargetType=TextBlock>
...
</Style>
</UserControl.Resources>
<Border Background=...>
<StackPanel>
<TextBlock
x:Name="Put_a_name_if_you_want_to_reference_this_item_in_code_behind"
Style="{StaticResource SidebarMenuTextblock}"
Text="{x:Static res:Resources.WinApp_SideBarMenu_Title}" />
...
</StackPanel>
</Border>
</UserControl>
.cs:
using System;
using System.Windows;
using System.Windows.Controls;
namespace SomeNamespace
{
public partial class SidebarMenu : UserControl
{
public NumericUpDown()
{
InitializeComponent();
}
...
// define here your properties etc,
}
}
现在,您可以像这样使用控件了:
Now, you can use the control like that:
<Window
x:Class="SomeOtherNamespace.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:controls="clr-namespace:SomeNamespace">
<Grid>
<controls:SidebarMenu PropertyIfYouDefinedOne="SomeValue"/>
...
</Grid>
</Window>
这篇关于将一个大 XAML 拆分为多个 Sub-XAML 文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!