我从一个项目中制作了自定义 winforms 控件,然后切换到 WPF。我引用了旧项目,但我的自定义控件没有出现在工具箱中,而且似乎没有“简单”的方法可以将它们添加到我的 winforms 主机中。我必须在代码中添加它们吗?
另外,如果我决定只用 wpf 重写代码,会有多困难/有什么不同?
请您参考如下方法:
因为 WPF 的工作方式与 WinForms 有很大不同,所以 WPF 中没有用于 WinForms 的交互式设计器。使用WindowsFormsHost
将 WinForms 控件放置在 WPF 窗口中。
您可以通过 XAML 执行此操作:
<Window x:Class="Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:wf="clr-namespace:My.Custom.Assembly;assembly=My.Custom.Assembly"
Title="HostingWfInWpf">
<Grid>
<WindowsFormsHost>
<wf:MyCustomControl />
</WindowsFormsHost>
</Grid>
</Window>
将 My.Custom.Assembly
替换为您自己的程序集,并将 MyCustomControl
替换为您的控件名称。
如果可能,为 WPF 重写控件将是最佳选择,因为它将允许 WPF 功能,例如硬件加速。