Search code examples
c#wpf

Dynamically loading code-behind along with XAML?


Related to this question I asked earlier, I wonder if it's possible to also dynamically load a code-behind file that is paired with a XAML file. Can it work this way or would it just be better to compile both into a DLL?.

Thanks!


Solution

  • Actually, it's the code-behind that loads the XAML file. The designer generates a hidden file that binds all of your named elements and events to the class. (Notice the 'partial' keyword on your class in the code-behind.) It functions similar to the file that the WinForms designer generates, only it's a bit harder to find. You can find them in the "obj/debug/" folder along with the compiled BAML.

    As for actually answering your quetion, it would be better to compile them to a DLL. It may not be impossible to set up a library that can connect a XAML to a special class that has methods to dynamically access elements, but there's nothing like that now as far as I know.