`
dongdian
  • 浏览: 99017 次
  • 性别: Icon_minigender_1
  • 来自: 北京
文章分类
社区版块
存档分类
最新评论

如何在C#引入Silverlight的initparams参数

 
阅读更多

今天学习内容是,我们将利用Silverlight给我们提供的一个便利的方法来实现: 当一个web page加裁时,把指定参数(或信息)从 web page传递到silverlight中,这就是initParams。
我们可以利用它把诸如页面url等相关信息传递到silverlight中(当然也可以传递其它信息)。
initParams 信息是按照 string/value对的方式来存放的。我们将学习如何设置以及如何读取它们。下面开始我们的实验。
仍按惯例,新建一个Silverlight应用程序,命名为:SLInitParamsFromWbToSL。如图:


一、在Web Page页面上放置我们将要传递的InitParams信息(InitParams信息设置格式与放置位置)。
WebPage页面是放置我们Silverlight控件的Host页面(本例为SLInitParamsFromWbToSLTestPage.aspx页面内容),其代码是:

<!--<br /> <br /> Code highlighting produced by Actipro CodeHighlighter (freeware)<br /> http://www.CodeHighlighter.com/<br /> <br /> --><%@PageLanguage="C#"AutoEventWireup="true"%>
<%@RegisterAssembly="System.Web.Silverlight"Namespace="System.Web.UI.SilverlightControls"
TagPrefix
="asp"%>

<!DOCTYPEhtmlPUBLIC"-//W3C//DTDXHTML1.0Transitional//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<htmlxmlns="http://www.w3.org/1999/xhtml"style="height:100%;">
<headrunat="server">
<title>SLInitParamsFromWbToSL</title>
</head>
<bodystyle="height:100%;margin:0;">
<formid="form1"runat="server"style="height:100%;">
<asp:ScriptManagerID="ScriptManager1"runat="server"></asp:ScriptManager>
<divstyle="height:100%;">
<asp:SilverlightID="Xaml1"runat="server"Source="~/ClientBin/SLInitParamsFromWbToSL.xap"MinimumVersion="2.0.31005.0"Width="100%"Height="100%"InitParameters="Australia=Mebourne,China=ChengDu,USA=Washington"/>
</div>
</form>
</body>
</html>

其间我们可以看到有一个Siverlight控件<asp:Silverlight ID="Xaml1" />,我们的InitParams信息将放置在其中,因为它有一个InitParams参数,设置格式为:

<!--<br /> <br /> Code highlighting produced by Actipro CodeHighlighter (freeware)<br /> http://www.CodeHighlighter.com/<br /> <br /> -->InitParameters="Australia=Mebourne,China=ChengDu,USA=Washington"

这就是将要传递到Silverlight中的InitParams 键值对信息.

二、在Silverlight中读取我们传递过来的InitParams信息。
当上面的页面(SLInitParamsFromWbToSLTestPage.aspx)加裁时,InitParameters的信息将会传递到Silverlight应用程序中,也即:传递到Silverlight的 App中,那么,我们如何在Silverlight application中获取InitParameters的信息呢?
我们知道,当我们新建一个Silverlight application时, Visual Studio 会为我们自动创建四个文件: App.xaml, App.xaml.cs, Page.xaml, and Page.xaml.cs. 通常,我们只关注后面两个文件,但当我们想要获取initParams时,我们就必须要关注App.xaml与App.xaml.cs文件了。
App.xaml.cs文件的内容如下:

<!--<br /> <br /> Code highlighting produced by Actipro CodeHighlighter (freeware)<br /> http://www.CodeHighlighter.com/<br /> <br /> -->publicpartialclassApp:Application
{
publicApp()
{
this.Startup+=this.Application_Startup;
this.Exit+=this.Application_Exit;
this.UnhandledException+=this.Application_UnhandledException;

InitializeComponent();
}

privatevoidApplication_Startup(objectsender,StartupEventArgse)
{
this.RootVisual=newPage();
}

privatevoidApplication_Exit(objectsender,EventArgse)
{

}
privatevoidApplication_UnhandledException(objectsender,ApplicationUnhandledExceptionEventArgse)
{}
privatevoidReportErrorToDOM(ApplicationUnhandledExceptionEventArgse)
{}
}

在此文件中,我们要特别关注Application_Startup 方法,它是在当Silverlight应用程序Startup事件触发时开始执行。其StartupEventArgs传入参数中含有一个名为InitParams的属性,其所含信息就是我们在第一步骤时设置的信息。而且,目前我们只有这么一个途径来取得InitParams中的信息。
下面我们有两个途径来实现在Page.xaml.cs后台代码中访问到此处的InitParams信息。
(一)途径一:通过在App.xaml.cs中添加一个属性来实现InitParameters信息的可访问性。。
1、添加属性: MyInitParams ,代码如下:

<!--<br /> <br /> Code highlighting produced by Actipro CodeHighlighter (freeware)<br /> http://www.CodeHighlighter.com/<br /> <br /> -->publicSystem.Collections.Generic.IDictionary<string,string>MyInitParams{get;set;}

2、在Application_Startup 方法中给属性MyInitParams 赋值

<!--<br /> <br /> Code highlighting produced by Actipro CodeHighlighter (freeware)<br /> http://www.CodeHighlighter.com/<br /> <br /> -->privatevoidApplication_Startup(objectsender,StartupEventArgse)
{
this.RootVisual=newPage();
//在这个位置,我们可以找到e.InitParams,它就是这个Silverlight程序的初始化参数
this.MyInitParams=e.InitParams;//在此处给我们上面建立的MyInitParams属性赋值
}

经过上面两步修改后的App.xaml.cs代码如下:


<!--<br /> <br /> Code highlighting produced by Actipro CodeHighlighter (freeware)<br /> http://www.CodeHighlighter.com/<br /> <br /> -->usingSystem;
usingSystem.Collections.Generic;
usingSystem.Linq;
usingSystem.Net;
usingSystem.Windows;
usingSystem.Windows.Controls;
usingSystem.Windows.Documents;
usingSystem.Windows.Input;
usingSystem.Windows.Media;
usingSystem.Windows.Media.Animation;
usingSystem.Windows.Shapes;
usingSystem.Collections;

namespaceSLInitParamsFromWbToSL
{
publicpartialclassApp:Application
{

publicSystem.Collections.Generic.IDictionary<string,string>MyInitParams{get;set;}

publicApp()
{
this.Startup+=this.Application_Startup;
this.Exit+=this.Application_Exit;
this.UnhandledException+=this.Application_UnhandledException;

InitializeComponent();
}

privatevoidApplication_Startup(objectsender,StartupEventArgse)
{
this.RootVisual=newPage();
//在这个位置,我们可以找到e.InitParams,它就是这个Silverlight程序的初始化参数
this.MyInitParams=e.InitParams;
}

privatevoidApplication_Exit(objectsender,EventArgse)
{

}
privatevoidApplication_UnhandledException(objectsender,ApplicationUnhandledExceptionEventArgse)
{
//如果应用程序是在调试器外运行的,则使用浏览器的
//异常机制报告该异常。在IE上,将在状态栏中用一个
//黄色警报图标来显示该异常,而Firefox则会显示一个脚本错误。
if(!System.Diagnostics.Debugger.IsAttached)
{

//注意:这使应用程序可以在已引发异常但尚未处理该异常的情况下
//继续运行。
//对于生产应用程序,此错误处理应替换为向网站报告错误
//并停止应用程序。
e.Handled=true;
Deployment.Current.Dispatcher.BeginInvoke(
delegate{ReportErrorToDOM(e);});
}
}
privatevoidReportErrorToDOM(ApplicationUnhandledExceptionEventArgse)
{
try
{
stringerrorMsg=e.ExceptionObject.Message+e.ExceptionObject.StackTrace;
errorMsg
=errorMsg.Replace('"','/'').Replace("/r/n",@"/n");

System.Windows.Browser.HtmlPage.Window.Eval(
"thrownewError(/"UnhandledErrorinSilverlight2Application"+errorMsg+"/");");
}
catch(Exception)
{
}
}
}
}

3、在Page.xaml.cs中访问MyInitParams信息。
i、按给定的Key访问Value

<!--<br /> <br /> Code highlighting produced by Actipro CodeHighlighter (freeware)<br /> http://www.CodeHighlighter.com/<br /> <br /> -->this.txtBxKey.Text="澳大利亚的城市有:"+myApp.MyInitParams["Australia"];

ii、遍历InitParams的内容

<!--<br /> <br /> Code highlighting produced by Actipro CodeHighlighter (freeware)<br /> http://www.CodeHighlighter.com/<br /> <br /> -->foreach(stringkeyinmyApp.MyInitParams.Keys)
{
this.txtBxValue.Text+=key+":"+myApp.MyInitParams[key]+"/n";
}

Page.xaml.cs代码如下:


<!--<br /> <br /> Code highlighting produced by Actipro CodeHighlighter (freeware)<br /> http://www.CodeHighlighter.com/<br /> <br /> -->usingSystem;
usingSystem.Collections.Generic;
usingSystem.Linq;
usingSystem.Net;
usingSystem.Windows;
usingSystem.Windows.Controls;
usingSystem.Windows.Documents;
usingSystem.Windows.Input;
usingSystem.Windows.Media;
usingSystem.Windows.Media.Animation;
usingSystem.Windows.Shapes;

namespaceSLInitParamsFromWbToSL
{
publicpartialclassPage:UserControl
{
publicPage()
{
InitializeComponent();
Loaded
+=newRoutedEventHandler(Page_Loaded);
}

privatevoidPage_Loaded(objectsender,EventArgse)
{
AppmyApp
=Application.CurrentasApp;
//按Key值来访问对应的Value值
this.txtBxKey.Text="澳大利亚的城市有:"+myApp.MyInitParams["Australia"];

//遍历InitParams的内容
foreach(stringkeyinmyApp.MyInitParams.Keys)
{
this.txtBxValue.Text+=key+":"+myApp.MyInitParams[key]+"/n";
}

}
}
}

(二)途径二:通过改造Page.xaml.cs的Page()构造函数来实现InitParameters信息的可访问性。
1、在App.xaml.cs代码的Application_Startup方法中修改代码如下(把e.InitParams做为参数传递到Page构造函数中):

<!--<br /> <br /> Code highlighting produced by Actipro CodeHighlighter (freeware)<br /> http://www.CodeHighlighter.com/<br /> <br /> -->this.RootVisual=newPage(e.InitParams);

2、修改Page.xaml.cs中的Page()构造函数,使其带有一个传入参数

<!--<br /> <br /> Code highlighting produced by Actipro CodeHighlighter (freeware)<br /> http://www.CodeHighlighter.com/<br /> <br /> -->publicPage(IDictionary<string,string>initParams)
{
InitializeComponent();
}

Page.xaml.cs全部代码如下:


<!--<br /> <br /> Code highlighting produced by Actipro CodeHighlighter (freeware)<br /> http://www.CodeHighlighter.com/<br /> <br /> -->usingSystem;
usingSystem.Collections.Generic;
usingSystem.Linq;
usingSystem.Net;
usingSystem.Windows;
usingSystem.Windows.Controls;
usingSystem.Windows.Documents;
usingSystem.Windows.Input;
usingSystem.Windows.Media;
usingSystem.Windows.Media.Animation;
usingSystem.Windows.Shapes;

namespaceSLInitParamsFromWbToSL
{
publicpartialclassPage:UserControl
{
publicPage(IDictionary<string,string>initParams)
{
InitializeComponent();

this.txtBxKey.Text="澳大利亚的城市有:"+initParams["Australia"];

//遍历InitParams的内容
foreach(stringkeyininitParams.Keys)
{
this.txtBxValue.Text+=key+":"+initParams[key]+"/n";
}
}

}
}

程序执行的效果如图:

分享到:
评论

相关推荐

    silverlight获取Asp.net参数(方式一)

    这是我做的一个silverlight获取Asp.net参数的测试项目,这是其中一种方式(&lt;param name="initParams"),很简单,代码很少,希望对大家有帮助

    Silverlight Loader

    2.通过项目模板创建一个SLLoader.xap项目:将SilverlightLoader模板文件拷贝到VS的模板文件夹下,如My Documents\Visual Studio 2008\ProjectTemplates\Visual C#。而后就可以在新建项目时看到Silverlight Loader...

    CS-sc-InitParams_CS.zip

    CS-sc-InitParams_CS.zip Silverlight源码

    Silverlight Audio Player源码

    并通过在Initparams(见下文)的MP3播放文件的URL或网址。按照惯例,喜欢的Visual Studio XAP文件的ClientBin在一个文件夹中。您不必这样做,如果你不这样做,您将需要调整 从下面的相对路径(即没有..需要的例子...

    富头像上传编辑器演示

    视图参数(__initParams)新增选取框尺寸参数,至此视图已臻佳境。 发布时间:2014-02-25 v1.8:将追加到上传接口url后的参数的提交方式更改为POST,避免参数中含有特殊字符(如base64中的+号)时产生错误。如果...

    Android代码-GuideView

    方法回调:创建GuideView -- initParams(初始化参数) -- getTargetViewPosition(获取TargetView位置核心方法) -- show(添加GuideView进DecorView) -- addHintView -- GuideView.onMeasure -- GuideView.onLayout -- ...

    富头像上传编辑器最新官方版

    修复了当加载的原图url中带视图参数(__initParams)时,旋转值未正确初始化的错误; 优化了生成头像的速度。 富头像上传编辑器 更新记录: v1.4:修复了上传原图时如果是使用摄像头拍照的方式而得到的原图并非来自...

    HelloApp:《帮助读者掌握Tomcat和Java Web开发》 Web + Eclipse

    3.new initParams setting method in servlet3.0 4.getParameter from the request 4.4.1在网络上共享数据 url:http://localhost:8080/HelloApp/counter code:4-8,4-9 1.getAttribute and setAttribute to share the...

    guideview:首次打开引导界面

    * 方法回调:创建GuideView -- initParams(初始化参数) -- getTargetViewPosition(获取TargetView位置核心方法) -- * show(添加GuideView进DecorView) -- addHintView -- GuideView.onMeasure -- GuideView....

    yunguanjiacode06131049.zip

    //添加初始化参数:initParams //白名单: servletRegistrationBean.addInitParameter("allow","127.0.0.1"); //IP黑名单 (存在共同时,deny优先于allow) : 如果满足deny的话提示:Sorry, you are not permitted...

    matlab实现字符识别代码-Video_ShotThread_SceneDetect:电视连续剧中的镜头穿线和场景检测

    它还假定您具有在Matlab中通过视频“读取”的合适方法,可能最好使用帧脉冲。 如以下建议的那样,镜头线程还可以用于场景检测: StoryGraphs:将角色交互可视化为时间轴Makarand Tapaswi,MartinBäuml和Rainer ...

    bookstore:基于JDBC+Servlet+JSP的网上书城

    Servlet3.0中Filter的配置initParams={@WebInitParam(name="charset",value="utf-8")} 查看图书库存的时候应该调用BookService,然后Service再调用DAO得到库存,而不能直接从购物车中取得书得到图书的数量。 因为是...

Global site tag (gtag.js) - Google Analytics