如何:在 .NET Compact Framework 中使用 WebBrowser 控件

更新:2007 年 11 月

.NET Compact Framework 支持 Windows 窗体 WebBrowser 控件的核心函数。以下成员需要使用用于 Pocket PC 或 Smartphone 的 Windows Mobile 5.0 版软件。此列表有可能会发生更改。

以下注意事项仅适用于 Windows Mobile 2003 for Pocket PC 和 Windows Mobile 2003 for Smartphone:

在除运行 Microsoft Windows CE 5.0 的 Pocket PC 或 Smartphone 以外的其他设备上,在访问一个新的 URL 时,NavigatedDocumentCompleted 这两个事件都会发生两次。我们计划在 Windows CE 的未来发行版本中纠正此错误。

您可以创建一个 WebBrowser 实例来响应 HelpRequested 事件,为您的应用程序显示联机帮助主题。

.NET Compact Framework 不支持除 DocumentText 属性以外的 Document 属性及其相关的属性和事件。您可以使用 DocumentText 向用户呈现 HTML,例如提供链接和简单的 HTML 窗体,但是 .NET Compact Framework 不支持使用此属性来访问网页的 HTML 内容。

您可以使用处理 Navigating 事件的代码中的 WebBrowserDocumentCompletedEventArgs.Url 属性来确定对窗体的响应。随后的第一个过程中提供了一个代码示例。

在 Smartphone 应用程序中,您不能定位到 WebBrowser 控件之外。若要解决此问题,您可以检测一个按键事件并将焦点置于另一控件上。随后的第二个过程中提供了一个代码示例。

说明:

此解决方法需要 Windows Mobile 5.0 或 .NET Compact Framework 3.5。

有关使用 WebBrowser 控件的一般信息,请参见如何:将 Web 浏览器功能添加到 Windows 窗体应用程序

收集来自嵌入式 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 之外

  • 下面的代码示例会在导航键上的 UP 被按下时将焦点置于另一个控件上。

    WebBrowser 控件使用 Microsoft Pocket Internet Explorer 中的 Tab 键导航逻辑来允许用户定位到由控件显示的网站上的不同链接和嵌入式控件。您可以通过使用 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);
    }
    

编译代码

此示例需要引用下面的命名空间:

请参见

任务

如何:将 Web 浏览器功能添加到 Windows 窗体应用程序

概念

.NET Compact Framework 帮助主题