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

发布于 2024-09-30 02:57:02 字数 1516 浏览 2 评论 0原文

假设我有一个标准按钮的主题文件 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.xaml、ComboBox 一一合并,无论是设计还是运行时,它看起来都很正常。

Say i had a theme file Button.xaml for standard button.

By merged in application resource dictionary it can implicitly applied in application project designer.

But after i move Button.xaml to component project, the component project designer can not implicitly apply the theme file.

How can i make that theme file works implicitly in component project ?

Update:
the Themes.xaml as follow

<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> 

If i use merged Theme.xaml, the Button look normal in designer, but failed in runtime.
But if i merge Button.xaml, ComboBox one by one, it look normal for both design and runtime.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

乞讨 2024-10-07 02:57:02

新的例子。

假设您有一个名为 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>

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

左边是我的主窗口。它有一个自己的按钮,然后是 TestLibrary 中的一个具有两个按钮的用户控件。右侧是 TestLibrary 中的一个窗口,其中包含 TestLibrary 中的一个 UserControl,所有按钮似乎都应用了该样式。

替代文本

New example.

Say that you have a Library called StyleLibrary.
In this Library you have a ResourceDictionary called Button.xaml which looks like this

<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>

In the Application we then add a reference to StyleLibrary and in App.xaml we add this.

<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>

This style will apply in the Application and in the Libraries both in runtime and in designer.

To the left is my MainWindow. Is has one Button of its own, and then a UserControl from TestLibrary that has two Buttons. To the right is a Window from TestLibrary which contains one UserControl from TestLibrary, all Buttons seem to have the style applied.

alt text

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文