在运行时更改主题

发布于 2024-10-06 13:50:38 字数 792 浏览 4 评论 0原文

我使用 JetPack 主题并从 App.xaml 中设置它:

<Application.Resources>
    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="Assets/Styles/Brushes.xaml"/>
            <ResourceDictionary Source="Assets/Styles/Fonts.xaml"/>
            <ResourceDictionary Source="Assets/Styles/CoreStyles.xaml"/>
            <ResourceDictionary Source="Assets/Styles/Styles.xaml"/>
            <ResourceDictionary Source="Assets/Styles/SdkStyles.xaml"/>
            <ResourceDictionary Source="Assets/Styles/ToolkitStyles.xaml"/>
        </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>
</Application.Resources>

如何从代码隐藏中设置主题并在运行时更改主题?

I use JetPack theme and set it from App.xaml:

<Application.Resources>
    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="Assets/Styles/Brushes.xaml"/>
            <ResourceDictionary Source="Assets/Styles/Fonts.xaml"/>
            <ResourceDictionary Source="Assets/Styles/CoreStyles.xaml"/>
            <ResourceDictionary Source="Assets/Styles/Styles.xaml"/>
            <ResourceDictionary Source="Assets/Styles/SdkStyles.xaml"/>
            <ResourceDictionary Source="Assets/Styles/ToolkitStyles.xaml"/>
        </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>
</Application.Resources>

How can i set theme from code-behind and change theme at run time?

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

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

发布评论

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

评论(1

悸初 2024-10-13 13:50:38

Silverlight Toolkit 基本 Theme 控件提供对在运行时更改主题的支持。不幸的是,像 JetPack 主题这样的应用程序主题不是 Toolkit 主题(询问 Microsoft 为什么)。所以你必须自己转换它们。查看 Toolkit 主题源有助于我们弄清楚如何实现:

public class JetPackTheme : Theme
{
    private static Uri ThemeResourceUri = new Uri("/MyComponent;component/JetPackTheme.xaml", UriKind.Relative);

    public JetPackTheme() : base(ThemeResourceUri) { }

    public static bool GetIsApplicationTheme(Application app)
    {
        return GetApplicationThemeUri(app) == ThemeResourceUri;
    }

    public static void SetIsApplicationTheme(Application app, bool value)
    {
        SetApplicationThemeUri(app, ThemeResourceUri);
    }
}

现在,假设您的资源位于名为 JetPackTheme 的文件夹中,这里是 JetPackTheme.xaml

<ResourceDictionary
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">

    <ResourceDictionary.MergedDictionaries>
        <ResourceDictionary Source="/MyComponent;component/JetPackTheme/Brushes.xaml"/>
        <ResourceDictionary Source="/MyComponent;component/JetPackTheme/Fonts.xaml"/>
        <ResourceDictionary Source="/MyComponent;component/JetPackTheme/CoreStyles.xaml"/>
        <ResourceDictionary Source="/MyComponent;component/JetPackTheme/Styles.xaml"/>
        <ResourceDictionary Source="/MyComponent;component/JetPackTheme/SdkStyles.xaml"/>
        <ResourceDictionary Source="/MyComponent;component/JetPackTheme/ToolkitStyles.xaml"/>    
    </ResourceDictionary.MergedDictionaries>
</ResourceDictionary>

现在您应该能够在应用程序中使用 JetPackTheme 控件:

<myCmp:JetPackTheme x:Name="myTheme">
    <SomeNeatStuff>
        ...
    </SomeNeatStuff>
</myCmp:JetPackTheme>

要在运行时更改主题,您只需执行以下操作

myTheme.ThemeUri = new Uri("Path/To/The/Theme.xaml", UriKind.RelativeOrAbsoluteOrWhatever);

The Silverlight Toolkit base Theme control provides support for changing a theme at runtime. Unfortunately, the Application Themes like the JetPack Theme are no Toolkit themes (ask Microsoft why). So you'd have to convert them yourself. A look at the Toolkit themes sources helps us to figure out how:

public class JetPackTheme : Theme
{
    private static Uri ThemeResourceUri = new Uri("/MyComponent;component/JetPackTheme.xaml", UriKind.Relative);

    public JetPackTheme() : base(ThemeResourceUri) { }

    public static bool GetIsApplicationTheme(Application app)
    {
        return GetApplicationThemeUri(app) == ThemeResourceUri;
    }

    public static void SetIsApplicationTheme(Application app, bool value)
    {
        SetApplicationThemeUri(app, ThemeResourceUri);
    }
}

Now, assuming your resources are in a folder called JetPackTheme, here is JetPackTheme.xaml:

<ResourceDictionary
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">

    <ResourceDictionary.MergedDictionaries>
        <ResourceDictionary Source="/MyComponent;component/JetPackTheme/Brushes.xaml"/>
        <ResourceDictionary Source="/MyComponent;component/JetPackTheme/Fonts.xaml"/>
        <ResourceDictionary Source="/MyComponent;component/JetPackTheme/CoreStyles.xaml"/>
        <ResourceDictionary Source="/MyComponent;component/JetPackTheme/Styles.xaml"/>
        <ResourceDictionary Source="/MyComponent;component/JetPackTheme/SdkStyles.xaml"/>
        <ResourceDictionary Source="/MyComponent;component/JetPackTheme/ToolkitStyles.xaml"/>    
    </ResourceDictionary.MergedDictionaries>
</ResourceDictionary>

Now you should be able to use a JetPackTheme control in your application:

<myCmp:JetPackTheme x:Name="myTheme">
    <SomeNeatStuff>
        ...
    </SomeNeatStuff>
</myCmp:JetPackTheme>

To change the theme at runtime, you can simply do

myTheme.ThemeUri = new Uri("Path/To/The/Theme.xaml", UriKind.RelativeOrAbsoluteOrWhatever);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文