将 XAML 文件合并到 Xaml 代码中
Merge XAML file into Xaml code
我想将一个仅包含样式的 XAML 文件合并到我的应用程序的 Window.Resource 中。
我尝试按照中所述使用 MergedDictionaries
WPF Reference custom resource defined in another xaml file .
首先:
该文件名为 "MyStyleDocument.xaml",包含不同的 WPF 样式,但不使用 x 键。
<ResourceDictionary
xmlns=" http://schemas.microsoft.com/winfx/2006/xaml/presentation "
xmlns:x=" http://schemas.microsoft.com/winfx/2006/xaml ">
<Style TargetType="{x:Type TabControl}">
(...)
</Style>
<Style TargetType="{x:Type XY}">
(...)
</Style>
.
.
.
</ResourceDictionary>
第二个:
<Window.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="MyStyleDocument.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Window.Resources>
我得到结果:“查找资源字典 "MyStyleDocument.xaml" 时出错。”该文件位于我的项目文件夹中。
我的问题是:
我怎样才能巧妙地将一个包含不同样式的 XAML 文件包含到另一个 XAML 代码中?
我的项目结构是:
WPFApplication(文件夹1) -> 文件夹1包括WPFApplication(文件夹2);WPFApplication.sln; WPFApplication.suo; WPFApplication.v11.suo
文件夹2包括:bin(文件夹2.1); obj(文件夹2.2);属性(filefolder2.3); App.xaml; App.xaml.cs; Application.ico; MainWindow.xaml.cs; MyStyleDocument.xaml; WpfApplication.csproj
这是一个演示 wpf 项目:
-WpfApplication
|--App.xaml
|--MyStyle[here is a floder]
|---MyStyleDocument1.xaml
|---MyStyleDocument2.xaml
|---MyStyleDocument3.xaml
然后你可以这样编辑App.xaml:
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<!--add style file here like this-->
<ResourceDictionary Source="/MyStyle/MyStyleDocument1.xaml" />
</ResourceDictionary.MergedDictionaries>
<ResourceDictionary>
<Application.Resources>
我想将一个仅包含样式的 XAML 文件合并到我的应用程序的 Window.Resource 中。 我尝试按照中所述使用 MergedDictionaries WPF Reference custom resource defined in another xaml file .
首先: 该文件名为 "MyStyleDocument.xaml",包含不同的 WPF 样式,但不使用 x 键。
<ResourceDictionary
xmlns=" http://schemas.microsoft.com/winfx/2006/xaml/presentation "
xmlns:x=" http://schemas.microsoft.com/winfx/2006/xaml ">
<Style TargetType="{x:Type TabControl}">
(...)
</Style>
<Style TargetType="{x:Type XY}">
(...)
</Style>
.
.
.
</ResourceDictionary>
第二个:
<Window.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="MyStyleDocument.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Window.Resources>
我得到结果:“查找资源字典 "MyStyleDocument.xaml" 时出错。”该文件位于我的项目文件夹中。
我的问题是: 我怎样才能巧妙地将一个包含不同样式的 XAML 文件包含到另一个 XAML 代码中?
我的项目结构是: WPFApplication(文件夹1) -> 文件夹1包括WPFApplication(文件夹2);WPFApplication.sln; WPFApplication.suo; WPFApplication.v11.suo 文件夹2包括:bin(文件夹2.1); obj(文件夹2.2);属性(filefolder2.3); App.xaml; App.xaml.cs; Application.ico; MainWindow.xaml.cs; MyStyleDocument.xaml; WpfApplication.csproj
这是一个演示 wpf 项目:
-WpfApplication
|--App.xaml
|--MyStyle[here is a floder]
|---MyStyleDocument1.xaml
|---MyStyleDocument2.xaml
|---MyStyleDocument3.xaml
然后你可以这样编辑App.xaml:
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<!--add style file here like this-->
<ResourceDictionary Source="/MyStyle/MyStyleDocument1.xaml" />
</ResourceDictionary.MergedDictionaries>
<ResourceDictionary>
<Application.Resources>