MegaMark
MegaMark

Reputation: 610

setting the MainWindow.Background to an external resource

I have a .dll that contains a theme 'engine'. I have all but completed this engine with a built in combobox that handles switching the themes through the .dll. In short what I'm trying to accomplish is a stand-alone .dll that I can reference from any project and (without adding any code to the hosting app) have my themes, styles, etc...

So far this works but I've run into a little bit of a predicament. Since the .dll loads the necessary xaml files at runtime using URI syntax, the hosting applications controls themselves do not know about the resources until the app is loaded. Now this generates a compile-time warning but no problem when running the app.

My question has to do with this compile-time issue. How can I set the main window's background color if I can't view the brushes available from my .dll until runtime?

<Window x:Class="Check.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:mycon="clr-namespace:ThemeLibrary.MyControls;assembly=ThemeLibrary"
    Title="MainWindow" Height="350" Width="525" WindowStyle="None" AllowsTransparency="True" 
Background="{***DynamicResource ResourceKey=TransparentColorBrush***}">

Again despite the fact that there is an error, the program compiles and runs fine because if the dynamic resource cannot be resolved the window just defaults, but once the .dll loads a resource dictionary containing a brush whose key is TransparentColorBrush, then the window background changes appropriately. But if I hand my .dll off to say the public to play with, they won't KNOW what the different brush keys are and therefore won't be able to do this hack.

So, how can I set the Application.Current.MainWindow.Background = (a brush from a URI loaded resource dictionary, or the dictionary itself) within the .dll, OR expose the brushes' keys as public properties?

Upvotes: 1

Views: 415

Answers (1)

MegaMark
MegaMark

Reputation: 610

Ok here's what I've found out...

In my .dll I can set the following properties for the application window by simply using the below syntax...

Application.Current.MainWindow.SetResourceReference(Window.BackgroundProperty, "MainWindowBackgroundBrush");
        Application.Current.MainWindow.SetResourceReference(Window.BorderBrushProperty, "BorderBrush");
        Application.Current.MainWindow.SetResourceReference(Window.BorderThicknessProperty, "MainBorderThickness");
        Application.Current.MainWindow.SetResourceReference(Window.AllowsTransparencyProperty, "MainAllowsTransparency");

in the static class I created to help govern the interactions of the skin changing. I also declared a new resource dictionary as follows:

xmlns:s="clr-namespace:System;assembly=mscorlib"
<Thickness x:Key="MainBorderThickness" >2</Thickness>
<s:Boolean x:Key="MainAllowsTransparency">True</s:Boolean>

I'm super excited!!! This is exactly what I've been searching for. So now my custom skins and the mechanisms to change them from one to another are all wrapped up into a single .dll that handles EVERYTHING regarding the skin without having to add a single character of code to the hosting application. Now every File>New can simply have an assembly reference and xmlns namespace and POOF all of my skins are available.

Upvotes: 1

Related Questions