我需要将 Windows 窗体用户控件 (Barcode_Scanner.cs) 添加到 WPF 视图 (MainWindow.xaml)
有没有一种简单的方法可以做到这一点?任何帮助,将不胜感激。
我需要将 Windows 窗体用户控件 (Barcode_Scanner.cs) 添加到 WPF 视图 (MainWindow.xaml)
有没有一种简单的方法可以做到这一点?任何帮助,将不胜感激。
您可以使用 WPF元素承载Windows.Forms
控件。WindowsFormsHost
例子:
<Window x:Class="WpfApplication10.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:winforms="clr-namespace:System.Windows.Forms;assembly=System.Windows.Forms"
xmlns:myControls="clr-namespace:MyContromNamespace;assembly=MyContromNamespace"
Title="MainWindow" Height="195" Width="191" Name="UI">
<Grid>
<WindowsFormsHost>
<winforms:Button Text="StackOverflow" />
</WindowsFormsHost>
<WindowsFormsHost>
<myControls:MyUserControl />
</WindowsFormsHost>
</Grid>
</Window>
结果:
你可以用WindowsFormsHost
所以我们需要做的第一件事(在创建一个新的 WPF 项目之后)是添加一些引用。您可以通过右键单击解决方案资源管理器中的引用文件夹并选择“添加引用”来执行此操作:
然后你会得到一个这样的对话框: