开发者

Changing the Theme at Run Time

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?


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);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜