Multiple WPF applications in the same AppDomain - c#

I got following setup:
WPF_Application.exe
and
a DLL that contains a WinForms Window and an WPF Window.
The 'WPF_Application.exe' calls the WinForms window from the DLL and the WinForms Window creates an instance of the WPF window in the DLL.
That creates following exception:
Cannot create more than one System.Windows.Application instance in the same AppDomain.
Tried different things, but couldn't figure out how to fix it.

I fixed it by asigning the Application of the wpf window that is called to the Application.Current Window:
if (Application.Current == null)
{
MyApplication = new Application
{
ShutdownMode = ShutdownMode.OnExplicitShutdown
};
}
else
MyApplication = Application.Current;

System.Windows.Application is a singleton: its constructor must only be invoked once (including App.xaml de-xamlization), or exception is thrown.
I'm affraid there is not much you can do about it, except maybe check if Application.Current is set before starting your second application, and somewhat use that instance to load it.
Or you can create another AppDomain, and use it to launch the second application.

Make sure you only have one Application defined in XAML.
Visual studio creates an App.xaml when making a new WPF solution, if you have created a new .xaml file with the <Application></Application> tag, this will cause this error and the solution will build but fail at runtime.
Delete the duplicate application and use Window or UserControl to have multiple windows.

Related

Developing C# Visual Studio .exes similar to Java [duplicate]

This question already has answers here:
How do I create a C# app that decides itself whether to show as a console or windowed app?
(11 answers)
Closed 5 years ago.
I've come from developing in Java to developing in C# in Visual Studio. I'm very comfortable with the change apart from one thing: The way in which I can write hidden or GUI applications.
I used to use Swing for developing my GUI based applications in Java.
What i'd like to do is develop an application in the style of a Console Application with static void Main(string[] args){} however I want to be able to create windows and tell them to be visible/open using a method similar to jFrameObject.setVisible(bool). What I want is a console style application which I can use WPF windows and their components in, and i'd like to be able to make those windows in the window designer built in to Visual Studio 2017.
If at all possible, it would be nice if I had an option to show/hide the console too. I have tried a few different methods to do this, like this, however the console which is shown isn't linked to the C# Console object so doing Console.WriteLine(string); outputs to the debug console in Visual Studio 2017 and not the console which was opened in a window.
Thank you for any help given!
Theres no special thing if you want to open a WPF-window (even in a Console Project).
You can simply add a WPF Form using the Visual Studio Utilities (New/Window or something like this).
To open your window, you have two possibilities:
The first one is to open a dialog modal (the code doesn't continue until the form ist closed)
MyWindow window = new MyWindow();
window.ShowModal();
The second solution is open the window without waiting for the dialog to close(some kind like asynchronous)
MyWindow window = new MyWindow();
window.Show();
You can find further information on this article..
Enjoy programming in C# instead of Java.
Starting out, you can use Windows Forms which enables you to easily add GUI elements to an application, e.g. in your Console Application's Main():
var form = new System.Windows.Forms.Form();
form.Text = "Window Title";
var button = new System.Windows.Forms.Button();
button.Text = "OK";
form.Controls.Add(button);
form.ShowDialog();
This requires you to add a reference to the [System.Windows.Forms] assembly.
As for the console window [question]: Show/Hide the console window of a C# console application
If you want to do WPF, add a class representing some window:
public class SomeWindow : System.Windows.Window
{
public SomeWindow()
{
this.AddChild(new System.Windows.Controls.Button() { Content = "OK", Height = 30, Width = 100 });
}
}
Then use the application object to run the window:
new System.Windows.Application().Run(new SomeWindow());
The Main needs to be marked with the [STAThreadAttribute]. The assemblies PresentationCore, PresentationFramework and WindowsBase needs to be referenced.
You can start out with a Console application and add references to WinForms or WPF assemblies to open windows.
Alternatively you can start with a WinForms or WPF application and open a console using the Win32 API: How do I create a C# app that decides itself whether to show as a console or windowed app?

How do I open an executable as a MDI child?

I have a parent form that starts another application. When the application starts it should set the application as an mdichild of parent.
I have managed to get this to work with notepad using the code below.
Process proc = new Process();
proc.StartInfo.FileName = "notepad.exe";
proc.Start();
proc.WaitForInputIdle();
SetParent(proc.MainWindowHandle, this.panel.Handle);
The problem is that it doesn't work with all applications. Specifically it doesn't work with other win forms applications developed in c#, which is what I need it for.
Any help is appreciated, Thanks.
Edit: I have access to the code for the c# app i want to open as a mdi child.
OP: I have access to the code for the c# app i want to open as a mdi
child.
To open a form of other .Net windows application, you don't need to use SetParent and you can simply add a reference of that .Net Application (dll, exe or project) to your current project and then create an instance of the main form of that application and show it like other forms of your application.
If you have access to project of that application, you can add it to your current solution and use add a reference to your current project.
If you don't have access to project of the project of that application, you can add a reference to its exe/dll to your current project.
To learn more about add reference, read How to: Add or Remove References By Using the Add Reference Dialog Box
add .exe(for example another.exe) file as reference
add another.exe references // may be differ from your mdi form
add below code
another.Form1 fr = new another.Form1(); //another.exe has Form1 if not select form.
fr.MdiParent = this;
fr.Show(); // Display the new form.

Entry Point Not Found Exception when i create and display Window object

I've developed of software which consists, in a first configuration window which parse a ini file at his startup.
Using C# with WPF library.
If parsing it is correct the window is closed immediately and a second window is displayed.
The application runs correctly on Windows 8.
When i run the application on a Windows 7 PC a ENTRY POINT NOT FOUND EXCEPTION it is thrown. Following the code which throws exception from configuration window.
if(parse()){ //parse() returns true if it is correct
MainWindow w = new MainWindow(); //here where the exception is thrown
w.Show();
Close();
}
I've made the following attempts:
I've tried the following procedure, but it did not work:
http://answers.microsoft.com/en-us/windows/forum/windows_7-system/exe-entry-point-not-found/5b0866c1-8e45-457e-a83c-651c1fd307cc.
I've tried to develop a new WPF project, with two window.
In the start up window i've added a simple button which display the second window, in the same way as shown. And it runs correctly.
I suspect that in my project something is wrong.
And the entry point of the window that i'm trying to display is not well defined, but i'm not able what exaclty is wrong.
Build -> Configuration Manager -> Acitve solution platform -> New -> x86 (instead of x64) This helped to me.

Get opened Forms-Windows in WPF-Application

I want to get all opened windows (WPF) and forms (WF) of my Mixed-application.
There is a method called Application.Current.Windows ... but its only returning the WPF Windows.
Note: The Windows Forms are stored in an other project (DLL) in the solution.
You can access Windows Forms' open forms via the System.Windows.Forms.Application.OpenForms static property. The property is contained in the System.Windows.Forms.dll assembly, so you will need a reference to it.
(IIRC, this property only returns the forms that were created on the current thread; therefore, if you have two separate UI threads (one for Windows Forms, one for WPF), I believe you need to call the property on Windows Forms' UI thread.)
Check this msdn
Hope you find what your looking for.
it also has example.
http://msdn.microsoft.com/en-us/library/microsoft.visualbasic.applicationservices.windowsformsapplicationbase.openforms%28v=vs.110%29.aspx
For this you have to use
foreach (Window win in System.Windows.Application.Current.Windows)
{
win..title;
}

Get Application that runs a Windows Form Control

I'm making an Autocad Plugin which runs fine using Windows Forms
And I've created a user control (winforms) to replicate in any form I like in the future.
The question is
From the control's code, how do I get the instance of the application running that control?
(Probably a pure winforms problem)
Coditions:
Within the plug-in I can get the Autocad Application instance with no
problem.
This user control is meant to be in a separate assembly (dll) to be referenced in the plug-in application, so it has no direct access to the application instance.
A little explanation about the scenario:
There's a Main Assembly being run by Autocad as a plug-in.
That assembly has the Autocad application instantiated.
Now I have some useful form controls to work with Autocad, and they are in a separate assembly. (That's because I want to use them in as many different plug-ins I like).
So, Autocad runs main assembly, and main assembly runs controls of the separate assembly.
In order to work properly, those controls need to have access to the Autocad application wich is running the main assembly.
Today I use the application as a property in the controls, wich I must set before working with them. (If I forget to set that, exceptions are raised). Since I cannot have a control with a creator taking parameters.
I want the controls to detect their running application so I avoid that workaround.
Please see the following code
public class MyCommands {
[CommandMethod("NS", "TEST", "TEST", CommandFlags.Modal)]
public void TestCommand() // This method can have any name
{
Form fromAutoCADAPI = new TestForm();
Form independent1 = new TestForm();
Form independent2 = new TestForm();
//Using AutoCAD application
Autodesk.AutoCAD.ApplicationServices.Application.ShowModelessDialog(fromAutoCADAPI);
independent1.Show();
independent2.Show();
//Using Windows Forms Application
var count = System.Windows.Forms.Application.OpenForms.Count; //should be 3
Autodesk.AutoCAD.ApplicationServices.Application.ShowAlertDialog(count.ToString());
}
}
If this is what you already know, then may be you should paste some sample code that will help understand where specifically you are stuck in your code. This is the way I have used AutoCAD application and Windows Forms application. In case you wan't to grab WPF application then you can use
var application = System.Windows.Application.Current;

Categories