How to build WPF from memory?

If the ms variable is a MemoryStream and contains the .Net assembly, you usually run it like this:

 var asm = Assembly.Load(ms.ToArray()); var entry = asm.EntryPoint; var inst = asm.CreateInstance(entry.Name); entry.Invoke(inst, null); 

This works well in console and Windows form applications, however WPF applications throw an exception:

 Exception has been thrown by the target of an invocation. 

With an internal exception of type System.IO.IOException :

 Cannot locate resource 'mainwindow.xaml'. 

The column is really large, but guessing from the very beginning, it cannot find resources when loading from memory:

 at MS.Internal.AppModel.ResourcePart.GetStreamCore(FileMode mode, FileAccess access) at System.IO.Packaging.PackagePart.GetStream(FileMode mode, FileAccess access) at System.IO.Packaging.PackagePart.GetStream() at System.Windows.Application.LoadComponent(Uri resourceLocator, Boolean bSkipJournaledProperties) at System.Windows.Application.DoStartup() at System.Windows.Application.<.ctor>b__1(Object unused) [...] 

How can i fix this?

+4
source share
2 answers

When you load an assembly dynamically from a MemoryStream, its working directory will work with your own assembly. This directory is unlikely to contain the XAML markup files referenced by the assembly.

Try installing Environment.CurrentDirectory into a new directory containing the necessary XAML, at least for the time the assembly is loaded and the class is instantiated.

0
source

As S.A. Kryukov suggested about codeproject here , I made a WPF application in the library with a user input point, which I then call with my second expression. The problem seems to be due to the fact that App.xaml implements starturi

0
source

All Articles