【发布时间】:2011-05-18 13:17:30
【问题描述】:
我有一个 ResourceDictionary Generic.xaml,它由一组其他资源字典组成,这是通过合并字典来实现的。如果在我的 Generic.xaml 中没有定义合并字典以外的资源,则在运行时不会应用任何样式。 但是,如果我在 Gerneric.xaml 中定义任何样式,所有样式都将开始正常工作。
所以下面的例子不起作用(这是来自我的 Generic.xaml)
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="pack://application:,,,/Resources;component/ResourceDictionaries/CommonControls/CommonControlsResources.xaml"/>
</ResourceDictionary.MergedDictionaries>
<!--<Style TargetType="{x:Type ToolTip}"/>-->
这是工作版本:
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="pack://application:,,,/Resources;component/ResourceDictionaries/CommonControls/CommonControlsResources.xaml"/>
</ResourceDictionary.MergedDictionaries>
<Style TargetType="{x:Type ToolTip}"/>
为什么会这样?
编辑:
为了查明问题,我创建了一个示例解决方案。所以项目的结构如下:
ProjectA WPF Windows 应用程序
ProjectC 类库,包含解决方案文件夹中所有项目共享的样式。
这是项目A:
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="pack://application:,,,/ProjectC;component/Themes/ThemeZ.xaml"/>
</ResourceDictionary.MergedDictionaries>
<!--<Style TargetType="{x:Type ToolTip}"/>-->
</ResourceDictionary>
<Application x:Class="MergedResourceDictionaryIssue.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="/Themes/Generic.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
</Application>
<Window x:Class="MergedResourceDictionaryIssue.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">
<Grid>
<Button/>
</Grid>
</Window>
这是项目C:
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<!-- Styles for all our buttons, let make them all red squares -->
<Style TargetType="{x:Type Button}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate>
<Grid Width="50"
Height="50"
Background="Red" />
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</ResourceDictionary>
如果我取消注释 ResourceDictionary 项目中的代码按预期呈现,否则资源字典将被忽略。
【问题讨论】:
标签: wpf xaml resourcedictionary