Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Open WPF window in WindowsForm APP [duplicate]

Tags:

c#

winforms

wpf

I added to my WindowsForm app a new WPF window called novoLogin.

After adding it, I added the system.xaml reference....debug fine.

Now I'm trying to open this new window from the existing windowsForm.

novoLogin nl = new novoLogin();
nl.show();

The compiler is giving this error:

Error 1 'WindowsFormsApplication1.novoLogin' does not contain a definition for 'show' and no extension method 'show' accepting a first argument of type 'WindowsFormsApplication1.novoLogin' could be found (are you missing a using directive or an assembly reference?)

like image 910
Michel Avatar asked Nov 29 '11 13:11

Michel


3 Answers

This brief article explains how you can achieve this.

If you find yourself in need to open a WPF Window from a WinForms program, this is one way to do it (works for me):

  1. Create/Add a new project of type WPF Custom Control Library
  2. Add a new Item of type Window (WPF)
  3. Do your thing with the WPF Window
  4. From your WinForms app, create and open the WPF Window

    using System;  
    using System.Windows.Forms;  
    using System.Windows.Forms.Integration;  
    
    var wpfwindow = new WPFWindow.Window1(); 
    ElementHost.EnableModelessKeyboardInterop(wpfwindow); 
    wpfwindow.Show();
    
like image 187
Abbas Avatar answered Sep 29 '22 02:09

Abbas


I wanted to show wpf form in windowForm and there was some resource problem...

(because I used resources..). Finally I used this code in my windowsForm project:

First create a global instance of your app class like this :

WPFTest.App app;

why this is global?

because this class is singleton and you can not create more than one instance in the same AppDomain

Now for example you have a button event to show wpf form. At the button event we have :

    private void button1_Click(object sender, EventArgs e)
    {
        if (System.Windows.Application.Current == null)
        {
            app = new WPFTest.App()
            {
                ShutdownMode = ShutdownMode.OnExplicitShutdown
            };
            app.InitializeComponent();
        }
        else
        {
            app = (WPFTest.App)System.Windows.Application.Current;
            app.MainWindow = new WPFTest.YourWindow();
            System.Windows.Forms.Integration.ElementHost.EnableModelessKeyboardInterop(app.MainWindow);
            app.MainWindow.Show();
        }
    }

note : WPFTest is name of your project and YourWindow() is window that you wanna to show

like image 24
Mohammad Galouz Lee Avatar answered Sep 29 '22 03:09

Mohammad Galouz Lee


Have a look to this: http://www.mobilemotion.eu/?p=1537&lang=en

Summary:

Open the project’s manifest file (the one with the .csproj or .vbproj extension) in any text editor. The top node usually contains several tags, one for each build configuration and a global one. In the global node (the one without Condition attribute), search for the sub-node or create one if it does not exist. This node should contain two GUIDs: FAE04EC0-301F-11D3-BF4B-00C04F79EFBC, which stands for a C# project, and 60dc8134-eba5-43b8-bcc9-bb4bc16c2548 which stands for WPF. The full line should look as follows:

<ProjectTypeGuids>{60dc8134-eba5-43b8-bcc9-bb4bc16c2548};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}</ProjectTypeGuids>

(If you’re interested in details, codeproject holds a complete list of potential project GUIDs: http://www.codeproject.com/Reference/720512/List-of-Visual-Studio-Project-Type-GUIDs)

Reload the project in Visual Studio, and open the Add New Item wizard.

Since the project is now officially classified as WPF project, this wizard should now contain the WPF window option. By the way, since there is no WinForms project GUID that could be overwritten, this approach does not harm the existing project components.

I just tried this approach for a VB.NET project and it works!

Using VB.NET obviously you have to edit above lines substituting the GUID from {FAE04EC0-301F-11D3-BF4B-00C04F79EFBC} to {F184B08F-C81C-45F6-A57F-5ABD9991F28F}

like image 25
Andrea Antonangeli Avatar answered Sep 29 '22 01:09

Andrea Antonangeli