views:

2220

answers:

7

Is it possible to deploy a WPF windows application in such a way that the xaml files can be manipulated at run-time? If possible, I would imagine this would work similar to an asp.net application that can deploy the .aspx pages as content, which are then compiled just-in-time at run-time.

I'd like to allow the simple layout of a screen to be edited at run-time by editing the XAML. Does anyone know if this is possible?

Edit: When I refer to xaml files, I mean the corresponding xaml to my UIElement classes. In other words, I have defined UserControl classes using Xaml and code-behind, inheritance, event handlers, assembly references, etc. When deployment time comes, I'd like to be able to keep the code-behind functionality but still allow the xaml to be edited.

+4  A: 

Not easily. A WPF application has it's XAML translated into .g.cs and .baml by the compiler, and is subsequently compiled into the binary.

Certainly it's possible to create a runtime XAML compiler. This would not be an easy task, and would really require some thinking about why not just do this as a website!

TheSoftwareJedi
A: 

XAMLPad and XAML Cruncher both do it, so it must be possible.

XAML Cruncher source code is available only in Charles Petzold's book on WPF:

http://www.charlespetzold.com/wpf/

Looks like it uses XamlReader to parse XAML.

Lou Franco
+1  A: 

You can load XAML to the application at runtime as bellow.

UIElement documentRoot = (UIElement)System.Windows.Markup.XamlReader.Load(xmlReader);

There is a concept of "loose XAML'. Loose XAML is basically the capability that allows you to open any XAML file on your hard disk (or a hyperlink on a web page) and "run" it within a browser without compilation.

Check out this sample loose XAML page which has a set of Buttons and custom control templates I had implemented, http://wpf.ria-labs.com/ux.xaml in this blog It just opens in IE. You can load the XAML to a WPF application and ca be used.

Jobi Joy
FWIW, I was able to open it fine using FF3. I have .NET 3.5SP1 installed, if that matters.
Andy
But it doesn't really answer the question. The OP stated specifically that this was a WPF application, so XAML's ability to be displayed in a web browser is completely beside the point.
GalacticCowboy
I used 'Web browser' just to explain more characteristics of LooseXAML concept.
Jobi Joy
+2  A: 

This blog post: http://www.thinkbottomup.com.au/site/blog/Embedding_DLR_Scripts_in_XAML_Part_6 has some info on loading XAML at runtime as well as other fun stuff.

Andrew Kennan
+6  A: 

Thank you unthinkableMayhem for the reference.

The problem with loose XAML is that the XamlReader, when invoked at runtime, can not hookup events to functions in your assemblies, nor is there a mechanism to dynamically load assemblies. The way handlers for routed events are specified in XAML is one of the biggest design flaws of WPF/XAML and makes loose XAML next to useless.

The work I've done in embedding Dynamic Language Runtime (DLR) scripts in XAML is useful for both loose and compiled XAML. My intention was to make loose XAML a first class citizen in WPF by allowing routed events, commands, value converters and other XAML/WPF concepts available in loose XAML. I feel that this has been achieved (though my blogs don't mention dynamically loading assemblies, but this is straight forward).

If you persist, it is possible to deploy a WPF windows application in such a way that the xaml files can be manipulated at run-time. Heck, you can dynamically generate XAML with embedded scripts - no Assembly required. Man, that's one of the funniest things I've said for a long time.

Be warned though - the "X" in XAML is only a marketing term. It is not eXtensible from an engineering point of view, so expect a world of pain when you get off the path set out by Microsoft. I'll be happy to help if you go this way - contact details on my web site (www.thinkbottomup.com.au).

Cheers,

Dan

Daniel Paull
+5  A: 

I have another suggestion for you - the real question was:

"I'd like to allow the simple layout of a screen to be edited at run-time by editing the XAML. Does anyone know if this is possible?"

The answer is definitely, "YES"! And there are many ways to achieve this, making a few assumptions of course.

If you have no need to handle events or write custom value converters (or anything else that would normally go in the code behind) in the "dynamic" part of your XAML, then you can simply use the XamlReader class to parse a XAML file or string containing XAML. Since you are merely editing the layout, I expect that these assumption holds true.

So, here is what I would do:

1) Write all your custom controls, data models, value converters, etc, and stick them in an assembly.

2) Load that assembly, either by having your app reference it or load it dynamically - both will work.

3) Create a string/file/resource (take your pick) that has your XAML that does layout, complete with the mapping of your .NET namespace to an XML namespace. Make sure you do not have an "x:Class" attribute on the root element as you have no code behind file! The string would use the standard WPF controls (like the StackPanel) to layout your custom controls. (Of course you can also write custom layout controls).

4) Allow the user to edit this string. When they have edited it, use the XamlReader to parse the file and then display the resulting UIElement in your window.

BINGO!

One problem - everytime the XAML is changed, the GUI is tossed and a new one created. If your GUI is sateful (even if the current caret position is important), the user will get annoyed pretty quickly. It depend on what your intend use is - this may not be an issue.

I expect that with some more work, you could write a MarkupExtension that is used to refer to the parts that you are trying to layout. This way they could be reused when the layout changes.

I hope this is clear. If not, let me know and I can expand on the concept - it'd make a nice blog entry.

Daniel Paull
this is helpful insight. I have to do some research to determine if this is what I need to do.
YeahStu
I'm happy to help further if the approach sounds like what you want to do but cant get WPF to display good manners. daniel.paull at thinkbottomup.com.au if you need to get hold of me.Cheers, Dan.
Daniel Paull
A: 

Hi,

I am trying to create a XAML page where I don't have to compile the code to have events fired for buttons etc. What I want is to create the base XAML, possibly compile it once at design time, but then be able to change the interface at run time. We could assume every screen will have either one or no buttons. Is this possible?