tags:

views:

1424

answers:

6

Hi,

First some background: I'm working on an application and I'm trying to follow MVVM conventions writing it. One thing I'd like to do is to be able to give the application different "skins" to my application. The same application, but show one "skin" for one client and a different "skin" for another.

And so my questions are:
1. Is it possible to load a xaml file at run time and "assign" it to my app?
2. Can the xaml file be an external file residing in a different folder?
3. Can the application switch to another xaml file easily, or only at startup time?

So where should I start looking at for information on this? Which WPF methods, if they exist, handle this functionality?

Thanks!

Edit: the type of "skinning" I'm wanting to do is more than just changing the look of my controls. The idea is having a completely different UI. Different buttons, different layouts. Kinda like how one version of the app would be fully featured for experts and another version would be simplified for beginners.

A: 

AFAIK All this stuff (runtime parsing) is coming in 4.0

In the meantime, compiling skins [to BAML and making them resources in a DLL] is the [not very nice] answer.

Ruben Bartelink
A: 

What you should do is create ONE XAML file that defines the controls and let the code behind assing the styles to the controls. This can be accomplished by creating different resource dictionaries. There are many examples. The best I could find for now is: http://blog.rioterdecker.net/blogs/chaz/archive/2006/03/06/100.aspx

Good luck!

Dabblernl
+1  A: 

You can load any XAML that you want using XamlReader.Load.

If you style all your controls in your application and define those styles in your applications resource dictionary you can load new styles defined in XAML somewhere else using XamlReader.Load and replace parts of your resource dictionary with the loaded XAML. Your controls will change appearance accordingly.

Jakob Christensen
+2  A: 

I think this is fairly simple with the XamlReader, give this a shot, didn't try it myself, but I think it should work.

http://blogs.msdn.com/ashish/archive/2007/08/14/dynamically-loading-xaml.aspx

Carlo
+1  A: 

As Jakob Christensen noted, you can load any XAML you want using XamlReader.Load. This doesn't apply only for styles, but UIElements as well. You just load the XAML like:

UIElement rootElement;
FileStream s = new FileStream(fileName, FileMode.Open);
rootElement = (UIElement)XamlReader.Load(s);
s.Close();

Then you can set it as the contents of the suitable element, e.g. for

<Window x:Class="MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Foo Bar">
    <Grid x:Name="layoutGrid">
        <!-- any static elements you might have -->
    </Grid>
</Window>

you could add the rootElement in the grid with:

layoutGrid.Children.Add(rootElement);
layoutGrid.SetColumn(rootElement, COLUMN);
layoutGrid.SetRow(rootElement, ROW);

You'll naturally also have to connect any events for elements inside the rootElement manually in the code-behind. As an example, assuming your rootElement contains a Canvas with a bunch of Paths, you can assign the Paths' MouseLeftButtonDown event like this:

Canvas canvas = (Canvas)LogicalTreeHelper.FindLogicalNode(rootElement, "canvas1");
foreach (UIElement ui in LogicalTreeHelper.GetChildren(canvas)) {
    System.Windows.Shapes.Path path = ui as System.Windows.Shapes.Path;
    if (path != null) {
        path.MouseLeftButtonDown += this.LeftButtonDown;
    }
}

I've not tried switching XAML files on the fly, so I cannot say if that'll really work or not.

TomiJ
Hi TomiJ!This seems to be about what I'm looking for. The only bit I'm missing is how to handle .xaml files that are part of the solution. How would I go and load them?
Steve the Plant
In the application I'm working on, I just keep the XAML files in the solution and set their Build Action to "none", and have Visual Studio copy the files to the output directory if newer. The first code snippet above will then be used to load them.
TomiJ
Here's what I was looking for, loading a xaml as a resourcehttp://msdn.microsoft.com/en-us/library/aa970494.aspx
Steve the Plant
A: 

Check out http://69.10.233.10/KB/WPF/SkinningInWPF.aspx (this is CodeProject, don't know why Google indexes it by IP) - Josh Smith wrote a great article on how to do skinning in WPF.

Paul Betts