我使用2個WPF程序集創建了一個測試解決方案。StaticResource在從另一個程序集加載時導致XamlParseException
在 「AssemblyTest」,我有一個簡單的默認窗口, 「OtherWindow」,什麼也不做。 在「StartingAssembly中,我也有一個簡單的默認窗口」MainWindow「,用於初始化並顯示AssemblyTest中的OtherWindow。
所有引用都是正確的,在這種情況下,一切正常;兩個窗口都正確顯示這樣,他們預計
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();
}
}
}
但是,當我改變 「OtherWindow」 使用靜態資源時出現的問題
這正常工作:。
<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>
然而,這會導致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>
資源位於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>
這是App.xaml中從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>
最後,這裏是錯誤:
我用一個儘可能基本的例子來寫這個問題,這樣我就可以應用到一個更大的項目中。如果你能幫助我找出缺失的東西,我將能夠在我正在處理的更大的項目中使用此解決方案。另外,我已經在StackOverflow中搜索了類似的問題,但都沒有接近這個特定的問題。
你嘗試'DynamicResource'而不是' StaticResource'? – Safe
是的,我做了,它編譯和運行,但風格被忽略。 – Rafael
關於我正在處理的更大項目* – Rafael