我正在使用Silverlight 5 webbrowser控件来显示内容。我已经使用了这个对我来说很好的代码。
的Xaml:
<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);
}
}
禁用按钮并搜索TextBox输入的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事件未触发。
这两者背后的区别是什么。
请帮助!