动态改变隐式样式

发布于 2024-12-02 22:54:56 字数 65 浏览 3 评论 0原文

现在我的应用程序中有一些隐式样式的 TabItem。我想在我的应用程序中添加“夜间模式”并更改我的风格。我该怎么办?

Right now I have some TabItems in my App which are implicitly styled. I want to add a "Night mode" to my app and change my style. How should I go about this?

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

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

发布评论

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

评论(2

安静被遗忘 2024-12-09 22:54:56

您可以使用合并字典来做到这一点。将所有“正常”样式放入字典中,并默认将其添加到应用程序资源中:

<Application.Resources>
    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="Styles/Normal.xaml"/>
        </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>
</Application.Resources>

然后您可以删除当前字典并动态加载另一个字典:

private void ChangeStyles()
{
    App.Current.Resources.MergedDictionaries.Clear();

    StreamResourceInfo resInfo = App.GetResourceStream(new Uri("Styles/NewStyles.xaml", UriKind.Relative));
    XDocument xaml = XDocument.Load(resInfo.Stream);
    ResourceDictionary resource = XamlReader.Load(xaml.ToString()) as ResourceDictionary;

    App.Current.Resources.MergedDictionaries.Add(resource);
}

You can do this with merged dictionaries. Put all your "normal" styles in a dictionary and add it to the app resources by default:

<Application.Resources>
    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="Styles/Normal.xaml"/>
        </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>
</Application.Resources>

Then you can remove the current dictionary and load another one dynamically:

private void ChangeStyles()
{
    App.Current.Resources.MergedDictionaries.Clear();

    StreamResourceInfo resInfo = App.GetResourceStream(new Uri("Styles/NewStyles.xaml", UriKind.Relative));
    XDocument xaml = XDocument.Load(resInfo.Stream);
    ResourceDictionary resource = XamlReader.Load(xaml.ToString()) as ResourceDictionary;

    App.Current.Resources.MergedDictionaries.Add(resource);
}
狂之美人 2024-12-09 22:54:56

阿方索的想法是对的……
但你必须在 WPF 中这样做

App.Current.Resources.MergedDictionaries.Clear(); 
Uri uri = new Uri("/Resources/GlassButton5Night.xaml", UriKind.Relative);
var resDict = Application.LoadComponent(uri) as ResourceDictionary;
App.Current.Resources.MergedDictionaries.Add(resDict);

并且你必须确保在正确的级别重置你的 MergedDictionaries

Alfonso was right in idea...
but you have to do it like this in WPF

App.Current.Resources.MergedDictionaries.Clear(); 
Uri uri = new Uri("/Resources/GlassButton5Night.xaml", UriKind.Relative);
var resDict = Application.LoadComponent(uri) as ResourceDictionary;
App.Current.Resources.MergedDictionaries.Add(resDict);

And you have make sure you reset your MergedDictionaries at the right level

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