【问题标题】:How to make winform run on WPF?如何让winform在WPF上运行?
【发布时间】:2011-03-22 07:13:22
【问题描述】:

我想在我的 wpf 应用程序上使用我的 winform - 可以吗?

【问题讨论】:

    标签: wpf winforms


    【解决方案1】:

    是的 - 这是可能的。

    使用 ElementHost 将 WPF 内容放入 Windows 窗体控件中, 还可以使用 WindowsFormsHost 在 WPF 元素中直接托管您的 Windows 窗体组件。

    【讨论】:

    • 谢谢你这是我需要的
    【解决方案2】:

    是的,WPF 应用程序可以承载 WinForm 控件和窗口。

    还有一种方法可以打包 WPF 控件以在 WinForm 中使用,但它涉及更多,绝对不推荐。

    大多数关于 WPF 的书籍都会教您如何托管 WinForm 控件。

    【讨论】:

      【解决方案3】:

      显示在 WPF 应用程序中托管 WinForms 浏览器控件(禁用快捷方式)的小示例:

      <Window x:Class="how_to_make_winform_run_on_wpf.MainWindow"
              xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
              xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
              xmlns:wf="clr-namespace:System.Windows.Forms;assembly=System.Windows.Forms"
              >
          <WindowsFormsHost>
              <wf:WebBrowser WebBrowserShortcutsEnabled="False" Url="http://stackoverflow.com"></wf:WebBrowser>
          </WindowsFormsHost>
      </Window>
      

      当然,您应该添加对 WinFormsIntegration 和 System.Windows.Forms 程序集的引用。 使用此示例时,您会注意到所有标准 WinForms 控件都使用平面样式。要为您的 WinForms 控件启用类似 WinXP 的样式,请将

      public App()
      {
          System.Windows.Forms.Application.EnableVisualStyles();
      }
      

      在您的 App.xaml.cs 中

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2021-08-12
        • 2016-10-16
        • 2020-09-04
        • 2014-02-08
        • 2018-10-11
        • 2016-06-06
        • 1970-01-01
        相关资源
        最近更新 更多