[转][Silverlight] aspx页面上传递参数给Silverlight插件的方法

在做Silverlight和asp.net web应用程序中,常常会遇到将用户登录的相关信息从aspx页面传递到Silverlight应用程序中,如何才能实现呢?

一是总结一下,便于备忘,另一方面是给后来学Silverlight的同志们,一个可供查询的方法。

现在总结如下,主要有两种方法:

假若从Login.aspx页面登录进来,并保存了Session相关信息,而另外一个功能是通过Silverlight来实现的,那么必然涉及到参数传递到那个Silverlight应用程序中去的问题。

Silverlight应用程序插件的aspx页面为:index.aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="index.aspx.cs" Inherits="ZDXX_index" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
<title>MultiVideoPicture</title>
<mce:style type="text/css"><!--
html, body {
height: 100%;
overflow: auto;
}
body {
padding: 0;
margin: 0;
}
#silverlightControlHost {
height: 100%;
text-align:center;
}

--></mce:style><style type="text/css" mce_bogus="1"> html, body {
height
: 100%;
overflow
: auto;
}
body
{
padding
: 0;
margin
: 0;
}
#silverlightControlHost
{
height
: 100%;
text-align
:center;
}
</style>
<mce:script type="text/javascript" src="Silverlight.js" mce_src="Silverlight.js"></mce:script>
<mce:script type="text/javascript"><!--
function onSilverlightError(sender, args) {
var appSource = "";
if (sender != null && sender != 0) {
appSource = sender.getHost().Source;
}
var errorType = args.ErrorType;
var iErrorCode = args.ErrorCode;
if (errorType == "ImageError" || errorType == "MediaError") {
return;
}
var errMsg = "Silverlight 应用程序中未处理的错误 " + appSource + "\n";
errMsg += "代码: " + iErrorCode + " \n";
errMsg += "类别: " + errorType + " \n";
errMsg += "消息: " + args.ErrorMessage + " \n";
if (errorType == "ParserError") {
errMsg += "文件: " + args.xamlFile + " \n";
errMsg += "行: " + args.lineNumber + " \n";
errMsg += "位置: " + args.charPosition + " \n";
}
else if (errorType == "RuntimeError") {
if (args.lineNumber != 0) {
errMsg += "行: " + args.lineNumber + " \n";
errMsg += "位置: " + args.charPosition + " \n";
}
errMsg += "方法名称: " + args.methodName + " \n";
}
引发新错误(errMsg);
}

//
--></mce:script>
</head>
<body>
<form id="form1" runat="server" style="height:100%">
<div id="silverlightControlHost">
<object data="data:application/x-silverlight-2," type="application/x-silverlight-2" width="100%" height="100%">
<param name="source" value="../ClientBin/SInfo.xap"/>
<param name="onError" value="onSilverlightError" />
<param name="background" value="white" />
<param name="minRuntimeVersion" value="4.0.50826.0" />
<param name="autoUpgrade" value="true" />
<param name="windowless" value="true" />
<param name="initParams" value="root=<%=ROOT %>,role=<%=ROLE %>" />
<a href="http://go.microsoft.com/fwlink/?LinkID=149156&v=4.0.50826.0" mce_href="http://go.microsoft.com/fwlink/?LinkID=149156&v=4.0.50826.0" style="text-decoration:none" mce_style="text-decoration:none">
<img src="http://go.microsoft.com/fwlink/?LinkId=161376" mce_src="http://go.microsoft.com/fwlink/?LinkId=161376"
alt
="获取 Microsoft Silverlight" style="border-style:none" mce_style="border-style:none"/>
</a>
</object><iframe id="_sl_historyFrame" style="visibility:hidden;height:0px;0px;border:0px"></iframe></div>
</form>
</body>
</html>

index.aspx.cs代码如下:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using USTC;
public partial class ZDXX_index : System.Web.UI.Page
{
public int ROOT;
public int ROLE;
DM dm2
= new DM();
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
//获取登录用户的Session信息,进行权限控制
string userID = Session["UserID"].ToString();
string strSQL = "select * from SYS_USERINF where ID=" + int.Parse(userID);
string role = dm2.getsql(strSQL).Tables[0].Rows[0]["权限编号"].ToString();
if (role == "0" || role == "1")
{
//省级及高级维护人员
ROOT = 340000;
ROLE
= 1;
}
else if (role == "2")
{
//市级人员
ROOT = int.Parse(dm2.getsql(strSQL).Tables[0].Rows[0]["所在市编码"].ToString().Trim());
ROLE
= 2;
}
else if (role == "3")
{
//县级人员
ROOT = int.Parse(dm2.getsql(strSQL).Tables[0].Rows[0]["所在县编码"].ToString().Trim());
ROLE
= 3;
}
}
}
}

在web项目中,通过一个页面,使用Silverlight应用程序的XAP文件

传递参数方法如下:

<param name="initParams " value="root=<%=ROOT %>,role=<%=ROLE %> " />

上面的参数中传递你需要传递的键值对。

这样我们的Silverlight应用程序SInfo就可以接受到参数了,怎么实现呢?这样:

方法一:

在App.xaml.cs文件中的Application_StartUp事件中添加如下的代码:

 
private void Application_Startup(object sender, StartupEventArgs e)
{
//第一种写法
Dictionary<string, string> ppDic = new Dictionary<string, string>();
if (e.InitParams != null)
{
foreach (var item in e.InitParams)
{
ppDic.Add(item.Key.ToUpper(), item.Value);
}
}
this.RootVisual = new MainPage(ppDic);
}

然后再MainPage.xaml.cs的构造函数中就可以取得参数了:

public partial class MainPage : UserControl
{
//获取参数,作为全局变量
public string root = null;
public string role = null;
//第一种获取插件页面传递过来的参数
public MainPage(Dictionary<string, string> paramsDic)
{
InitializeComponent();
if (paramsDic.TryGetValue("ROOT", out root))
{
//在这里我们就能拿到root的值(测试用)
//MessageBox.Show(root);
}
if (paramsDic.TryGetValue("ROLE", out role))
{
//在这里我们就能拿到role的值(测试用)
//MessageBox.Show(role);
}
}

方法二:在App.xaml.cs文件中的Application_StartUp事件中添加如下的代码:

 
private void Application_Startup(object sender, StartupEventArgs e)
{
//第二种写法
if (e.InitParams != null)
{
foreach (var item in e.InitParams)
{
this.Resources.Add(item.Key, item.Value);
}
}
this.RootVisual = new MainPage();
}

同样在MainPage.xaml.cs的构造函数中就可以取得参数了:

      
//获取页面传递参数的通用方法,单独抽取出来作为一个方法
private string GetParam(string p)
{
if (App.Current.Resources[p] != null)
{
return App.Current.Resources[p].ToString();
}
else
{
return string.Empty;
}
}
public MainPage()
{
InitializeComponent();
MessageBox.Show(GetParam(
"root"));
MessageBox.Show(GetParam(
"role"));
}
原文地址:https://www.cnblogs.com/slow/p/2032383.html