views:

24

answers:

1

Hi,

Is it possible to create a WPF Window control at runtime?

I'm thinking of a somewhat hack-ish solution to a problem, but I would essentially like to be able to have a dll which allows me to dynamically load a WPF Window and compile it's associated code behind (from an xaml and cs file respectivly). I assume I would also have to manually load any assemblies that the codebehind requires.

This would give me the ability to create modular addons which allow for Windows to be run from another application without being compiled into the source.

There are a few SO posts about this topic, but none seem to deal with window/code creation specifically.

If it's not possible (or just really not reccomended) thoughts as to alternatives would be greatly appreciated.

Cheers,

Kyle

+1  A: 

I would recommend looking into technologies like MEF instead. This is now included directly in the Framework, but allows you to "inject" 3rd party code/windows/views/viewmodels directly into your application, without distributing your application.

Instead of trying to compile the code in your application, you can provide simple interfaces, and let MEF compose the application for you. This is a much easier, more flexible option.

I've used this before where I've had a contract for ViewModels and a contract for ResourceDictionaries containing DataTemplates. This allows you to do fully-dynamic runtime injection of logic (ViewModel) with their associated Views, and just wrap it up in a Window at runtime.

Reed Copsey
Thanks a lot, it sounds like this might be a more manageable solution. I'll check it out.
Kyle