跨多个 Xaml 文件拆分 WPF 接口

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/224765/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 19:43:45  来源:igfitidea点击:

Splitting WPF interface across multiple Xaml files

wpfxaml

提问by Dave Turvey

I am trying to create a user interface using XAML. However, the file is quickly becoming very large and difficult to work with. What is the best way for splitting it across several files.

我正在尝试使用 XAML 创建用户界面。但是,该文件很快变得非常大且难以处理。将其拆分为多个文件的最佳方法是什么。

I would like to be able to set the content of an element such as a ComboBox to an element that is defined in a different xaml file (but in the same VS project).

我希望能够将诸如 ComboBox 之类的元素的内容设置为在不同 xaml 文件(但在同一个 VS 项目中)中定义的元素。

thanks

谢谢

采纳答案by stusmith

You can split a large user interface by defining UserControls.

您可以通过定义 UserControl 来拆分大型用户界面。

Right-click on the solution tree, choose Add->New Item... then User Control. You can design this in the normal way.

右键单击解决方案树,选择 Add->New Item... 然后选择 User Control。您可以按照正常方式进行设计。

You can then reference your usercontrol in XAML using a namespace declaration. Let's say you want to include your UserControl in a Window. In the following example I've added a UserControl named "Foo" to the namespace "YourCompany.Controls":

然后,您可以使用命名空间声明在 XAML 中引用您的用户控件。假设您想将 UserControl 包含在 Window 中。在以下示例中,我将名为“Foo”的 UserControl 添加到命名空间“YourCompany.Controls”:

<Window x:Class="YourCompany.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:Controls="clr-namespace:YourCompany.Controls">

  <Controls:Foo ... />

For your specific example, you would make use of your usercontrol in a combobox by defining a DataTemplate that displayed the data within your usercontrol.

对于您的特定示例,您将通过定义显示用户控件中数据的 DataTemplate 来在组合框中使用用户控件。

回答by EFrank

You can split up XAML files by using a ResourceDictionary. The ResourceDictionary can be used to merge other files:

您可以使用ResourceDictionary拆分 XAML 文件。ResourceDictionary 可用于合并其他文件:

<Page.Resources>
  <ResourceDictionary>
    <ResourceDictionary.MergedDictionaries>
      <ResourceDictionary Source="myresourcedictionary.xaml"/>
      <ResourceDictionary Source="myresourcedictionary2.xaml"/>
    </ResourceDictionary.MergedDictionaries>
  </ResourceDictionary>
</Page.Resources>

In the ResourceDictionary, you can also declare Styles that you can use at your elements, such that the main XAML file gets smaller.

在 ResourceDictionary 中,您还可以声明可以在元素上使用的样式,以便主 XAML 文件变小。

Another possibility to get a smaller XAML file is to define your own controls that you then use in your main app.

获得较小 XAML 文件的另一种可能性是定义您自己的控件,然后在主应用程序中使用这些控件。

回答by Mike de Klerk

You can also create a Page, instead of a UserControl. A Pagecan be hosted by the Windowor by a Frame. Search for the pros and cons of a Page vs UserControl. It depends a bit on your requirements with respect to navigation which will suit your needs best.

您还可以创建一个Page,而不是一个UserControl. APage可以由Window或 由Frame托管。搜索 Page 与 UserControl 的优缺点。这在一定程度上取决于您对导航的要求,这将最适合您的需求。

Here is an example of using a Page in a Frame.

这是在框架中使用页面的示例。

回答by Andrey Neverov

Use styles and user controls. Divide your interface on smaller parts and code them in another xaml files. Example:

使用样式和用户控件。将您的界面分成更小的部分并将它们编码到另一个 xaml 文件中。例子:

<Window>
<VeryBigControl>
<VeryBigControl.Style>
... <!--very long style-->
</VeryBigControl.Style>
.. <!--content of very big control-->
</VeryBigControl
</Window>

<Window>
<VeryBigControl>
<VeryBigControl.Style>
... <!--very long style-->
</VeryBigControl.Style>
.. <!--content of very big control-->
</VeryBigControl
</Window>

divide it into three xaml files:
Window.xaml - this will be Window
VeryBigControl.xaml - this will be UserControl
VeryBigControlStyle.xaml - this will be resource dictionary
and so on :)

将它分成三个 xaml 文件:
Window.xaml - 这将是 Window VeryBigControl.xaml - 这将
是 UserControl
VeryBigControlStyle.xaml - 这将是资源字典
等等:)