1

我正在使用 Silverlight 5 webbrowser 控件来显示内容。我使用了这段代码,对我来说效果很好。

xml:

<TextBox x:Name="txtUri" Margin="5" HorizontalAlignment="Stretch" />
<Button x:Name="btnGo" Grid.Column="1" Content="Go" Click="OnGo" Margin="5" />
<WebBrowser LoadCompleted="browserControl_LoadCompleted"  ScriptNotify="browserControl_ScriptNotify" Grid.Row="1" Margin="5" x:Name="browserControl" />

。CS

private void webBrowser_LoadCompleted(object sender, System.Windows.Navigation.NavigationEventArgs e)
    {
        MessageBox.Show("called");
    }

    private void browserControl_LoadCompleted(object sender, System.Windows.Navigation.NavigationEventArgs e)
    {
        MessageBox.Show("called");
        btnGo.IsEnabled = true;
    }

    private void OnGo(object sender, RoutedEventArgs e)
    {
         btnGo.IsEnabled = false;
         browserControl.Navigate(new Uri(txtUri.Text, UriKind.RelativeOrAbsolute));

    }

    private void browserControl_ScriptNotify(object sender, NotifyEventArgs e)
    {
        if (!string.IsNullOrEmpty(e.Value))
        {
            MessageBox.Show(e.Value);
        }
    }

它禁用按钮并搜索文本框输入的 URI 但是当我更改 OnGo 方法时

private void OnGo(object sender, RoutedEventArgs e)
    {
         btnGo.IsEnabled = false;
         browserControl.NavigateToString("<html>   <head><title>Sample Readme File</title><script type='text/javascript' language='javascript'> function GetSelectedText() {window.external.notify('Text to show in Message box');}</script></head>   <body>    <a onclick='GetSelectedText()'>Click Me</a>  <p>Sample Readme Content</p>   </body></html>");
    }

并添加 ScriptNotify 事件处理程序

void webBrowser_ScriptNotify(object sender, NotifyEventArgs e)
    {
        if (!string.IsNullOrEmpty(e.Value))
        {
            MessageBox.Show(e.Value);
        }
    }

然后 webBrowser_LoadCompleted 事件不会触发。

这两者背后有什么区别。

请帮忙!

4

0 回答 0