Open a WPF Window from WinForms & link form app with WPF app

前端 未结 3 508
忘了有多久
忘了有多久 2020-12-14 05:16

I\'m using Visual studio 2012 & I\'m using Win Form app called Form1 & add a new item which is wpf Window is called \"wpfWin\". I want to open the wpfWin through a b

相关标签:
3条回答
  • 2020-12-14 05:33

    It's also very important to check the .NET Framework version of your projects. Your referenced WPF application must have the same or lower version than your root project. took me a lot of time!

    0 讨论(0)
  • 2020-12-14 05:43

    Open WPF window in WindowsForm APP so you are having issues with the above linked tutorial and question? The "WPF Custom Control Library" is the WPF window that you want to use in your Win-Forms app.

    Just a brief explanation as it is explained a few times through the question and tutorial. You will want to open the win-forms project you are currently working on. Then you will want to go to Solution Explorer and right-click the SOLUTION, not the project, go to "Add" and select "New Project".

    In the add new Project go to Visual C# and then Windows in the tree on the left hand side. They should be located in the installed sub menu. In the main section you should see "WPF Custom Control Library" click on it and then name it what you would like and click ok.

    Add a Window(WPF) control to the project, this window would be the WPF window that you want to open.

    Then from the WinForm, open it like so:

    var wpfwindow = new WPFWindow.Window1();
    ElementHost.EnableModelessKeyboardInterop(wpfwindow);
    wpfwindow.Show();
    

    However ensure you have the following using statements:

    using System; //Given 
    using System.Windows.Forms; //Given
    using System.Windows.Forms.Integration; //Not so Given.
    

    You will need to add some references as well to make this work correctly, here is the list which should be all you need to add to a win-forms:

     PresentationCore
     PresentationFramework
     WindowsFormsIntegration
     WindowsBase
     System.Xaml
     YourWpfControlProjectName
    

    You should add these to your Win-forms project using the reference picker in VS by right-clicking the reference folder in the solution explorer and adding a new reference. All of the references are located in the Framework tab, sans your WPF control which is in the solution tab.

    0 讨论(0)
  • 2020-12-14 05:44

    Try referencing windowbase.dll in your project. Also ensure that the "Copy Local" property is set to true.

    0 讨论(0)
提交回复
热议问题