C# 从 WinForms 打开 WPF 窗口并使用 WPF 应用程序链接表单应用程序

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16511382/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 01:11:21  来源:igfitidea点击:

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

c#wpfwinformshyperlink

提问by Sarah

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 button in the Form1 once it's clicked, it opened - separated windows-.

我正在使用 Visual Studio 2012 & 我正在使用名为 Form1 的 Win Form 应用程序 & 添加一个名为“wpfWin”的新项目 wpf Window。单击后,我想通过 Form1 中的按钮打开 wpfWin,它会打开 - 分离的窗口 -。

I have tried weblogs.aspbut I haven't found the "WPF Custom Control Library" & once I skip it an error appeared. Is there any other way?!

我已经尝试过weblogs.asp,但我还没有找到“WPF 自定义控件库”,一旦我跳过它,就会出现错误。有没有别的办法?!

Also, how can I link Two applications one in WinForm & the other Wpf ?!

另外,如何链接 WinForm 中的两个应用程序和另一个 Wpf 中的应用程序?!

采纳答案by Nomad101

Open WPF window in WindowsForm APPso 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.

在 WindowsForm APP 中打开 WPF 窗口,所以您对上述链接的教程和问题有疑问?“WPF 自定义控件库”是您要在 Win-Forms 应用程序中使用的 WPF 窗口。

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".

只是一个简短的解释,因为它通过问题和教程解释了几次。您将要打开当前正在处理的 win-forms 项目。然后您将需要转到解决方案资源管理器并右键单击解决方案,而不是项目,转到“添加”并选择“新建项目”。

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.

在添加新项目中,转到 Visual C#,然后转到左侧树中的 Windows。它们应该位于已安装的子菜单中。在主要部分,您应该看到“WPF 自定义控件库”,单击它,然后将其命名为您想要的名称,然后单击确定。

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

在项目中添加一个 Window(WPF) 控件,这个窗口就是你要打开的 WPF 窗口。

Then from the WinForm, open it like so:

然后从 WinForm 中,像这样打开它:

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

However ensure you have the following using statements:

但是请确保您有以下 using 语句:

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:

您还需要添加一些引用以使其正常工作,这是您需要添加到 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.

您应该使用 VS 中的引用选择器将这些添加到您的 Win-forms 项目中,方法是右键单击解决方案资源管理器中的引用文件夹并添加一个新引用。所有引用都位于框架选项卡中,没有解决方案选项卡中的 WPF 控件。

回答by PinoyDev

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

尝试在您的项目中引用 windowbase.dll。还要确保“复制本地”属性设置为 true。

回答by F.Sun

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!

检查项目的 .NET Framework 版本也非常重要。您引用的 WPF 应用程序必须具有与根项目相同或更低的版本。花了我很多时间!