Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can a wpf application be deployed without compiling the xaml?

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.

like image 553
YeahStu Avatar asked Dec 01 '08 16:12

YeahStu


People also ask

Can WPF applications be built without XAML?

But WPF without XAML is possible. You don't even need Visual Studio installed for this, just the . NET CLI and the Developer Command Prompt. Drop these two files in a folder and run msbuild and then you can run the file in the Bin directory.

Does WPF use XAML?

Most WPF apps consist of both XAML markup and code-behind. Within a project, the XAML is written as a . xaml file, and a CLR language such as Microsoft Visual Basic or C# is used to write a code-behind file.

Why XAML is used in WPF?

The goal of XAML is to enable visual designers to create user interface elements directly. WPF aims to make it possible to control all visual aspects of the user interface from mark-up.


2 Answers

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.

like image 81
Daniel Paull Avatar answered Oct 17 '22 08:10

Daniel Paull


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!

like image 21
TheSoftwareJedi Avatar answered Oct 17 '22 10:10

TheSoftwareJedi