c# - Windows 窗体中的 WPF

标签 c# .net wpf winforms

大家好,

我正在开发基于 Windows 窗体 C# 模式的新项目。 我的软件中的一个窗口需要使用WPF来设计,有没有办法在Windows Forms项目中使用WPF窗口?

我的意思是,当我调用新窗口时,我使用以下代码:

Windows w = new Windows();
w.Show();

但是当我在 WPF 类上尝试它时,它向我抛出一个错误,表明它不受支持。

我可以用 WPF 窗口做类似的事情吗?

最佳答案

如果您发现自己需要从 WinForms 程序打开 WPF 窗口:

  1. 创建/添加“WPF 自定义控件库”类型的新项目
  2. 添加类型为“Window (WPF)”的新项目
  3. 使用 WPF 窗口完成您的任务
  4. 在您的 WinForms 应用中,创建并打开 WPF 窗口。

代码片段:

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

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

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

引用:
How to programmatically create a WPF window in a WinForm application
Open a WPF Window from WinForms & link form app with WPF app
How to add a WPF window to a WinForms App

关于c# - Windows 窗体中的 WPF,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18143414/

相关文章:

c# - 带有 EF6 和 dotConnect for oracle 的 aspnetcore 2.0

c# - C#相对于Python有什么优势

.net - XML注释intellisense中的RTL注释以相反的顺序显示单词

c# - 使用 VirtualFileDataObject 使用 IStream 拖放大型虚拟文件

c# - 如何在 WPF 自定义 Adorner 中用线条排列 Thumbs

c# - NGit rebase 创建新提交

c# - 为什么我在 React 应用程序中的 Fetch URL 没有改变?

c# - 如何用一定长度的空行初始化DataTable?

.net - Structuremap - 多个接口(interface)实现

wpf - 如何将控件绑定(bind)到 WPF ListView 列