【问题标题】:'WindowsFormsHost' was not found找不到“WindowsFormsHost”
【发布时间】:2014-07-11 16:12:30
【问题描述】:

在我的 wpf 应用程序中,我尝试使用 Windows 窗体控件....但我收到一个错误,即 错误找不到类型“WindowsFormsHost”。确认您没有丢失程序集引用并且所有引用的程序集都已构建。 谁能帮我完成它...以下是我的代码

       c#:code

     public Window2()
    {
        InitializeComponent();
        System.Windows.Forms.PictureBox PictureBox1 = new System.Windows.Forms.PictureBox();
        windowsFormsHost1.Child = PictureBox1;
        PictureBox1.Paint += new System.Windows.Forms.PaintEventHandler(PictureBox1_Paint);


    }
   xaml:code


         <WindowsFormsHost Height="175" HorizontalAlignment="Left" Margin="10,10,0,0" Name="windowsFormsHost1" VerticalAlignment="Top" Width="255" />

【问题讨论】:

  • 你确定你已经包含了这个xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  • 是的,我添加了这些引用...
  • 你用过WindowsFormsIntegration ref 吗?
  • 没有。如何添加此引用?我应该将此 WindowsFormsIntegration.dll 添加到我的应用程序吗?

标签: c# wpf


【解决方案1】:

通常当我们看到错误提示时:

找不到类型SomeClass。确认您没有丢失程序集引用并且所有引用的程序集都已构建

可能存在一些问题。这意味着我们没有将相关 dll 的引用添加到我们的项目中,或者我们没有正确导入命名空间。

要查找我们需要添加引用的 dll,我们通常会转到 MSDN,使用搜索词“SomeClassclass”搜索...在您的情况下为“WindowsFormsHostclass”。为WindowsFormsHost 类执行此操作,我们看到:

注意上面写着的那一行:

程序集:WindowsFormsIntegration(在 WindowsFormsIntegration.dll 中)

查看 Visual Studio 中的 添加引用 对话框,我们可以看到 WindowsFormsIntegration 的 dll 条目:

这就是我们找出要导入哪个 dll 的方法。现在,我们只需要确保在 C# 中正确导入命名空间:

using System.Windows.Forms.Integration;

在 XAML 中,您无需在使用 WindowsFormsHost 控件之前为 WindowsFormsIntegration dll 添加 XML 命名空间。

<WindowsFormsHost>
    <!-- Some WinForms Control -->
</WindowsFormsHost>

有关详细信息,请参阅 MSDN 上的WindowsFormsHost Class 页面。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-02-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多