Loading XAML at runtime?

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

Up vote 9 down vote favorite 4 share g+ share fb share tw.

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. C# wpf xaml link|improve this question edited May 26 '09 at 14:44 asked May 26 '09 at 13:53DJ Couchy Couch1,780933 87% accept rate.

I think this is fairly simple with the XamlReader, give this a shot, didn't try it myself, but I think it should work. blogs.msdn.com/ashish/archive/2007/08/14....

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 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.

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? – DJ Couchy Couch May 26 '09 at 15:28 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.

– Tomi Junnila May 26 '09 at 15:36 Here's what I was looking for, loading a xaml as a resource msdn.microsoft.com/en-us/library/aa97049... – DJ Couchy Couch May 26 '09 at 16:37 Do you know if this approach is supposed to support bindings? I've tried it and can't even get it to throw an exception, so I suspect nothing is making the binding actually happen in my case. – Neil Barnwell Sep 21 '11 at 11:08 @NeilBarnwell, I rather suspect it doesn't support bindings, so you'd have to create them manually after loading the XAML.

(Disclaimer: Just my guess. ) – Tomi Junnila Sep 21 '11 at 18:48.

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.

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.

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: blog.rioterdecker.net/blogs/chaz/archive... Good luck!

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.

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.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions