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.
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.
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.
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.
NET MAUI Alternatives. As Xamarin. Forms morphs into the new .
As Jakob Christensen noted, you can load any XAML you want using XamlReader.Load
. This doesn't apply only for styles, but UIElement
s 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 Path
s, you can assign the Path
s' 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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With