Silverlight 4:Web特性大显身手

Silverlight 4版本中加入了一系列令人兴奋的新特性,WebBrowser便是其中之一。WebBrowser是Silverlight 4版本中加入的一个运行在Silverlight应用程序内部的浏览器控件,这是一个非常有用的控件。当我们的Silverlight应用程序需要在某个位置显示一些HTML内容或是一个网页的时候,WebBrowser就会大显身手。

另一方面,通过在Silverlight应用程序嵌入WebBrowser控件的方法可以弥补Silverlight应用程序不能显示HTML网页的不足。本文中,我们将通过构建一个简化版本的Silverlight浏览器来介绍如何在Silverlight 4应用程序使用WebBrowser控件。之后,我们还要深入探讨这个控件的其他功能,力争全面了解WebBrowser控件在新一代Silverlight RIA应用程序开发下可能蕴藏的巨大能量。

在开始构建实例之前,我们还要明确:WebBrowser控件只能在浏览器外(Out-of-Browser)Silverlight应用程序中使用。

一、创建示例工程

启动Visual Studio 2010,创建一个普通的Silverlight 4应用程序,并命名为SL4WebBrowser。此后,系统会自动开启主程序界面MainPage.xaml。

现在,我们把一个WebBrowser控件拖动到上述主界面中。系统出现如下图所示的提示。

图中提示的意思是,WebBrowser控件仅能运行于浏览器外(以Out-of-Browser模式运行)。

二、修改工程属性

为了支持浏览器外运行功能,我们需要修改Silverlight应用程序的属性。为此,点击菜单“Project|SL4WebBrowser Properties…”,随后弹出如图所示的Silverlight工程属性设置对话框。

勾选图中的“Enable running application out of the browser”选项,并单击按钮“Out-of-Browser Settings…”。

注意,为了简单起见,可选择“Require elevated trust when running outside the browser”选项。这样一来,我们就能够使用WebClient类或WebBrowser控件导航到任意网址,而不必考虑Silverlight应用程序限制的跨域调用的安全问题。如果该复选框未选中,我们只能打开我们本地服务器上的网页。

三、后台代码编程

现在,我们简单地看一下后台代码中需要的编码内容:

以下是代码片段:

 
 
  1. public MainPage()
  2. {
  3. InitializeComponent();
  4. webBrowser1.Width = Application.Current.MainWindow.Width;
  5. webBrowser1.Height = Application.Current.MainWindow.Height;
  6. if (App.Current.IsRunningOutOfBrowser)
  7. {
  8. webBrowser1.Source = new Uri("http://space.itpub.net/14466241/");
  9. }
  10. }

非常简洁明了吧。下图给出了上面示例的运行时快照。

显然,上图中页面不是运行于浏览器中,而是以浏览器外方式运行的。

四、WebBrowser控件其他重要成员剖析

现在我们更深入地追踪分析WebBrowser控件提供的其他一些重要成员的使用情况。

WebBrowser控件提供了如下一些重要方法及属性,其各自的含义如下:

1. Source属性:获取或设置要显示的HTML内容对应的URI。

2. Navigate方法:要加载的HTML内容,可以是来自于一个跨域的位置。

3. NavigateToString方法:以文本方式显示要加载的HTML内容。

4. SaveToString方法:以文本方式保存在此控件中加载的HTML内容,可以是来自于一个跨域的位置。

请特别注意,出于安全原因,在使用WebBrowser控件时请务必确保与NavigateToString方法一起显示的HTML来自于受信任源。

最后,我们来观察一下WebBrowser控件的完整定义代码:

以下是代码片段:

 
 
  1. namespace System.Windows.Controls
  2. {
  3. public sealed class WebBrowser : FrameworkElement
  4. {
  5. // Summary:
  6. // Initializes a new instance of the System.Windows.Controls.WebBrowser class.
  7. public WebBrowser();
  8. // Summary:
  9. // Gets or sets the URI source of the HTML content to display in the System.Windows.Controls.WebBrowser
  10. // control.
  11. //
  12. // Returns:
  13. // The URI source of the HTML content to display in the System.Windows.Controls.WebBrowser
  14. // control.
  15. public Uri Source { get; set; }
  16. // Summary:
  17. // Occurs when top-level navigation completes and the content loads into the
  18. // System.Windows.Controls.WebBrowser control or when an error occurs during
  19. // loading.
  20. public event LoadCompletedEventHandler LoadCompleted;
  21. //
  22. // Summary:
  23. // Occurs when the content contained in the System.Windows.Controls.WebBrowser
  24. // control passes a string to the Silverlight plug-in by using JavaScript.
  25. public event EventHandler<NotifyEventArgs> ScriptNotify;
  26. // Summary:
  27. // Executes the specified script, which is defined in the currently loaded HTML.
  28. //
  29. // Parameters:
  30. // scriptName:
  31. // The name of the script to execute.
  32. //
  33. // Returns:
  34. // The result of the script invocation.
  35. //
  36. // Exceptions:
  37. // System.Security.SecurityException:
  38. // The script target is at a cross-domain location.
  39. public object InvokeScript(string scriptName);
  40. //
  41. // Summary:
  42. // Executes the specified script function, which is defined in the currently
  43. // loaded HTML, with the specified arguments.
  44. //
  45. // Parameters:
  46. // scriptName:
  47. // The name of the script to execute.
  48. //
  49. // args:
  50. // The arguments to pass to the script function.
  51. //
  52. // Returns:
  53. // The result of the script invocation.
  54. //
  55. // Exceptions:
  56. // System.Security.SecurityException:
  57. // The script target is at a cross-domain location.
  58. public object InvokeScript(string scriptName, params string[] args);
  59. //
  60. // Summary:
  61. // Loads the HTML content at the specified URI.
  62. //
  63. // Parameters:
  64. // source:
  65. // The URI of the HTML content to load.
  66. //
  67. // Exceptions:
  68. // System.Security.SecurityException:
  69. // The HTML content to load is from a cross-domain location.
  70. public void Navigate(Uri source);
  71. //
  72. // Summary:
  73. // Displays the specified HTML content.
  74. //
  75. // Parameters:
  76. // text:
  77. // The HTML content to display in the System.Windows.Controls.WebBrowser control.
  78. public void NavigateToString(string text);
  79. //
  80. // Summary:
  81. // Saves the source for the HTML content currently displayed in the System.Windows.Controls.WebBrowser
  82. // as a string.
  83. //
  84. // Returns:
  85. // The string representation of the source for the currently displayed HTML
  86. // content.
  87. //
  88. // Exceptions:
  89. // System.Security.SecurityException:
  90. // The HTML content to be saved is from a cross-domain location.
  91. public string SaveToString();
  92. }
  93. }

五、InvokeScript方法及ScriptNotify事件应用举例

根据前面的控件定义,我们知道:InvokeScript方法能够执行在当前加载的HTML中定义的指定脚本。

根据MSDN指示,如果对InvokeScript的调用加载跨域内容,我们可以不再用InvokeScript与该内容交互。而且,出于安全原因,你不能在

<iframe>

中承载的脚本目标上调用此方法。

InvokeScript方法提供了如下两种重载形式:

(1)public object InvokeScript(string scriptName);

(2)public object InvokeScript(string scriptName, params string[] args);

在下面的示例中,调用了InvokeScript,它在以下HTML中反过来调用LoadSearch函数。注意,这个HTML文件必须与Silverlight应用程序承载在同一个域中。

你可以使用Window.external对象的Notify方法从HTML中的JavaScript传输到托管代码。在发生此情况时,将触发ScriptNotify事件。

以下是代码片段:

 
 
  1. <html xmlns="http://www.w3.org/1999/xhtml" >
  2. <head>
  3. <title></title>
  4. <script type="text/javascript" >
  5. function LoadSearch(searchString) {
  6. window.location = "http://www.bing.com/search?q=" + searchString
  7. window.external.notify("Search completed")
  8. }
  9. </script>
  10. </head>
  11. <body>
  12. Silverlight WebBrowser control。
  13. </body>
  14. </html>

现在,让我们来观察一下客户端后台代码:

以下是代码片段:

 
 
  1. public partial class MainPage : UserControl
  2. {
  3. public MainPage()
  4. {
  5. InitializeComponent();
  6. }
  7. void WB1_ScriptNotify(object sender, NotifyEventArgs e)
  8. {
  9. Button1.Content = e.Value;
  10. Button1.IsEnabled = false;
  11. }
  12. private void Button1_Click(object sender, RoutedEventArgs e)
  13. {
  14. object results = WB1.InvokeScript("LoadSearch", new string[] { "Silverlight" });
  15. }
  16. }

接下来,让我们再来观察一下客户端Silverlight 4 XAML脚本:

以下是代码片段:

 
 
  1. <StackPanel x:Name="LayoutRoot" Height="358" Width="489" Background="LightBlue">
  2. <WebBrowser x:Name="WB1" Source="http://localhost/HTMLPage1.htm" Height="272" Width="379"
  3. ScriptNotify="WB1_ScriptNotify" Margin="5" />
  4. <Button Width="200" x:Name="Button1" Content="Click to search!" Click="Button1_Click" />
  5. </StackPanel>

归纳来看,InvokeScript方法及ScriptNotify事件联手提供了一种从WebBrowser控件中与其所承载的HTML中的JavaScript代码进行的一种重要途径。

六、联手使用WebBrowser和WebBrowserBrush控件

WebBrowser和WebBrowserBrush控件设计为一起使用,以便在浏览器外运行的Silverlight应用程序中显示丰富的HTML内容。

你不能旋转、应用效果或创建部分透明的WebBrowser控件。此外,不能使用WebBrowser控件覆盖HTML上的Silverlight内容。对于这些情况,应该使用WebBrowserBrush。

(一)、WebBrowserBrush简介

WebBrowserBrush是Brush对象的类型,该对象类似于VideoBrush。但是,该对象使用HTML内容而不是视频内容来绘制区域。此HTML内容由WebBrowser控件提供。与其他画笔类型类似,你可以使用WebBrowserBrush来绘制以下内容:

• 形状(例如Rectangle)的填充。

• Path的几何内容。

• Canvas的背景色。

• extBlock的前景色。

与其他类型的画笔不同,你必须通过调用Redraw方法,手动更新WebBrowserBrush中的内容。

为了使用WebBrowserBrush,你需要事先创建WebBrowser控件并且设置其Source属性。然后,你可以将WebBrowserBrush应用于要绘制的对象,并且将该WebBrowserBrush对象的SourceName属性设置为你创建的WebBrowser的名称。

如果WebBrowser中的内容有更改,你必须调用Redraw方法更新WebBrowserBrush。此外,用户无法与通过WebBrowserBrush一起显示的内容交互。下面的示例说明如何显示具有WebBrowserBrush的内容。在这个示例中,在用户移动鼠标时,将更新WebBrowserBrush的内容。

以下是代码片段:

 
 
  1. privatevoidLayoutRoot_MouseMove(object sender, MouseEventArgs e)
  2. {
  3. WBB1.Redraw();
  4. }

其中,XAML标记代码如下所示:

以下是代码片段:

 
 
  1. <StackPanel x:Name="LayoutRoot" Background="Blue" MouseMove="LayoutRoot_MouseMove">
  2. <WebBrowser Name="WB1" Source="Hello.html" Height="150" Width="150" />
  3. <Rectangle Height="150" Width="150" >
  4. <Rectangle.Fill>
  5. <WebBrowserBrush SourceName="WB1" Opacity=".5" x:Name="WBB1"/>
  6. </Rectangle.Fill>
  7. </Rectangle>
  8. </StackPanel>

通常,在你需要显示旋转、应用效果或与HTML内容交互时,一起使用WebBrowser和WebBrowserBrush控件将具有良好的效果。

(二)、WebBrowser和WebBrowserBrush联合应用例二

有时,你想在于浏览器之外运行的Silverlight应用程序中显示HTML内容。你可能想对HTML内容旋转或应用效果,但仍使用户能够与内容交互。为此,你可以将WebBrowser和WebBrowserBrush控件添加到你的应用程序以及在两者之间进行交换,具体取决于用户是否正在与内容交互。下面的代码示例演示如何使用WebBrowser和WebBrowserBrush控件。

此代码示例包含一个名为WB1的WebBrowser控件和一个名为htmlBrush的WebBrowserBrush控件。WebBrowser的源属性设置为http://www.bing.com。反过来,WebBrowserBrush的源设置为WebBrowser控件。

最初,WebBrowserBrush控件处于隐藏状态,而WebBrowser处于显示状态。当用户移动鼠标时,将隐藏WebBrowser而显示WebBrowserBrush,此时将对画笔应用转换。当用户单击HTML内容以指示他们可能想要与HTML交互时,将再次显示WebBrowser并隐藏WebBrowserBrush。

WebBrowserBrush的内容定期更新,以便它与WebBrowser控件保持同步。以下示例使用DispatcherTimer每100毫秒对WebBrowserBrush调用一次Redraw。

为了测试本示例,你需要能够在浏览器之外运行应用程序。

示例程序后台代码如下所示:

以下是代码片段:

 
 
  1. namespace WebBrowserBrushEx{
  2. public partial class MainPage : UserControl{
  3. DispatcherTimer dt = new DispatcherTimer();
  4. public MainPage(){
  5. InitializeComponent();
  6. dt.Interval = new TimeSpan(100);
  7. dt.Tick += new EventHandler(dt_Tick);
  8. }
  9. void dt_Tick(object sender, EventArgs e) {
  10. htmlBrush.Redraw();
  11. }
  12. bool animating = false;
  13. private void LayoutRoot_MouseMove(object sender, MouseEventArgs e) {
  14. dt.Start();
  15. if (!animating) {
  16. WB1.Visibility = Visibility.Collapsed;
  17. brush.Visibility = Visibility.Visible;
  18. Spin.Begin();
  19. animating = true;
  20. }
  21. }
  22. private void brush_MouseLeftButtonDown(object sender, MouseButtonEventArgs e) {
  23. Spin.Stop();
  24. WB1.Visibility = Visibility.Visible;
  25. brush.Visibility = Visibility.Collapsed;
  26. animating = false;
  27. }
  28. }
  29. }

XAML声明性代码如下:

以下是代码片段:

 
 
  1. <UserControl x:Class="WebBrowserBrushEx.MainPage"
  2. <!--省略其他-->
  3. d:DesignHeight="400" d:DesignWidth="412">
  4. <Grid x:Name="LayoutRoot" Background="LightBlue" MouseMove="LayoutRoot_MouseMove" ShowGridLines="True">
  5. <Grid.RowDefinitions>
  6. <RowDefinition Height="40"/>
  7. <RowDefinition />
  8. </Grid.RowDefinitions>
  9. <Grid.Resources>
  10. <Storyboard x:Name="Spin" >
  11. <DoubleAnimation
  12. Storyboard.TargetName="myTransform"
  13. Storyboard.TargetProperty="Angle"
  14. From="0" To="360" Duration="0:0:5"
  15. RepeatBehavior="Forever" />
  16. </Storyboard>
  17. </Grid.Resources>
  18. <TextBlock Margin="5" Text="Right-click to install the out-of-browser application" />
  19. <WebBrowser Grid.Row="1" Visibility="Visible" Name="WB1" Height="350" Width="350" Source="http://www.bing.com" />
  20. <!—添加一个与WebBrowser控件同样大小的矩形-->
  21. <Rectangle Grid.Row="1" x:Name="brush" Width="350" Height="350" Visibility="Collapsed"
  22. MouseLeftButtonDown="brush_MouseLeftButtonDown" >
  23. <Rectangle.Fill>
  24. <!-- Fill (set background) as an HTML Brush -->
  25. <WebBrowserBrush x:Name="htmlBrush" SourceName="WB1"/>
  26. </Rectangle.Fill>
  27. <Rectangle.RenderTransform>
  28. <RotateTransform x:Name="myTransform" Angle="45" CenterX="175" CenterY="175" />
  29. </Rectangle.RenderTransform>
  30. </Rectangle>
  31. </Grid>
  32. </UserControl>

上面给出的仅仅是WebBrowser和WebBrowserBrush联合增强应用效果的基本示例,请结合上面列出的WebBrowserBrush的其他可能应用自行试验。

七、小结

WebBrowser是Silverlight 4版本中加入的一个运行在Silverlight应用程序内部的浏览器控件。当我们Silverlight应用程序需要在某个位置显示一些HTML内容或是一个网址网页的时候,借助于WebBrowser控件,实现类似功能将会得到极大简化。本文给出的仅仅是有关WebBrowser控件应用的基本示例,更复杂实用的案例需要你自己探讨开发。

原文链接:http://publish.itpub.net/a2010/0811/1089/000001089217.shtml

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值