gpt4 book ai didi

c# - 在大型 WPF 项目中使用大型 WinForms 项目的一部分

转载 作者:太空宇宙 更新时间:2023-11-03 23:39:14 29 4
gpt4 key购买 nike

这两个应用程序都相当古老,并且已经由几个人构建和维护了好几年。目前,WinForms项目中使用的其中一个控件确实需要在WPF项目中显示。

我读过有关在 WPF 项目中使用 WinForms 控件的信息,并且在大多数情况下,如果您只是实例化一个常规的空 WinForm 控件,它似乎相对简单。

我想知道您如何最好地在另一个项目中使用大型项目​​的一部分?理想情况下,在发送并加载所需数据后,WinForm 控件将在我们的一个 WPF 控件中的一个选项卡上可见。

最佳答案

这里有一些一般准则。

从您的 WPF 应用程序中,将项目引用添加到:

  • 您的 WinForms 项目
  • WindowsFormsIntegration
  • System.Windows.Forms

修改您的 XAML 以包含 WindowsFormsHost :

<Window x:Class="WpfApplication1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Grid>
<TabControl>
<TabItem Header="Old Form">
<WindowsFormsHost Name="WinFormsHost"></WindowsFormsHost>
</TabItem>
</TabControl>
</Grid>
</Window>

实例化您的旧 Form 并将其设置为 WindowsFormsHost 的子项。将 TopLevel 设置为 false 否则它会提示“子控件不能是顶级窗体”。也更改 FormBorderStyle,以防止显示 Form 的标题栏并允许用户拖动 Form。

public MainWindow()
{
InitializeComponent();

WinFormsHost.Child =
new Form1 { TopLevel = false, FormBorderStyle = FormBorderStyle.None };
}

你最终得到这样的结果:

enter image description here

您可以在“Walkthrough: Hosting a Windows Forms Control in WPF”和“WindowsFormsHost Class”的 MSDN 文档中阅读更多内容。

关于c# - 在大型 WPF 项目中使用大型 WinForms 项目的一部分,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29682899/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com