Wednesday, May 20, 2015

WPF.Themes dis-assembled for customization with Nuget & ClickOnce Deployments


// NOTE: CLASSES HAVE BEEN EXTRACTED FROM WPF.THEMES.DLL
// NOTE: FOR THE PURPOSE OF KEEPING THE SHINYBLUE THEME,
// NOTE: SINCE THE NUGET DLL WILL NOT ALLOW CODE SIGNING.
//
// NOTE: THIS CODE ATTEMPTS TO KEEP THE SAME OBJECT LOADING PROCESSES AS THE ORIGINAL DLL.
// NOTE: HOWEVER, SINCE THE THEME.XAML MUST BE LOADED INTO THE ACTUAL "RESOURCES" FILE,
// NOTE: AND WE HAVE ALREADY LOADED THE SHINYBLUE.XAML INTO THE APP VIA THE \Resources\DefaultStyles.xaml
// NOTE: WE HAVE SIMPLY ADDED THIS CODE, TO PROVIDE WPF THEME CUSTOMIZATION, IF NEEDED.
//
namespace myApp.MainForm.Themes
{
using System;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Resources;

public static class ThemeManager
{
public static readonly DependencyProperty ThemeProperty = DependencyProperty.RegisterAttached("Theme", typeof(string), typeof(ThemeManager), new FrameworkPropertyMetadata(string.Empty, new PropertyChangedCallback(ThemeManager.OnThemeChanged)));

public static void ApplyTheme(this Application app, string theme)
{
ResourceDictionary themeResourceDictionary = GetThemeResourceDictionary(theme);
if (themeResourceDictionary != null)
{
app.Resources.MergedDictionaries.Clear();
app.Resources.MergedDictionaries.Add(themeResourceDictionary);
}
}

public static void ApplyTheme(this FrameworkElement control, string theme)
{
ResourceDictionary themeResourceDictionary = GetThemeResourceDictionary(theme);
if (themeResourceDictionary != null)
{
control.Resources.MergedDictionaries.Clear();
control.Resources.MergedDictionaries.Add(themeResourceDictionary);
}
}

public static string GetTheme(DependencyObject d)
{
return (string)d.GetValue(ThemeProperty);
}

public static Uri GetResourceThemesUri(string theme)
{
Assembly assembly = Assembly.GetExecutingAssembly();
AssemblyName assemblyName = new AssemblyName(assembly.FullName);
var x = new Uri(String.Format(assemblyName.Name + @";component/Themes/{0}.xaml", theme), UriKind.Relative);
return x;
}


public static ResourceDictionary GetThemeResourceDictionary(string theme)
{
if (theme != null)
{
//KEEP: Assembly assembly = Assembly.LoadFrom("WPF.Themes.dll");
//KEEP: return (Application.LoadComponent(new Uri(string.Format("/WPF.Themes;component/{0}/Theme.xaml", theme), UriKind.Relative)) as ResourceDictionary);
//KEEP: Uri uri = new Uri(String.Format("myApp.MainForm;component/{0}/Theme.xaml", theme), UriKind.Relative);

StreamResourceInfo info = Application.GetResourceStream(GetResourceThemesUri(theme));
System.Windows.Markup.XamlReader reader = new System.Windows.Markup.XamlReader();
ResourceDictionary myResourceDictionary = (ResourceDictionary)reader.LoadAsync(info.Stream);
//KEEP: Application.Current.Resources.MergedDictionaries.Add(myResourceDictionary);
return myResourceDictionary;
}
return null;
}

public static string[] GetThemes()
{
//return new string[] { "ExpressionDark", "ExpressionLight", "ShinyBlue", "ShinyRed", "DavesGlossyControls", "WhistlerBlue", "BureauBlack", "BureauBlue", "BubbleCreme", "TwilightBlue", "UXMusingsRed", "UXMusingsGreen", "UXMusingsBubblyBlue" };
return new string[] { "ShinyBlue" };
}

private static void OnThemeChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
string newValue = e.NewValue as string;
if (!String.IsNullOrEmpty(newValue))
{
ContentControl control = d as ContentControl;
if (control != null)
{
control.ApplyTheme(newValue);
}
}
}

public static void SetTheme(DependencyObject d, string value)
{
d.SetValue(ThemeProperty, value);
}
}
}

namespace myApp.MainForm.Themes.Properties
{
using System;
using System.CodeDom.Compiler;
using System.ComponentModel;
using System.Configuration;
using System.Diagnostics;
using System.Globalization;
using System.Resources;
using System.Runtime.CompilerServices;

[DebuggerNonUserCode, CompilerGenerated, GeneratedCode("System.Resources.Tools.StronglyTypedResourceBuilder", "4.0.0.0")]
internal class Resources
{
private static CultureInfo resourceCulture;
private static System.Resources.ResourceManager resourceMan;

internal Resources()
{
}

[EditorBrowsable(EditorBrowsableState.Advanced)]
internal static CultureInfo Culture
{
get
{
return resourceCulture;
}
set
{
resourceCulture = value;
}
}

[EditorBrowsable(EditorBrowsableState.Advanced)]
internal static System.Resources.ResourceManager ResourceManager
{
get
{
if (object.ReferenceEquals(resourceMan, null))
{
System.Resources.ResourceManager manager = new System.Resources.ResourceManager("myApp.MainForm.Properties.Resources", typeof(Resources).Assembly);
resourceMan = manager;
}
return resourceMan;
}
}
}

[CompilerGenerated, GeneratedCode("Microsoft.VisualStudio.Editors.SettingsDesigner.SettingsSingleFileGenerator", "10.0.0.0")]
internal sealed class Settings : ApplicationSettingsBase
{
private static Settings defaultInstance = ((Settings) SettingsBase.Synchronized(new Settings()));

public static Settings Default
{
get
{
return defaultInstance;
}
}
}
}

No comments:

Post a Comment