WPF中嵌入WinForm中的webbrowser控制項

來源:互聯網
上載者:User

標籤:winform   class   blog   http   tar   ext   

原文:WPF中嵌入WinForm中的webbrowser控制項

使用VS2008建立WPF應用程式,需使用webbrowser。從工具箱中添加WPF組件中的webbrowser發現其中有很多屬性事件不能使用。決定還是使用WinForm中的webbrowser。要想在WPF中使用WinForm控制項,查看MSDN,需經過以下步驟。

  1. 建立名為 HostingWfInWpf 的 WPF 應用程式項目。

  2. 在方案總管中,添加一個對名為 WindowsFormsIntegration.dll 的 WindowsFormsIntegration 程式集的引用。

  3. 在方案總管中,添加一個對名為 System.Windows.Forms.dll 的 Windows 表單程式集的引用。

  4. 在 WPF 設計器中開啟 Window1.xaml。

  5. 用以下 XAML 替換 Window1.xaml 中自動產生的 XAML。

<Window x:Class="HostingWfInWpf.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="HostingWfInWpf"
 Loaded="WindowLoaded"
    >
    <Grid Name="grid1">

    </Grid>
</Window>
6.在代碼編輯器中開啟 Window1.xaml.cs。
7.將 Window1.xaml.cs 中的代碼替換為以下代碼。

 

using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Media;
using System.Windows.Shapes;

using System.Windows.Forms;

namespace HostingWfInWpf
{  
    public partial class Window1 : Window
    {
        public Window1()
        {
            InitializeComponent();
        }

        private void WindowLoaded(object sender, RoutedEventArgs e)
        {
            // Create the interop host control.
            System.Windows.Forms.Integration.WindowsFormsHost host =
                new System.Windows.Forms.Integration.WindowsFormsHost();

            // Create the MaskedTextBox control.
            MaskedTextBox mtbDate = new MaskedTextBox("00/00/0000");

            // Assign the MaskedTextBox control as the host control‘s child.
            host.Child = mtbDate;

            // Add the interop host control to the Grid
            // control‘s collection of child controls.
            this.grid1.Children.Add(host);
        }
    }
}

此處,將MaskedTextBox換成System.Windows.Forms.WebBrowser即可。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.