[源码下载]
作者: webabcd
介绍
Silverlight 4.0 控件一览:
在线DEMO
http://www.cnblogs.com/webabcd/archive/2010/08/09/1795417.html
示例
1、ViewBox 的 Demo
WebbrowserDemo.xaml
WebbrowserDemo.xaml.cs
相关的 JavaScript 部分
Silverlight40TestPage.html
2、WebbrowserBrush 的 Demo
WebbrowserBrushDemo.xaml
WebbrowserBrushDemo.xaml.cs
OK
[源码下载]
作者: webabcd
介绍
Silverlight 4.0 控件一览:
- Webbrowser - 在 Silverlight 应用程序中显示 HTML 内容(只能在 OOB 模式下运行)
- WebbrowserBrush - 一个 Webbrowser 类型的画笔(只能在 OOB 模式下运行)
在线DEMO
http://www.cnblogs.com/webabcd/archive/2010/08/09/1795417.html
示例
1、ViewBox 的 Demo
WebbrowserDemo.xaml
@H_502_32@ @H_502_32@
代码

<
navigation:Page
x:Class
="Silverlight40.Control.WebbrowserDemo"
xmlns ="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x ="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d ="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc ="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:navigation ="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Navigation"
Title ="WebbrowserDemo Page" >
< Grid x:Name ="LayoutRoot" >
< StackPanel HorizontalAlignment ="Left" >
< Button Name ="btnOutOfbrowser" Click ="btnOutOfbrowser_Click" />
< Button Name ="btnSource" Content ="Source 属性" Click ="btnSource_Click" />
< Button Name ="btnNavigate" Content ="Navigate 方法" Click ="btnNavigate_Click" />
< Button Name ="btnNavigatetoString" Content ="NavigatetoString 方法" Click ="btnNavigatetoString_Click" />
< Button Name ="btnScript" Content ="与 Webbrowser 中的脚本交互" Click ="btnScript_Click" />
< Webbrowser Name ="webbrowser" Width ="400" Height ="300" ScriptNotify ="webbrowser_ScriptNotify" LoadCompleted ="webbrowser_LoadCompleted" />
</ StackPanel >
</ Grid >
</ navigation:Page >
xmlns ="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x ="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d ="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc ="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:navigation ="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Navigation"
Title ="WebbrowserDemo Page" >
< Grid x:Name ="LayoutRoot" >
< StackPanel HorizontalAlignment ="Left" >
< Button Name ="btnOutOfbrowser" Click ="btnOutOfbrowser_Click" />
< Button Name ="btnSource" Content ="Source 属性" Click ="btnSource_Click" />
< Button Name ="btnNavigate" Content ="Navigate 方法" Click ="btnNavigate_Click" />
< Button Name ="btnNavigatetoString" Content ="NavigatetoString 方法" Click ="btnNavigatetoString_Click" />
< Button Name ="btnScript" Content ="与 Webbrowser 中的脚本交互" Click ="btnScript_Click" />
< Webbrowser Name ="webbrowser" Width ="400" Height ="300" ScriptNotify ="webbrowser_ScriptNotify" LoadCompleted ="webbrowser_LoadCompleted" />
</ StackPanel >
</ Grid >
</ navigation:Page >
WebbrowserDemo.xaml.cs
@H_502_32@ @H_502_32@
代码

/*
* Webbrowser - 在 Silverlight 应用程序中显示 HTML 内容(只能在 OOB 模式下运行)
* Source - 将指定的 URI 中的 HTML 内容显示在 Webbrowser 中
* Navigate() - 加载指定的 URI 中的 HTML 内容到 Webbrowser 中
* NavigatetoString() - 显示指定的 HTML 内容
* SavetoString() - 获取当前 Webbrowser 所显示的 HTML 内容,返回一个字符串类型(不能跨域)
* InvokeScript() - 调用当前 Webbrowser 所加载的 HTML 内容中的 JavaScript 脚本(不能跨域)
* ScriptNotify - 当 Webbrowser 内的 JavaScript 以 “window.external.notify(string);” 的方式发送信息到 Silverlight 程序中时所触发的事件(不能跨域)
* NotifyEventArgs - ScriptNotify 事件的事件参数
* NotifyEventArgs.Value - JavaScript 发送到 Silverlight 程序中的信息。即 “window.external.notify(string);” 中的字符串
*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Navigation;
namespace Silverlight40.Control
{
public partial class WebbrowserDemo : Page
{
public WebbrowserDemo()
{
InitializeComponent();
}
protected override void OnNavigatedTo(NavigationEventArgs e)
{
if (App.Current.IsRunningOutOfbrowser)
btnOutOfbrowser.Content = " 卸载 " ;
else
btnOutOfbrowser.Content = " 安装 " ;
}
private void btnOutOfbrowser_Click( object sender, RoutedEventArgs e)
{
if ( ! App.Current.IsRunningOutOfbrowser && App.Current.InstallState == InstallState.notinstalled)
App.Current.Install();
else
MessageBox.Show( " 已经安装,使用右键卸载 " );
}
private void btnSource_Click( object sender, RoutedEventArgs e)
{
webbrowser.source = new Uri( " http://webabcd.cnblogs.com " );
}
private void btnNavigate_Click( object sender, RoutedEventArgs e)
{
webbrowser.Navigate( new Uri( " http://www.cnblogs.com/webabcd/archive/2007/02/24/655035.html " ));
}
private void btnNavigatetoString_Click( object sender, RoutedEventArgs e)
{
webbrowser.NavigatetoString( " <div style='color: red'>webabcd</div> " );
}
private void btnScript_Click( object sender, RoutedEventArgs e)
{
webbrowser.Navigate( new Uri( " http://localhost:9483/Silverlight40TestPage.html " ));
}
private void webbrowser_ScriptNotify( object sender, NotifyEventArgs e)
{
// 获取 Webbrowser 中的 HTML 内所包含的 JavaScript 发给 Silverlight 程序的信息
MessageBox.Show(e.Value);
// 调用 Webbrowser 中的 HTML 内所包含的 JavaScript 函数
MessageBox.Show(( string )webbrowser.InvokeScript( " hello " , " webabcd " ));
}
private void webbrowser_LoadCompleted( object sender, NavigationEventArgs e)
{
try
{
string html = webbrowser.SavetoString();
MessageBox.Show(html);
}
catch (System.Security.SecurityException se)
{
MessageBox.Show(se.Message);
}
}
}
}
* Webbrowser - 在 Silverlight 应用程序中显示 HTML 内容(只能在 OOB 模式下运行)
* Source - 将指定的 URI 中的 HTML 内容显示在 Webbrowser 中
* Navigate() - 加载指定的 URI 中的 HTML 内容到 Webbrowser 中
* NavigatetoString() - 显示指定的 HTML 内容
* SavetoString() - 获取当前 Webbrowser 所显示的 HTML 内容,返回一个字符串类型(不能跨域)
* InvokeScript() - 调用当前 Webbrowser 所加载的 HTML 内容中的 JavaScript 脚本(不能跨域)
* ScriptNotify - 当 Webbrowser 内的 JavaScript 以 “window.external.notify(string);” 的方式发送信息到 Silverlight 程序中时所触发的事件(不能跨域)
* NotifyEventArgs - ScriptNotify 事件的事件参数
* NotifyEventArgs.Value - JavaScript 发送到 Silverlight 程序中的信息。即 “window.external.notify(string);” 中的字符串
*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Navigation;
namespace Silverlight40.Control
{
public partial class WebbrowserDemo : Page
{
public WebbrowserDemo()
{
InitializeComponent();
}
protected override void OnNavigatedTo(NavigationEventArgs e)
{
if (App.Current.IsRunningOutOfbrowser)
btnOutOfbrowser.Content = " 卸载 " ;
else
btnOutOfbrowser.Content = " 安装 " ;
}
private void btnOutOfbrowser_Click( object sender, RoutedEventArgs e)
{
if ( ! App.Current.IsRunningOutOfbrowser && App.Current.InstallState == InstallState.notinstalled)
App.Current.Install();
else
MessageBox.Show( " 已经安装,使用右键卸载 " );
}
private void btnSource_Click( object sender, RoutedEventArgs e)
{
webbrowser.source = new Uri( " http://webabcd.cnblogs.com " );
}
private void btnNavigate_Click( object sender, RoutedEventArgs e)
{
webbrowser.Navigate( new Uri( " http://www.cnblogs.com/webabcd/archive/2007/02/24/655035.html " ));
}
private void btnNavigatetoString_Click( object sender, RoutedEventArgs e)
{
webbrowser.NavigatetoString( " <div style='color: red'>webabcd</div> " );
}
private void btnScript_Click( object sender, RoutedEventArgs e)
{
webbrowser.Navigate( new Uri( " http://localhost:9483/Silverlight40TestPage.html " ));
}
private void webbrowser_ScriptNotify( object sender, NotifyEventArgs e)
{
// 获取 Webbrowser 中的 HTML 内所包含的 JavaScript 发给 Silverlight 程序的信息
MessageBox.Show(e.Value);
// 调用 Webbrowser 中的 HTML 内所包含的 JavaScript 函数
MessageBox.Show(( string )webbrowser.InvokeScript( " hello " , " webabcd " ));
}
private void webbrowser_LoadCompleted( object sender, NavigationEventArgs e)
{
try
{
string html = webbrowser.SavetoString();
MessageBox.Show(html);
}
catch (System.Security.SecurityException se)
{
MessageBox.Show(se.Message);
}
}
}
}
相关的 JavaScript 部分
Silverlight40TestPage.html
@H_502_32@ @H_502_32@
代码

<
script type
=
"
text/javascript
"
>
// 此函数用于:Silverlight 程序调用 Webbrowser 所加载的 HTML 内容中的 JavaScript 函数
function hello(name) {
return " hello: " + name;
}
// 此方法用于:在 Webbrowser 所加载的 HTML 内容中用 JavaScript 向 Silverlight 程序发送信息
try {
window.external.notify( ' window.external.notify to silverlight ' );
} catch (err) { }
< / script>
// 此函数用于:Silverlight 程序调用 Webbrowser 所加载的 HTML 内容中的 JavaScript 函数
function hello(name) {
return " hello: " + name;
}
// 此方法用于:在 Webbrowser 所加载的 HTML 内容中用 JavaScript 向 Silverlight 程序发送信息
try {
window.external.notify( ' window.external.notify to silverlight ' );
} catch (err) { }
< / script>
2、WebbrowserBrush 的 Demo
WebbrowserBrushDemo.xaml
@H_502_32@ @H_502_32@
代码

<
navigation:Page
x:Class
="Silverlight40.Control.WebbrowserBrushDemo"
xmlns ="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x ="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d ="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc ="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:navigation ="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Navigation"
Title ="WebbrowserBrushDemo Page" >
< Grid x:Name ="LayoutRoot" MouseMove ="LayoutRoot_MouseMove" >
< StackPanel HorizontalAlignment ="Left" >
< Button Name ="btnOutOfbrowser" Click ="btnOutOfbrowser_Click" />
< Webbrowser Name ="webbrowser" Width ="200" Height ="150" Source ="http://webabcd.cnblogs.com" />
<!--
演示在 Rectangle.Fill 中使用 WebbrowserBrush
-->
< Rectangle Width ="200" Height ="150" HorizontalAlignment ="Left" >
< Rectangle.Fill >
< WebbrowserBrush x:Name ="webbrowserBrushRectangle" SourceName ="webbrowser" Opacity ="0.7" >
< WebbrowserBrush.Transform >
< ScaleTransform ScaleX ="2" ScaleY ="2" />
</ WebbrowserBrush.Transform >
</ WebbrowserBrush >
</ Rectangle.Fill >
</ Rectangle >
<!--
演示在 Canvas.Background 中使用 WebbrowserBrush
-->
< Canvas Width ="200" Height ="150" HorizontalAlignment ="Left" >
< Canvas.Background >
< WebbrowserBrush x:Name ="webbrowserBrushCanvas" SourceName ="webbrowser" Opacity ="0.7" />
</ Canvas.Background >
< Grid Width ="200" Height ="150" >
< TextBlock Text ="我是在 Canvas 里的 TextBlock" HorizontalAlignment ="Center" VerticalAlignment ="Center" />
</ Grid >
</ Canvas >
<!--
演示在 Path.Fill 中使用 WebbrowserBrush
-->
< Path strokeThickness ="10" stroke ="Red" >
< Path.Data >
< EllipseGeometry Center ="100,75" RadiusX ="100" RadiusY ="75" />
</ Path.Data >
< Path.Fill >
< WebbrowserBrush x:Name ="webbrowserBrushPath" SourceName ="webbrowser" Opacity ="0.7" />
</ Path.Fill >
</ Path >
</ StackPanel >
</ Grid >
</ navigation:Page >
xmlns ="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x ="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d ="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc ="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:navigation ="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Navigation"
Title ="WebbrowserBrushDemo Page" >
< Grid x:Name ="LayoutRoot" MouseMove ="LayoutRoot_MouseMove" >
< StackPanel HorizontalAlignment ="Left" >
< Button Name ="btnOutOfbrowser" Click ="btnOutOfbrowser_Click" />
< Webbrowser Name ="webbrowser" Width ="200" Height ="150" Source ="http://webabcd.cnblogs.com" />
<!--
演示在 Rectangle.Fill 中使用 WebbrowserBrush
-->
< Rectangle Width ="200" Height ="150" HorizontalAlignment ="Left" >
< Rectangle.Fill >
< WebbrowserBrush x:Name ="webbrowserBrushRectangle" SourceName ="webbrowser" Opacity ="0.7" >
< WebbrowserBrush.Transform >
< ScaleTransform ScaleX ="2" ScaleY ="2" />
</ WebbrowserBrush.Transform >
</ WebbrowserBrush >
</ Rectangle.Fill >
</ Rectangle >
<!--
演示在 Canvas.Background 中使用 WebbrowserBrush
-->
< Canvas Width ="200" Height ="150" HorizontalAlignment ="Left" >
< Canvas.Background >
< WebbrowserBrush x:Name ="webbrowserBrushCanvas" SourceName ="webbrowser" Opacity ="0.7" />
</ Canvas.Background >
< Grid Width ="200" Height ="150" >
< TextBlock Text ="我是在 Canvas 里的 TextBlock" HorizontalAlignment ="Center" VerticalAlignment ="Center" />
</ Grid >
</ Canvas >
<!--
演示在 Path.Fill 中使用 WebbrowserBrush
-->
< Path strokeThickness ="10" stroke ="Red" >
< Path.Data >
< EllipseGeometry Center ="100,75" RadiusX ="100" RadiusY ="75" />
</ Path.Data >
< Path.Fill >
< WebbrowserBrush x:Name ="webbrowserBrushPath" SourceName ="webbrowser" Opacity ="0.7" />
</ Path.Fill >
</ Path >
</ StackPanel >
</ Grid >
</ navigation:Page >
WebbrowserBrushDemo.xaml.cs
@H_502_32@ @H_502_32@
代码

/*
* WebbrowserBrush - 一个 Webbrowser 类型的画笔(只能在 OOB 模式下运行)
* SourceName - 为此画笔提供 HTML 内容的 Webbrowser
* Redraw() - 重绘画笔。当 Webbrowser 所加载内容更改时,需要调用此方法
*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Navigation;
namespace Silverlight40.Control
{
public partial class WebbrowserBrushDemo : Page
{
public WebbrowserBrushDemo()
{
InitializeComponent();
}
protected override void OnNavigatedTo(NavigationEventArgs e)
{
if (App.Current.IsRunningOutOfbrowser)
btnOutOfbrowser.Content = " 卸载 " ;
else
btnOutOfbrowser.Content = " 安装 " ;
}
private void btnOutOfbrowser_Click( object sender, RoutedEventArgs e)
{
if ( ! App.Current.IsRunningOutOfbrowser && App.Current.InstallState == InstallState.notinstalled)
App.Current.Install();
else
MessageBox.Show( " 已经安装,使用右键卸载 " );
}
private void LayoutRoot_MouseMove( object sender, MouseEventArgs e)
{
webbrowserBrushRectangle.Redraw();
webbrowserBrushCanvas.Redraw();
webbrowserBrushPath.Redraw();
}
}
}
* WebbrowserBrush - 一个 Webbrowser 类型的画笔(只能在 OOB 模式下运行)
* SourceName - 为此画笔提供 HTML 内容的 Webbrowser
* Redraw() - 重绘画笔。当 Webbrowser 所加载内容更改时,需要调用此方法
*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Navigation;
namespace Silverlight40.Control
{
public partial class WebbrowserBrushDemo : Page
{
public WebbrowserBrushDemo()
{
InitializeComponent();
}
protected override void OnNavigatedTo(NavigationEventArgs e)
{
if (App.Current.IsRunningOutOfbrowser)
btnOutOfbrowser.Content = " 卸载 " ;
else
btnOutOfbrowser.Content = " 安装 " ;
}
private void btnOutOfbrowser_Click( object sender, RoutedEventArgs e)
{
if ( ! App.Current.IsRunningOutOfbrowser && App.Current.InstallState == InstallState.notinstalled)
App.Current.Install();
else
MessageBox.Show( " 已经安装,使用右键卸载 " );
}
private void LayoutRoot_MouseMove( object sender, MouseEventArgs e)
{
webbrowserBrushRectangle.Redraw();
webbrowserBrushCanvas.Redraw();
webbrowserBrushPath.Redraw();
}
}
}
OK
[源码下载]
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。