I'm currently struggling to find a way to load and unload a WPF assembly from an arbitrary path (not a sub directory of the base path) from a C# console application.
I'm exploring stackoverflow for a couple of days (as I have done with some other forums), but I can't actually find a way how to get the console application loading the WPF assembly and instantiating an object from the assembly.
Starting point is following example as C# console application:
using WPFLibrary;
namespace Caller
{
class Program
{
[STAThread]
static void Main(string[] args)
{
SampleWindow wnd = new SampleWindow();
wnd.ShowDialog();
}
}
}
and the following code as WPF assembly:
namespace WPFLibrary
{
public partial class SampleWindow : Window
{
public SampleWindow()
{
InitializeComponent();
}
}
}
I found various similar questions and suggestions to use:
Assembly.Load();
Activator.CreateInstance();
CreateInstanceAndUnwrap
AssemblyResolve
for the AppDomain etc. But I could never get my simple example above working if the WPFLibrary is not in a sub directory of the base path of the Caller console application. Since I'm new in this field I may just have missed a simple thing or this scenario may be completely impossible.... Any help would be appreciated.
In case you are not interested to understand my motivation for this question just skip the remaining of this post. We are developing an c++ dll application for a commercial CAD system. The interface for this system is c/c++ only so there no way to change the complete code to a C# application. This application includes a simple scripting language which also allows to define and execute a graphical user interface. Currently this is encoded in a win32 / MFC way, but since it includes a self build layout manager it is obviously a good idea to think about other alternatives. A perfect match seems to be use use WPF, but that requires to use .NET and to allow XAML definitions a C# library connected by a C++/CLI dll seems to be a good setup.
Some major requirements are:
I had to do something kind of similar, although from my console app I was starting up a WPF app. Maybe this will help:
static void Main(string[] args)
{
Initialise();
if (!RunSilently)
FireUpGui();
else
RunConsoleApp(); // not so relevant to the answer
}
private static void FireUpGui()
{
Console.SetWindowSize(80, 5);
Thread t = new Thread(Start_Wpf);
t.SetApartmentState(ApartmentState.STA);
t.Start();
}
static App WpfApp;
static void Start_Wpf()
{
WpfApp = new App(); // my WPF App
WpfApp.Run();
}
It's worth noting though that my App class was in the same assembly as my console app, which meant I already referenced the normal WPF framework assemblies.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.