Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Loading XAML at runtime?

Tags:

c#

wpf

xaml

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.

like image 486
djcouchycouch Avatar asked May 26 '09 13:05

djcouchycouch


People also ask

How do I start XAML?

To begin editing your first XAML file, use Visual Studio or Visual Studio for Mac to create a new Xamarin. Forms solution. (Select the tab below corresponding to your environment.) In the Configure your new project window, set the Project name to XamlSamples (or whatever your prefer), and click the Create button.

How do I open a XAML file in my browser?

Step 1 : Open Microsoft Visual Studio 2010. Step 2 : Select File->New->Project. Step 3 : Select WPF Browser Application and then click Ok. Step 4 : Now drag two button controls and three label controls from the toolbox onto the design window of Page1.

How do I view XAML in Visual Studio?

To open this page, choose the Tools menu and then choose Options. To access the XAML Designer property page, choose the XAML Designer node. Settings for the XAML Designer are applied when you open the document. So, if you make changes to the settings, you need to close and then reopen Visual Studio to see the changes.

What replaced XAML?

NET MAUI Alternatives. As Xamarin. Forms morphs into the new .


1 Answers

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.

like image 88
Tomi Junnila Avatar answered Oct 15 '22 05:10

Tomi Junnila