I created a test solutions with 2 WPF assemblies.
In "AssemblyTest", I have a simple default window, "OtherWindow", that does nothing. In "StartingAssembly, I have also have a simple default window, "MainWindow" that initializes and Show() the OtherWindow from AssemblyTest.
All references are correct and in this case everything works fine; both windows show up correctly the way they are expected to.
using AssemblyTest;
namespace StartingAssembly
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
OtherWindow window = new OtherWindow();
public MainWindow()
{
InitializeComponent();
window.Show();
}
}
}
However, an issue occurs when I change "OtherWindow" to use a StaticResource.
This works correctly:
<Window x:Class="AssemblyTest.OtherWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="OtherWindow" Height="350" Width="525" Background="Blue">
<Grid>
</Grid>
</Window>
However, this causes a XamlParseException:
<Window x:Class="AssemblyTest.OtherWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="OtherWindow" Height="350" Width="525" Background="{StaticResource mainLoginBackground}">
<Grid>
</Grid>
</Window>
The resource is located at CommonResourceDictionary.xaml
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:sys="clr-namespace:System;assembly=mscorlib">
<RadialGradientBrush x:Key="mainLoginBackground" GradientOrigin="0.5,0.5" Center="0.5,0.5" RadiusX="1" RadiusY="1">
<GradientStopCollection>
<GradientStop Color="#ff0f75ba" Offset="0" />
<GradientStop Color="#ff033657" Offset="1" />
</GradientStopCollection>
</RadialGradientBrush>
</ResourceDictionary>
This is App.xaml from AssemblyTest
<Application x:Class="AssemblyTest.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
StartupUri="OtherWindow.xaml">
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="/Resources/CommonResourceDictionary.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
</Application>
Finally, Here is the error:
I wrote this question using an example that is as basic as possible, so that I can apply to a bigger project. If you can help me figure what is missing, I will be able to use this solution on a bigger project I'm working with. In addition, I have searched StackOverflow for similar questions, but none close to this specific issue.
Since the ResourceDictionary
is defined in another assembly you should use a pack URI to reference it:
<ResourceDictionary Source="pack://application:,,,/AssemblyTest;component/Resources/CommonResourceDictionary.xaml" />
You can read more about this here: https://msdn.microsoft.com/en-us/library/aa970069(v=vs.110).aspx
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.