如何在对话框中设置主应用程序主题,而不在 XAML 中显式设置?

发布于 2024-10-11 03:14:06 字数 950 浏览 1 评论 0原文

更新

我很不好意思地说我犯了一个错误。错误是我的主题没有设置我用来测试的元素的样式,所以我当然没有看到应用的样式。

然而,针对这个问题的答案确实显示了如何将资源从一个 UserControl 显式设置到另一个......这很有趣。

当我按照下面描述的方式在应用程序上设置资源时,它确实隐式地在生成的应用程序中设置了所有用户控件主题。


我正在使用一个主题,在我的主应用程序类上设置为 ResourceDictionary。我想我的主窗口隐含地使用这个主题来设计自己的风格。

<Application>
    <Application.Resources>
        <ResourceDictionary Source="Themes/ExpressionDark.xaml" />

以下注释是错误的,并且所有内容都隐式设置了样式

但是,当我显示对话框时,这没有设置样式。

DialogBox dialog = new DialogBox();
dialog.ShowDialog();

有没有某种方法可以隐式执行此操作,而无需在 DialogBox 的 XAML 中显式指定样式?

编辑

我尝试通过以下方式设置资源。他们没有工作。

Window main = App.Current.Windows[0];
dialog.Resources = main.Resources;
dialog.Owner = main;

还尝试从主应用程序设置...这是定义原始资源的地方。

dialog.Resources = App.Current.Resources;

Update

I am embarrassed to say that I made a mistake. The error was that my theme doesn't style the elements I was using to test, so of course I wasn't seeing the applied styles.

However, the answers made in response to this question do show how to explicitly set resources from one UserControl to another... which is interesting.

When I set resources on the application in the way I describe below, it does indeed implicitly set all user control themes in the resulting application.


I am using a theme, set as a ResourceDictionary on my main Application class. My main window I guess implicitly uses this theme to style itself.

<Application>
    <Application.Resources>
        <ResourceDictionary Source="Themes/ExpressionDark.xaml" />

The following comments are wrong, and everything is styled implicitly

When I show a dialog however, this is not styled.

DialogBox dialog = new DialogBox();
dialog.ShowDialog();

Is there some way to do this implicitly without specifying the style explicitly in the XAML of DialogBox?

Edit

I tried setting the resources in the following ways. They did not work.

Window main = App.Current.Windows[0];
dialog.Resources = main.Resources;
dialog.Owner = main;

Also tried setting the from the main App... which is where the original resources are defined.

dialog.Resources = App.Current.Resources;

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

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

发布评论

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

评论(2

抽个烟儿 2024-10-18 03:14:06

您可以按如下方式实现:

dialog.Resources.MergedDictionaries.Add( this.Resources.MergedDictionaries[0] );

另一种可能性是从应用程序加载它们(但我不喜欢):

ResourceDictionary dict = Application.LoadComponent( new Uri( "<library>;component/Themes/<theme>.xaml", UriKind.Relative ) ) as ResourceDictionary;

dialog.Resources.MergedDictionaries.Add( dict );

You can implement like as follows:

dialog.Resources.MergedDictionaries.Add( this.Resources.MergedDictionaries[0] );

And the other possibility is to load them from app (but I wouldn't prefer):

ResourceDictionary dict = Application.LoadComponent( new Uri( "<library>;component/Themes/<theme>.xaml", UriKind.Relative ) ) as ResourceDictionary;

dialog.Resources.MergedDictionaries.Add( dict );
泪意 2024-10-18 03:14:06

您可以以一种或另一种方式传输资源,或者如果您不需要指定对话特定资源,则只需将对话的资源设置为主窗口的资源:(

    public Dialogue(Window owner)
    {
        this.InitializeComponent();
        Owner = owner;
        Resources = Owner.Resources;
    }

因为它是一个对话,所以我设置了 Owner属性也是如此)

You could transfer resources one way or another, or just set the dialogue's resources to the resources of the main window if you don't need to specify dialogue specific resources as well:

    public Dialogue(Window owner)
    {
        this.InitializeComponent();
        Owner = owner;
        Resources = Owner.Resources;
    }

(Since it's a dialogue i set the Owner property as well)

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