Reputation: 1055
I have created a new WPF-Project. After creating the project, I changed the project properties to outputtype = classlibrary.
Here is my file structure:
The CustomWindow.xaml
is a ResourceDictionary which discribes the appearance of the CustomWindow.cs
.
The file CustomWindow.xaml
looks like
<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>
The class CustomWindow.xaml
looks like
namespace WpfApplication1
{
public partial class CustomWindow : ResourceDictionary
{
public CustomWindow()
{
InitializeComponent();
}
}
}
Now I added to the App.xaml
following
<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>
I changed the property buildprocess of the file CustomWindow.xaml
to Page
.
After that I build the lib. Now I want to use the style MainWindow
for my new project's main window. I've added the lib to the project references. And to the App.xaml
I added some lines of code:
<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>
I tried to build it, but I become two error's. The first
The resource "MainWindow" couldn't be find.
The second
An error occurred while finding the resource dictionary "pack://application:,,,/WpfApplication1;component/CustomWindow.xaml".
Maybe someone of you got an idea.
Upvotes: 3
Views: 5288
Reputation: 169340
Modify the App.xaml file in the WPF Application project:
<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>
"WpfControlLibrary1" is the name of the WPF User Control Library in the sample markup above and "CustomWindow" is the name of the resource dictionary in this project.
Set the Style property of your MainWindow in the WPF Application like this:
<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>
Upvotes: 4