English 中文(简体)
WPF 中的动态主题
原标题:Dynamic Themes in WPF

I am gong to develop an windows application in .net using WPF. So, how we can implement dynamic themes at run time. I have have searched a lot about this but I can nt understand this thing. If I add the below line in app.xaml then it shows error because how we can add thing line directly. Although there is no file exist with the name of "ExpressionDark".

<ResourceDictionary Source="Themes/ExpressionDark.xaml"/>
***or*** 
<ResourceDictionary Source="ExpressionDark.xaml"/>

Thanks in advance :)

最佳回答

假设使用 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);

        }
    }
问题回答

您可以像这样在 App.Xaml 中合并主题 :

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

默认主题. xaml 文件必须位于您的工程根部。 如果您想要为此主题构建自己的工程, 您可以将资源合并 :

  <ResourceDictionary Source="/MyThemeProject;component/defaulttheme.xaml" />       

在此默认hteme. xaml 中也必须在 root 的 MyTheme Project 中, 不要忘记从您主工程中添加该工程的引用 。

要构建一个结构,您可以随意添加文件夹。

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

观 观 观

长笛





相关问题
WPF convert 2d mouse click into 3d space

I have several geometry meshes in my Viewport3D, these have bounds of (w:1800, h:500, d:25). When a user clicks in the middle of the mesh, I want the Point3D of (900, 500, 25)... How can I achieve ...

Editing a xaml icons or images

Is it possible to edit a xaml icons or images in the expression design or using other tools? Is it possible to import a xaml images (that e.g you have exported) in the expression designer for editing?...

WPF: writing smoke tests using ViewModels

I am considering to write smoke tests for our WPF application. The question that I am faced is: should we use UI automation( or some other technology that creates a UI script), or is it good enough to ...

WPF - MVVM - NHibernate Validation

Im facing a bit of an issue when trying to validate a decimal property on domain object which is bound to a textbox on the view through the viewmodel. I am using NHibernate to decorate my property on ...

How do WPF Markup Extensions raise compile errors?

Certain markup extensions raise compile errors. For example StaticExtension (x:Static) raises a compile error if the referenced class cannot be found. Anyone know the mechanism for this? Is it baked ...

WPF design-time context menu

I am trying to create a custom wpf control, I m wondering how I can add some design-time features. I ve googled and can t seem to get to my goal. So here s my simple question, how can I add an entry ...

How to combine DataTrigger and EventTrigger?

NOTE I have asked the related question (with an accepted answer): How to combine DataTrigger and Trigger? I think I need to combine an EventTrigger and a DataTrigger to achieve what I m after: when ...

热门标签