最近一直在做Silverlight插件,用来在页面上展示一些动画效果和视频播放,有关如何通过页面传递参数给Silverlight插件已经是一个老生常谈的话题了,不过我还是想在这里重新做下记录,方便新人学习,也方便自己日后查阅。
大家都知道,我们在页面上使用Silverlight插件一般都采用下面这种标记,当然你也可以在aspx页面中使用silverlight标记,不过这种方式用得很少,而且只限于在aspx页面上使用。
<object data&#61;"data:application/x-silverlight-2," type&#61;"application/x-silverlight-2" width&#61;"100%" height&#61;"100%">
<param name&#61;"source" value&#61;"ClientBin/SilverlightTest.xap"/>
<param name&#61;"onError" value&#61;"onSilverlightError" />
<param name&#61;"background" value&#61;"white" />
<param name&#61;"minRuntimeVersion" value&#61;"3.0.40624.0" />
<param name&#61;"autoUpgrade" value&#61;"true" />
<a href&#61;"http://go.microsoft.com/fwlink/?LinkID&#61;149156&v&#61;3.0.40624.0" style&#61;"text-decoration:none">
<img src&#61;"http://go.microsoft.com/fwlink/?LinkId&#61;108181" alt&#61;"Get Microsoft Silverlight" style&#61;"border-style:none"/>
a>
object>
与flash插件类似&#xff0c;在页面上使用的也是一个object标记&#xff0c;我们可以通过已有的param参数来修改silverlight插件的一些属性&#xff0c;例如我们可以使用source来要求silverlight插件使用不同的xap文件&#xff0c;使用onError设置Silverlight加载出错时要执行的脚本&#xff0c;使用background参数设置silverlight插件的背景色&#xff0c;以及使用minRuntimeVersion参数要求客户端运行Silverlight插件的最小版本号&#xff0c;使用autoUpgrade参数要求是否允许客户端的Silverlight浏览器插件自动升级等等。除此之外&#xff0c;你还可以自定义参数&#xff0c;我们可以使用initParams来命名参数&#xff0c;通过该参数我们可以同时传递多个值给Silverlight&#xff0c;如下面的代码&#xff1a;
<object data&#61;"data:application/x-silverlight-2," type&#61;"application/x-silverlight-2" width&#61;"100%" height&#61;"100%">
<param name&#61;"source" value&#61;"ClientBin/SilverlightTest.xap"/>
<param name&#61;"onError" value&#61;"onSilverlightError" />
<param name&#61;"background" value&#61;"white" />
<param name&#61;"minRuntimeVersion" value&#61;"3.0.40624.0" />
<param name&#61;"autoUpgrade" value&#61;"true" />
<param name&#61;"initParams" value&#61;"programmer&#61;Jaxu, blog&#61;http://www.cnblogs.com/jaxu, title&#61;silvelight" />
<a href&#61;"http://go.microsoft.com/fwlink/?LinkID&#61;149156&v&#61;3.0.40624.0" style&#61;"text-decoration:none">
<img src&#61;"http://go.microsoft.com/fwlink/?LinkId&#61;108181" alt&#61;"Get Microsoft Silverlight" style&#61;"border-style:none"/>
a>
object>
我们通过name/value对来设定initParams参数的值&#xff0c;如上例中就传递了三个不同的参数programmer、blog和title。在服务器端如何接收这些值呢&#xff1f;看下面的示例&#xff0c;首先我们需要在App.xaml.cs文件的Application_Startup事件中加载这些参数的值。
private void Application_Startup(object sender, StartupEventArgs e)
{
if ( e.InitParams !&#61; null )
{
foreach ( var item in e.InitParams )
{
this.Resources.Add( item.Key, item.Value );
}
}
this.RootVisual &#61; new Page();
}
通过遍历InitParams&#xff0c;我们将参数以键-值对的形式加载到Silverlight全局资源集合中&#xff0c;最后一行代码是Silverlight默认的代码。使用的时候我们可以通过参数名从Silverlight全局资源集合中取出相应的值&#xff0c;可以写成一个函数方便调用。
private string GetParam(string p)
{
if ( App.Current.Resources[p] !&#61; null )
{
return App.Current.Resources[p].ToString();
}
else
{
return string.Empty;
}
}
这样我们就可以轻松地获取到页面上传递过来的参数了。如果需要传递的参数过多&#xff0c;除了采用name/value对的形式外&#xff0c;我们还可以将参数写到xml里&#xff0c;如大家比较熟悉的playList&#xff0c;将视频播放要使用的参数写到playList xml里&#xff0c;然后在Application_Startup事件中解析xml并加载参数的值&#xff0c;使用xml的形式便于组合和管理参数。
另外&#xff0c;有时候我们也需要在Silverlight中实现与页面DOM之间的交互。在Silverlight中&#xff0c;我们可以通过HtmlPage.Plugin来获取页面上用来呈现Silverlight插件的object标签&#xff0c;通过HtmlPage.Plugin.Parent.Id就可以得到父容器的id&#xff0c;如div或者td。除此之外&#xff0c;还可以通过HtmlPage.Document.GetElementById("elementID")来得到一个页面上的控件&#xff0c;该方法返回HtmlElement对象&#xff0c;通过该对象的SetProperty("PropertyName")方法可以修改控件的属性值&#xff0c;通过GetProperty("PropertyName")方法可以得到控件的属性值。这样&#xff0c;我们就可以实现Silverlight插件和页面元素之间的交互了。
有时候我们会碰到一些小问题&#xff0c;例如我们想让页面上的元素&#xff08;如div&#xff09;浮动到Silverlight元素之上&#xff0c;默认情况下Silverlight总会呈现在所有页面元素之上&#xff0c;这时你可以给页面上的object对象传递一个参数Windowless&#xff0c;将值设为true&#xff0c;让Silverlight插件以“无窗体”的形式呈现就可以了。
还有一种情况&#xff0c;就是当Silverlight插件在执行一个动画时&#xff0c;动画所控制的元素会超出Silverlight插件本身的范围&#xff0c;这时如果Silverlight插件周围存在页面上的其它容器控件&#xff08;如div&#xff09;&#xff0c;而Windowsless的值又为true&#xff0c;此时超出去的部分就会被旁边的容器挡住。这时我们就需要通过程序来动态控制Silverlight父容器控件的大小和ZIndex值&#xff0c;同时还需要将Silverlight插件的背景色设置为透明。下面的代码展示了这一点技巧。
public partial class MainPage : UserControl
{
private string divID &#61; string.Empty;
private HtmlElement element;
private double originalWidth &#61; 0;
private double originalHeight &#61; 0;
private double naturalWidth &#61; 865;
private double naturalHeight &#61; 410;
private int state &#61; 0;
public MainPage()
{
// Required to initialize variables
InitializeComponent();
divID &#61; HtmlPage.Plugin.Parent.Id;
element &#61; HtmlPage.Document.GetElementById(divID);
originalWidth &#61; Convert.ToDouble(element.GetProperty("offsetWidth"));
originalHeight &#61; Convert.ToDouble(element.GetProperty("offsetHeight"));
}
// Zoom in.
private void videoToggleButton_Checked(object sender, RoutedEventArgs e)
{
SetPropertiesBeforeZoomedIn();
videoZoomInStoryboard.Begin();
}
// Zoom out.
private void videoToggleButton_Unchecked(object sender, RoutedEventArgs e)
{
videoZoomOutStoryboard.Begin();
}
private void ZoomOutStoryboard_Completed(object sender, EventArgs e)
{
if (state.Equals(0))
{
SetProperitesAfterZoomedOut();
}
}
private void SetPropertiesBeforeZoomedIn()
{
element.SetStyleAttribute("width", naturalWidth.ToString() &#43; "px");
element.SetStyleAttribute("height", naturalHeight.ToString() &#43; "px");
element.SetStyleAttribute("zIndex", "100");
}
private void SetProperitesAfterZoomedOut()
{
element.SetStyleAttribute("width", originalWidth.ToString() &#43; "px");
element.SetStyleAttribute("height", originalHeight.ToString() &#43; "px");
element.SetStyleAttribute("zIndex", "0");
}
}
一开始程序会把Silverlight父容器的高和宽记录下来&#xff0c;事件videoToggleButton_Checked和事件videoToggleButton_Unchecked会分别执行一个放大和缩小的动画过程&#xff0c;在放大过程执行前程序会将容器的高和宽设定为预期的值并同时修改ZIndex&#xff0c;这时Silverlight插件就会显示在所有页面容器的最上面并且不会被周边的容器挡住&#xff1b;在缩小过程执行完后程序再将容器的高和宽设定为最初的值并同时修改回ZIndex&#xff0c;此时Silverlight插件就会恢复到最初的状态。你可能会问为什么我们不能只设定容器的ZIndex值而还要同时修改它的大小呢&#xff1f;这是因为当Silverlight插件被放大或缩小时它的父容器的大小并不会随之一起改变&#xff0c;这时即使你将父容器的ZIndex值改成最大Silverlight插件还是会被父容器本身挡住。
在进行Silverlight插件和页面元素之间交互时还会有许多的技巧&#xff0c;其实只要认真研究下&#xff0c;应该不会有什么问题不能解决&#xff01;
本文转自Jaxu博客园博客&#xff0c;原文链接&#xff1a;http://www.cnblogs.com/jaxu/archive/2010/01/22/1654052.html&#xff0c;如需转载请自行联系原作者