HOW TO:在 .NET Compact Framework 中使用 WebBrowser 控制項

更新:2007 年 11 月

.NET Compact Framework 支援 Windows Form WebBrowser 控制項的核心功能。下列成員需要 Windows Mobile for Pocket PC 5.0 版或 Smartphone 軟體。這份清單會隨時變更。

下列考量只適用於 Windows Mobile 2003 for Pocket PC 和 Windows Mobile 2003 for Smartphone:

在不是執行 Microsoft Windows CE 5.0 的 Pocket PC 或 Smartphone 裝置上,NavigatedDocumentCompleted 事件都會在造訪新的 URL 時發生兩次。Windows CE 未來版本計劃修正這個錯誤。

您可以建立 WebBrowser 執行個體回應 HelpRequested 事件,以顯示應用程式的線上 [說明] 主題。

.NET Compact Framework 不支援 Document 屬性及相關屬性和事件 (DocumentText 屬性除外)。您可以使用 DocumentText 向使用者展示 HTML,例如提供連結和簡單的 HTML 表單,但是 .NET Compact Framework 並不支援使用這個屬性存取 Web 網頁的 HTML 內容。

也可以在處理 Navigating 事件的程式碼中使用 WebBrowserDocumentCompletedEventArgs.Url 屬性,來決定對於表單的回應。下列的第一個程序中提供了程式碼範例。

但無法在 Smartphone 應用程式中離開 WebBrowser 控制項。解決方法是偵測按鍵事件,並將焦點設定到另一個控制項。下列的第二個程序中提供了程式碼範例。

注意事項:

這個解決方法需要 Windows Mobile 5.0 或 .NET Compact Framework 3.5。

如需使用 WebBrowser 控制項的一般資訊,請參閱 HOW TO:將 Web 瀏覽器功能加入至 Windows Form 應用程式

若要從內嵌的 HTML 控制項收集資訊

  1. 使用 DocumentText 屬性在 WebBrowser 控制項中顯示 HTML。這份 HTML 含有表單,該表單具有連結和指定 URL 的文字方塊。

    Dim sb As New StringBuilder()
    sb.Append("<html><body>")
    sb.Append("<a href=")
    sb.Append("""")
    sb.Append("https://www.microsoft.com")
    sb.Append("""")
    sb.Append(">Microsoft</a><p>")
    sb.Append("Specify a URL:<br>")
    sb.Append("<form action=''><input type='text' name='address'/>")
    sb.Append("<br><input type='submit'>")
    sb.Append("</form></body></html>")
    webBrowser1.DocumentText = sb.ToString()
    
    StringBuilder sb = new StringBuilder();
    sb.Append("<html><body>");
    sb.Append("<a href=");
    sb.Append("\"");
    sb.Append("https://www.microsoft.com");
    sb.Append("\"");
    sb.Append(">Microsoft</a><p>");
    sb.Append("Specify a URL:<br>");
    sb.Append("<form action=''><input type='text' name='address'/>");
    sb.Append("<br><input type='submit'>");
    sb.Append("</form></body></html>");
    webBrowser1.DocumentText = sb.ToString();
    
  2. 使用 Navigating 事件判斷 URL 是否含有來自表單的回應。如果肯定的話,便會巡覽至該 URL。

    Private Sub webBrowser1_Navigating(ByVal sender As Object, ByVal e As WebBrowserNavigatingEventArgs)  Handles webBrowser1.Navigating
        Dim x As Integer
        '  The URL contains the results of the
        '  HTML form following the equals sign.
        x = e.Url.ToString().LastIndexOf("=")
        If x <> - 1 Then
            Dim Redirect As String
            Redirect = e.Url.ToString().Substring((x + 1))
            If Redirect <> "" Then
                ' Error handling code omitted in this example.
                ' Uri constructor throws a UriFormatException if there's
                ' an error.
                webBrowser1.Navigate(New Uri(Redirect))
            Else
                MessageBox.Show("Specify a URL")
            End If
        End If
    
    End Sub    
    
    private void webBrowser1_Navigating(object sender, WebBrowserNavigatingEventArgs e)
     {
         int x;
         //  The URL contains the results of the
         //  HTML form following the equals sign.
         x = e.Url.ToString().LastIndexOf("=");
         if (x != -1)
         {
             string Redirect;
             Redirect = e.Url.ToString().Substring(x + 1);
             if (Redirect != "")
             {
                 // Error handling code omitted in this example.
                 // Uri constructor throws a UriFormatException if there's
                 // an error.
                 webBrowser1.Navigate(new Uri(Redirect));
             }
             else
             {
                 MessageBox.Show("Specify a URL");
             }
         }
     }
    

    上一個 Visual Basic 程式碼範例中,Navigating 事件處理常式已和控制項產生關聯。在 C# 中宣告此事件處理常式如下:

      this.webBrowser1.Navigating += new System.Windows.Forms.WebBrowserNavigatingEventHandler(this.webBrowser1_Navigating);
    

若要在 Smartphone 上離開 WebBrowser

  • 下列程式碼範例會在對巡覽鍵按向上時,將焦點設定到另一個控制項。

    WebBrowser 控制項使用 Microsoft Pocket Internet Explorer 的定位停駐邏輯,讓使用者在控制項所顯示的網站上,巡覽至不同的連結和內嵌控制項。您可以使用 KeyPreview 屬性來覆寫預設的定位停駐行為。

    下列程式碼範例會假設在表單的建構函式,或是為表單處理 Load 事件的程式碼中,已經將 KeyPreview 設定為 true。

    Protected Overrides Sub OnKeyDown(ByVal keyg As KeyEventArgs) 
        If keyg.KeyCode = Keys.Up Then
            textBox1.Focus()
        End If
        MyBase.OnKeyDown(keyg)
    
    protected override void OnKeyDown(KeyEventArgs keyg)
    {
        if (keyg.KeyCode == Keys.Up)
        {
            textBox1.Focus();
        }
        base.OnKeyDown(keyg);
    }
    

編譯程式碼

此範例需要下列命名空間的參考:

請參閱

工作

HOW TO:將 Web 瀏覽器功能加入至 Windows Form 應用程式

概念

.NET Compact Framework HOW TO 主題