【发布时间】:2015-05-15 23:43:47
【问题描述】:
我遇到的情况是 wpf 应用程序无法获取 StaticResource,而是因 XamlParseException 而失败。但是,如果我改用 DynamicResource,则会找到资源并且不会发生异常。
我正在尝试按照http://projekt202.com/blog/2010/xaml-organization/ 的建议设计和组织 wpf 资源
相应地,我有 2 个项目,一个包含所有资源的 wpf 控件库和一个使用这些资源的主 wpf 项目。这是两个项目的结构
Projects Structure
Wpf_Theme.ControlLibrary
--资源字典
----BaseControlStyles
------ButtonStyle.xaml
------TextBoxStyle.xaml
----画笔
------默认蓝色主题
----ResourceLibrary.xaml
Wpf_Theme.Main
--App.xaml
--MainWindow.xaml
Contents of xaml files
ButtonStyle.xaml
<Style TargetType="{x:Type Button}">
<Setter Property="Background" Value="{StaticResource ControlBackground}"/>
<Setter Property="BorderBrush" Value="{StaticResource BorderColor}"/>
...
</Style>
DefaultBlueTheme.xaml(画笔)
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<SolidColorBrush x:Key="PanelBackground" Color="#C8DCF0"/>
<SolidColorBrush x:Key="BorderColor" Color="#6A8FB5"/>
<SolidColorBrush x:Key="SelectedItemBackground" Color="Wheat"/>
<SolidColorBrush x:Key="TextForeground" Color="Black"/>
<LinearGradientBrush x:Key="ControlBackground" StartPoint="0,0" EndPoint="0,1">
<LinearGradientBrush.GradientStops>
<GradientStop Offset="0" Color="#DBECFD"/>
<GradientStop Offset="0.5" Color="#C7DBEF"/>
<GradientStop Offset="1" Color="#B0CAE5"/>
</LinearGradientBrush.GradientStops>
</LinearGradientBrush>
</ResourceDictionary>
ResourceLibrary.xaml(将所有字典合并到一个文件中以供主项目使用)
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="Brushes/DefaultBlueTheme.xaml"/>
<ResourceDictionary Source="BaseControlStyles/ButtonStyle.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
App.xaml(在主项目中)
<Application x:Class="Wpf_Themes.Main.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>
<ResourceDictionary
Source="/Wpf_Themes.ControlLibrary;component/ResourceDictionaries/ResourceLibrary.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
</Application>
MainWindow.xaml
<Window x:Class="Wpf_Themes.Main.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<StackPanel Width="200" Margin="10">
<Button>Click Me</Button>
</StackPanel>
</Window>
如帖子前面所述,我无法使用 ButtonStyle 中应用的 StaticResource 解析 Button 的样式(背景和边框画笔) .xaml。如果我改用 DynamicResource,则会正确找到画笔并将其应用于按钮。为什么会发生这种行为的任何见解。
编辑:
按照 Mike 的建议,我将 Wpf_Theme.ControlLibrary 项目中的 xaml 文件直接包含到 Main 项目的 App.xaml 中,如下所示
App.xaml
<Application x:Class="Wpf_Themes.Main.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>
<ResourceDictionary Source="/Wpf_Themes.ControlLibrary;component/ResourceDictionaries/Brushes/DefaultBlueTheme.xaml"/>
<ResourceDictionary Source="/Wpf_Themes.ControlLibrary;component/ResourceDictionaries/BaseControlStyles/ButtonStyle.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
</Application>
现在可以使用 StaticResource 正确定位资源
【问题讨论】:
-
您是否有任何特殊原因需要使用您的 ResourceLibrary.xaml 文件?你能不能只合并 App.xaml 中的所有字典?
-
其实我是在关注前面提到的那个网站。我认为它在那里是为了避免包含 xaml 的程序集 uri 语法。而是使用 esourceLibrary.xaml 嵌入单个文件。有趣的是,如果我避免使用 ResourceLibrary.xaml 而是将所有字典直接嵌入到 App.xaml 中,它就可以正常工作并且资源也可以正确定位。不知道为什么!