WPF中的动态主题

WPF中的动态主题,wpf,dynamic,themes,wpftoolkit,Wpf,Dynamic,Themes,Wpftoolkit,我打算使用WPF在.net中开发一个windows应用程序。 因此,我们如何在运行时实现动态主题。关于这一点我已经搜索了很多,但是 我不明白这件事。 如果我在app.xaml中添加下面的行,那么它会显示错误,因为我们如何直接添加东西行。尽管不存在名为“ExpressionDark”的文件 ***或*** 提前谢谢 :)您可以在App.Xaml中合并主题,如下所示: <Application.Resources> <ResourceDictionary>

我打算使用WPF在.net中开发一个windows应用程序。 因此,我们如何在运行时实现动态主题。关于这一点我已经搜索了很多,但是 我不明白这件事。 如果我在app.xaml中添加下面的行,那么它会显示错误,因为我们如何直接添加东西行。尽管不存在名为“ExpressionDark”的文件


***或***
提前谢谢
:)

您可以在App.Xaml中合并主题,如下所示:

<Application.Resources>
    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="defaulttheme.xaml" />
        </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>
</Application.Resources>
  <ResourceDictionary Source="/MyThemeProject;component/defaulttheme.xaml" />       
问候


flusser

假设通过
DynamicThemes
,您的意思是在运行时放置主题,这是加载资源字典的最佳方式,它将所有控件样式加载到主应用程序的资源或任何控件中

    public static ResourceDictionary GetThemeResourceDictionary(Uri theme)
    {
        if (theme != null)
        {
            return Application.LoadComponent(theme) as ResourceDictionary;
        }
        return null;
    }

    public static void ApplyTheme(this ContentControl control /* Change this to Application to use this function at app level */, string theme)
    {
        ResourceDictionary dictionary = GetThemeResourceDictionary(theme);

        if (dictionary != null)
        {
            // Be careful here, you'll need to implement some logic to prevent errors.
            control.Resources.MergedDictionaries.Clear();
            control.Resources.MergedDictionaries.Add(dictionary);
            // For app level
            // app.Resources.MergedDictionaries.Clear();
            // app.Resources.MergedDictionaries.Add(dictionary);

        }
    }
<ResourceDictionary Source="/MyThemeProject;component/Folder1/Folder2/defaulttheme.xaml" />
NewTheme = new Uri(@"/MyThemeProject;component/folder1/Folder2/bluetheme.xaml", UriKind.Relative);

Application.Current.Resources.MergedDictionaries.Clear();
Application.Current.Resources.MergedDictionaries.Add(NewTheme); 
    public static ResourceDictionary GetThemeResourceDictionary(Uri theme)
    {
        if (theme != null)
        {
            return Application.LoadComponent(theme) as ResourceDictionary;
        }
        return null;
    }

    public static void ApplyTheme(this ContentControl control /* Change this to Application to use this function at app level */, string theme)
    {
        ResourceDictionary dictionary = GetThemeResourceDictionary(theme);

        if (dictionary != null)
        {
            // Be careful here, you'll need to implement some logic to prevent errors.
            control.Resources.MergedDictionaries.Clear();
            control.Resources.MergedDictionaries.Add(dictionary);
            // For app level
            // app.Resources.MergedDictionaries.Clear();
            // app.Resources.MergedDictionaries.Add(dictionary);

        }
    }