我创建了一个新的WPF项目。创建项目后,我将项目属性更改为 outputtype = classlibrary 。
这是我的文件结构:
CustomWindow.xaml
是一个ResourceDictionary,用于描述CustomWindow.cs
的外观。
文件CustomWindow.xaml
看起来像
<ResourceDictionary x:Class="WpfApplication1.CustomWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:options="http://schemas.microsoft.com/winfx/2006/xaml/presentation/options"
xmlns:sys="clr-namespace:System;assembly=mscorlib">
<Style x:Key="MainWindow" TargetType="{x:Type Window}">
<Setter Property="Template">
<!-- ... -->
</Setter>
</Style>
</ResourceDictionary>
班级CustomWindow.xaml
看起来像
namespace WpfApplication1
{
public partial class CustomWindow : ResourceDictionary
{
public CustomWindow()
{
InitializeComponent();
}
}
}
现在我已添加到App.xaml
以下
<Application x:Class="WpfApplication1.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<!-- Common base theme -->
<ResourceDictionary Source="pack://application:,,,/WpfApplication1;component/CustomWindow.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
</Application>
我将文件CustomWindow.xaml
的属性构建过程更改为Page
。
之后我构建了lib。现在我想将样式MainWindow
用于我的新项目的主窗口。我已将lib添加到项目引用中。并且App.xaml
我添加了一些代码:
<Application x:Class="WpfApplication2.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
StartupUri="MainWindow.xaml">
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<!-- Common base theme -->
<ResourceDictionary Source="pack://application:,,,/WpfApplication1;component/CustomWindow.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
</Application>
我试图建立它,但我变成了两个错误。 第一个
无法找到资源“MainWindow”。
第二个
查找资源字典“pack:// application:,,, / WpfApplication1; component / CustomWindow.xaml”时发生错误。
也许有人有个主意。
答案 0 :(得分:2)
修改WPF应用程序项目中的App.xaml文件:
<Application x:Class="WpfApplication2.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:WpfApplication1"
StartupUri="Window9.xaml">
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<!-- Common base theme -->
<ResourceDictionary Source="pack://application:,,,/WpfControlLibrary1;component/CustomWindow.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
</Application>
&#34; WpfControlLibrary1&#34;是上面示例标记中的WPF用户控件库的名称和&#34; CustomWindow&#34;是此项目中资源字典的名称。
在WPF应用程序中设置MainWindow的Style属性,如下所示:
<Window x:Class="WpfApplication2.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:WpfApplication2"
mc:Ignorable="d"
Title="MainWindow" Height="350" Width="525" Style="{StaticResource MainWindow}">
<Grid>
</Grid>
</Window>