如何隐式在应用程序项目设计器中应用组件设计器中的主题?

时间:2010-11-02 12:09:01

标签: wpf resources themes

假设我有一个标题按钮的主题文件Button.xaml。

通过合并在应用程序资源字典中,它可以隐式应用于应用程序项目设计器中。

但是在我将Button.xaml移动到组件项目之后,组件项目设计者无法隐式应用主题文件。

如何在组件项目中隐式使用该主题文件?

更新 Themes.xaml如下

<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:,,,/StyleLibrary;component/Themes/Shared.xaml" />
    <ResourceDictionary Source="pack://application:,,,/StyleLibrary;component/Themes/Button.xaml" />
    <ResourceDictionary Source="pack://application:,,,/StyleLibrary;component/Themes/ComboBox.xaml" />
...




<Application x:Class="ButtonStyleTest.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="pack://application:,,,/StyleLibrary;component/Themes.xaml"/> 
            </ResourceDictionary.MergedDictionaries> 
        </ResourceDictionary> 
    </Application.Resources> 
</Application> 

如果我使用合并的Theme.xaml,Button在设计器中看起来很正常,但在运行时失败了。 但是如果我一个接一个地合并Button.xaml,ComboBox,那么设计和运行时看起来都很正常。

1 个答案:

答案 0 :(得分:0)

新例子。

假设您有一个名为StyleLibrary的库 在这个库中,你有一个名为Button.xaml的ResourceDictionary,它看起来像这样

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <Style TargetType="{x:Type Button}" >
        <Setter Property="Width" Value="75" />
        <Setter Property="Height" Value="23" />
    </Style>
</ResourceDictionary>

在应用程序中,我们添加对StyleLibrary的引用,在App.xaml中我们添加它。

<Application x:Class="ButtonStyleTest.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="pack://application:,,,/StyleLibrary;component/Button.xaml"/>
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
    </Application.Resources>
</Application>

此样式将在运行时和设计器中应用于应用程序和库中。

左边是我的MainWindow。是有一个自己的Button,然后是一个来自TestLibrary的UserControl,它有两个Buttons。右边是一个来自TestLibrary的窗口,它包含一个来自TestLibrary的UserControl,所有按钮似乎都应用了样式。

alt text

相关问题