Silverlight与浏览器通信技术总结3:应用实例演示

前两节简要介绍了Silverlight和浏览器的交互技术,本节将例举一些这方面的实际应用。
 
1 访问Query String
Http Get协议中URL可以利用Query String传递参数给Web服务器。从Silverlight也可以访问Query String,例如:
 
 
   
private void UserControl_Loaded( object sender, System.Windows.RoutedEventArgs e)
{
string name = HtmlPage.Document.QueryString[ " name " ];

if (String.IsNullOrEmpty(name))
{
txtHello.Text
= " Hello, Happy User! " ;
}
else
{
txtHello.Text
= String.Format( " Hello, {0}! " , name);
}
}

 

 
在上面代码中,Silverlight读取URL的Query String中name参数,并显示在text block中。
 
 
在Silverlight读取URL的Query String有什么实际用处呢?举个简单例子,某个web页面包含了很多silverlight游戏,需要通过URL的Query String来指定显示哪个游戏。在silverlight主程序中,读取URL的Query String,根据Query String来调用指定的游戏。除了Query String,当然也可以用其他方法实现,例如通过Silverlight object参数传递。这里只是给出一个思路,其实质就是把浏览器信息传递给Silverlight程序,具体如何应用就看实际需要了。
 

2 访问Cookie
 
大家知道,http协议本身是无状态的,Cookie是用来保存浏览器状态的技术之一。访问操作Cookie不但可以让Silverlight与浏览器共享状态信息,Silverlight也可以用Cookie来存取本身的状态信息。例如,Cookie可用来进行验证,保存shopping cart等。
 
Silverlight操作Cookie没有ASP.NET那么简单,但也不是很复杂,可以用HtmlPage.Document.Cookies对象。注意HtmlPage.Document.Cookies对象并不是一个集合,而是一个字符串。在该Cookie字符串中,每个变量由‘;’分隔。所以需要把该Cookie字符串分割到一个字符串数组中。
 
  
 
   
string [] cookies = HtmlPage.Document.Cookies.Split( ' ; ' );

 

 
HtmlDocument类的Cookies属性是只读的,因此无法直接通过该属性来设置Cookie。同时HtmlDocument类也没有提供一个属性来设置Cookie,我们需要调用SetProperty方法操作HTML文档的属性来写Cookies。例如:
 
 
   
public void SetCookie( string key, string value)
{
DateTime expireDate
= DateTime.Now.Add(TimeSpan.FromMinutes( 15 ));
string newCookie = String.Format( " {0}={1};expires={2} " , key, value, expireDate.ToString( " R " ));

HtmlPage.Document.SetProperty(
" cookie " , newCookie);
}

 

 
注意Cookie的格式,必须是:Key=Value;expires=ExpirationDate。ExpirationDate的格式:Thu, 17 Aug 2000 23:32:32 GMT。如果不设置ExpirationDate,Cookie在浏览器关闭后将不再存在。
 
 
笔者认为,Cookie对于Silverlight来说,主要是提供了一个浏览器与内嵌的Silverlight程序之间状态信息共享的机制,而且是轻量级的,即一般适用于少量文本存储。如果仅是Silverlight本身的状态保存,isolated storage是更佳选择,缺省时可以存1MB,经用户确认后可以存更多。

3 访问浏览器信息
 
在Silverligh中可以通过JavaScript很方便的访问浏览器信息,不过Silverlight提供了更容易的方式:使用BrowserInformation类。你可以访问HtmlPage静态类的BrowserInformation属性来获得BrowserInformation类的实例。下面是该类的一些属性:
 
BrowserVersion – 得到浏览器版本号。
CookiesEnabled – 检查Cookie是否使能(Enabled)。
Name – 得到浏览器名字。
Platform – 得到操作系统名字。
UserAgent – 得到浏览器user agent字符串。
 

 
   
public void GetBrowserInformation()
{
StringBuilder sb
= new StringBuilder();
BrowserInformation bi
= HtmlPage.BrowserInformation;

sb.AppendFormat(
" Name: {0}\n " , bi.Name);
sb.AppendFormat(
" Version: {0}\n " , bi.BrowserVersion);
sb.AppendFormat(
" Platform: {0}\n " , bi.Platform);
sb.AppendFormat(
" UserAgent: {0}\n " , bi.UserAgent);
sb.AppendFormat(
" Cookies Enabled?: {0}\n " , bi.CookiesEnabled);

HtmlPage.Window.Alert(sb.ToString());
}

 

 
在Silverlight实际应用中,可以把获得的浏览器信息通过web service上传给服务器,在服务器端保存到数据库中,用于以后的用户信息统计。当然也可以直接使用ASP.NET来实现浏览器信息获取和存储。用Silverlight实现的好处是分离Silverlight和容器网页的耦合,从而便于移植到其他网页中去。

4 在Silverlight应用程序中改变Silverlight对象在浏览器页面中的尺寸
我们来考虑这样一个Silverlight应用:Silverlight object尺寸的大小需要根据Silverlight程序的设置而变化。实际应用中由一个Silverlight主程序来控制很多小游戏,每个游戏的窗口尺寸可能各不相同,需要我们在Silverlight主程序中来指定。
 
这个应用实现起来很简单,需要用到上一节的Silverlight调用JavaScript的技术。
 
Silverlight代码如下: 
 
private void ResizeSilverlightOnject( double height )
{
    HtmlPage.Window.Invoke( "ResizeObject", new object[] { height } );
}

 

JavaScript代码如下:

function ResizeObject( height )
{     
    var host = document.getElementById( "silverlightControlHost" );
 
    host.style.height = height + "px";
}

代码中只显示了高度的设置,宽度的设置完全相同,就不重复了。

5 Silverlight探测浏览器窗口尺寸的改变
Silverlight窗口尺寸大小的设置看似很简单,其实涉及的问题很多。笔者 前面曾经讨论过合理的Silverlight窗口尺寸大小问题,实际上很难有一个统一的标准和方案。我们是否可以考虑,根据浏览器窗口的大小,来决定Silverlight窗口尺寸大小。在分辨率较高的显示器上,Silverlight窗口比较大,在分辨率较低的显示器上,Silverlight窗口比较小。这样一来,可以充分利用不同显示器的显示空间。同时如果用户改变浏览器窗口尺寸,Silverlight可以探测到这个变化,相应的改变自己的窗口尺寸。简单的说,就是实现Silverlight窗口尺寸根据浏览器窗口尺寸自适应变化。
 
这个应用实现的关键技术是Application.Current.Host.Content.Resized事件的处理。当浏览器窗口尺寸变化时,该事件被触发,我们可以在Silverlight中注册函数来处理该事件,从而实现Silverlight窗口尺寸的变化。代码如下:
 
 
   
Application.Current.Host.Content.Resized += new EventHandler(Content_Resized);

void Content_Resized( object sender, EventArgs e)
{
double browserHeight = Application.Current.Host.Content.ActualHeight;
double browserWidth = Application.Current.Host.Content.ActualWidth;

int x = 0 ;
int y = 0 ;
int width = 0 ;
int height = 0 ;

// Make the game window in the centre of root window when root window is larger than the max size of game window.
if (browserWidth > this .MaxWidth)
{
width
= Convert.ToInt32( this .MaxWidth);
x
= Convert.ToInt32((browserWidth - this .MaxWidth) / 2 );
}
else if (browserWidth < this .MinWidth)
{
width
= Convert.ToInt32( this .MinWidth);
}
else
width
= Convert.ToInt32(browserWidth);

if (browserHeight > this .MaxHeight)
{
height
= Convert.ToInt32( this .MaxHeight);
y
= Convert.ToInt32((browserHeight - this .MaxHeight) / 2 );
}
else if (browserHeight < this .MinHeight)
{
height
= Convert.ToInt32(MinHeight);
}
else
height
= Convert.ToInt32(browserHeight);

// adjust window location if changed
this .WindowCoordinate = new Point(x, y);

// adjust game window size and clip size if changed
if ( double .IsNaN( this .Width) || double .IsNaN( this .Height) || Math.Abs(width - this .Width) > 0.01 || Math.Abs(height - this .Height) > 0.01 )
{
this .Width = width;
this .Height = height;
((RectangleGeometry)
this .Clip).Rect = new Rect( 0 , 0 , this .Width, this .Height);
}
}

 

 
 
上面这段代码是从笔者正在开发的MMORPG游戏项目中截取出来的,比较啰嗦一点。由于开发还没有完成,这部分的代码就暂不提供下载了,可以看在线演示: http://rpg.yingyuantown.com
 
 
1,2,4源代码请在 这里下载。谢谢!

转载于:https://www.cnblogs.com/erichan/archive/2010/05/22/1741344.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值