在C#代码中获取Silverlight的初始化initparams参数
今天學(xué)習(xí)內(nèi)容是,我們將利用Silverlight給我們提供的一個便利的方法來實(shí)現(xiàn): 當(dāng)一個web page加裁時,把指定參數(shù)(或信息)從 web page傳遞到silverlight中,這就是initParams。
? 我們可以利用它把諸如頁面url等相關(guān)信息傳遞到silverlight中(當(dāng)然也可以傳遞其它信息)。
? initParams 信息是按照 string/value對的方式來存放的。我們將學(xué)習(xí)如何設(shè)置以及如何讀取它們。下面開始我們的實(shí)驗(yàn)。
? 仍按慣例,新建一個Silverlight應(yīng)用程序,命名為:SLInitParamsFromWbToSL。如圖:
一、在Web Page頁面上放置我們將要傳遞的InitParams信息(InitParams信息設(shè)置格式與放置位置)。
WebPage頁面是放置我們Silverlight控件的Host頁面(本例為SLInitParamsFromWbToSLTestPage.aspx頁面內(nèi)容),其代碼是:
<%@ Page Language="C#" AutoEventWireup="true" %>
<%@ Register Assembly="System.Web.Silverlight" Namespace="System.Web.UI.SilverlightControls"
??? TagPrefix="asp" %>
<!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" style="height:100%;">
<head runat="server">
<title>SLInitParamsFromWbToSL</title>
</head>
<body style="height:100%;margin:0;">
<form id="form1" runat="server" style="height:100%;">
<asp:ScriptManager ID="ScriptManager1" runat="server"></asp:ScriptManager>
<div? style="height:100%;">
<asp:Silverlight ID="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信息將放置在其中,因?yàn)樗幸粋€InitParams參數(shù),設(shè)置格式為:
InitParameters="Australia=Mebourne,China=ChengDu,USA=Washington"
這就是將要傳遞到Silverlight中的InitParams 鍵值對信息.
二、在Silverlight中讀取我們傳遞過來的InitParams信息。
?? 當(dāng)上面的頁面(SLInitParamsFromWbToSLTestPage.aspx)加裁時,InitParameters的信息將會傳遞到Silverlight應(yīng)用程序中,也即:傳遞到Silverlight的 App中,那么,我們?nèi)绾卧赟ilverlight application中獲取InitParameters的信息呢?
??? 我們知道,當(dāng)我們新建一個Silverlight application時, Visual Studio 會為我們自動創(chuàng)建四個文件: App.xaml, App.xaml.cs, Page.xaml, and Page.xaml.cs. 通常,我們只關(guān)注后面兩個文件,但當(dāng)我們想要獲取initParams時,我們就必須要關(guān)注App.xaml與App.xaml.cs文件了。
??? App.xaml.cs文件的內(nèi)容如下:
public partial class App : Application
??? {
public App()
??????? {
this.Startup += this.Application_Startup;
this.Exit += this.Application_Exit;
this.UnhandledException += this.Application_UnhandledException;
??????????? InitializeComponent();
??????? }
private void Application_Startup(object sender, StartupEventArgs e)
??????? {
this.RootVisual = new Page();
??????? }
private void Application_Exit(object sender, EventArgs e)
??????? {
??????? }
private void Application_UnhandledException(object sender, ApplicationUnhandledExceptionEventArgs e)
??????? {?? }
private void ReportErrorToDOM(ApplicationUnhandledExceptionEventArgs e)
??????? {}
}
???? 在此文件中,我們要特別關(guān)注Application_Startup 方法,它是在當(dāng)Silverlight應(yīng)用程序Startup事件觸發(fā)時開始執(zhí)行。其StartupEventArgs傳入?yún)?shù)中含有一個名為InitParams的屬性,其所含信息就是我們在第一步驟時設(shè)置的信息。而且,目前我們只有這么一個途徑來取得InitParams中的信息。
下面我們有兩個途徑來實(shí)現(xiàn)在Page.xaml.cs后臺代碼中訪問到此處的InitParams信息。
(一)途徑一:通過在App.xaml.cs中添加一個屬性來實(shí)現(xiàn)InitParameters信息的可訪問性。。
?????? 1、添加屬性: MyInitParams ,代碼如下:
public System.Collections.Generic.IDictionary? <string,string > MyInitParams { get;set;}
?????? 2、在Application_Startup 方法中給屬性MyInitParams 賦值
private void Application_Startup(object sender, StartupEventArgs e)
??????? {
this.RootVisual = new Page();
//在這個位置,我們可以找到 e.InitParams,它就是這個Silverlight程序的初始化參數(shù)
this.MyInitParams = e.InitParams; //在此處給我們上面建立的MyInitParams屬性賦值
??????? }
??????? 經(jīng)過上面兩步修改后的App.xaml.cs代碼如下:
Code
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.Collections;
namespace SLInitParamsFromWbToSL
{
public partial class App : Application
??? {
public System.Collections.Generic.IDictionary? <string,string > MyInitParams { get;set;}
public App()
??????? {
this.Startup += this.Application_Startup;
this.Exit += this.Application_Exit;
this.UnhandledException += this.Application_UnhandledException;
??????????? InitializeComponent();
??????? }
private void Application_Startup(object sender, StartupEventArgs e)
??????? {
this.RootVisual = new Page();
//在這個位置,我們可以找到 e.InitParams,它就是這個Silverlight程序的初始化參數(shù)
this.MyInitParams = e.InitParams;
??????? }
private void Application_Exit(object sender, EventArgs e)
??????? {
??????? }
private void Application_UnhandledException(object sender, ApplicationUnhandledExceptionEventArgs e)
??????? {
// 如果應(yīng)用程序是在調(diào)試器外運(yùn)行的,則使用瀏覽器的
// 異常機(jī)制報告該異常。在 IE 上,將在狀態(tài)欄中用一個
// 黃色警報圖標(biāo)來顯示該異常,而 Firefox 則會顯示一個腳本錯誤。
if (!System.Diagnostics.Debugger.IsAttached)
??????????? {
// 注意: 這使應(yīng)用程序可以在已引發(fā)異常但尚未處理該異常的情況下
// 繼續(xù)運(yùn)行。
// 對于生產(chǎn)應(yīng)用程序,此錯誤處理應(yīng)替換為向網(wǎng)站報告錯誤
// 并停止應(yīng)用程序。
??????????????? e.Handled = true;
??????????????? Deployment.Current.Dispatcher.BeginInvoke(delegate { ReportErrorToDOM(e); });
??????????? }
??????? }
private void ReportErrorToDOM(ApplicationUnhandledExceptionEventArgs e)
??????? {
try
??????????? {
string errorMsg = e.ExceptionObject.Message + e.ExceptionObject.StackTrace;
??????????????? errorMsg = errorMsg.Replace('"', '\'').Replace("\r\n", @"\n");
??????????????? System.Windows.Browser.HtmlPage.Window.Eval("throw new Error(\"Unhandled Error in Silverlight 2 Application " + errorMsg + "\");");
??????????? }
catch (Exception)
??????????? {
??????????? }
??????? }
??? }
}
? 3、在Page.xaml.cs中訪問MyInitParams信息。
?? i、按給定的Key訪問Value
this.txtBxKey.Text = "澳大利亞的城市有: " + myApp.MyInitParams["Australia"];
? ii、遍歷InitParams的內(nèi)容
foreach (string key in myApp.MyInitParams.Keys)
??????????? {
this.txtBxValue.Text += key + ": " + myApp.MyInitParams[key] + "\n";
??????????? }
????? Page.xaml.cs代碼如下:
Code
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;
namespace SLInitParamsFromWbToSL
{
public partial class Page : UserControl
??? {
public Page()
??????? {
??????????? InitializeComponent();
??????????? Loaded +=new RoutedEventHandler(Page_Loaded);
??????? }
private void Page_Loaded(object sender, EventArgs e)
??????? {
??????????? App myApp = Application.Current as App;
//按Key值來訪問對應(yīng)的Value值
this.txtBxKey.Text = "澳大利亞的城市有: " + myApp.MyInitParams["Australia"];
//遍歷InitParams的內(nèi)容
foreach (string key in myApp.MyInitParams.Keys)
??????????? {
this.txtBxValue.Text += key + ": " + myApp.MyInitParams[key] + "\n";
??????????? }
??????? }
??? }
}
(二)途徑二:通過改造Page.xaml.cs的Page()構(gòu)造函數(shù)來實(shí)現(xiàn)InitParameters信息的可訪問性。
? 1、在App.xaml.cs代碼的Application_Startup方法中修改代碼如下(把e.InitParams做為參數(shù)傳遞到Page構(gòu)造函數(shù)中):
this.RootVisual = new Page(e.InitParams);
? 2、修改Page.xaml.cs中的Page()構(gòu)造函數(shù),使其帶有一個傳入?yún)?shù)
public Page(IDictionary<string, string> initParams)
??????? {
??????????? InitializeComponent();
??????? }
Page.xaml.cs全部代碼如下:
Code
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;
namespace SLInitParamsFromWbToSL
{
public partial class Page : UserControl
??? {
public Page(IDictionary<string, string> initParams)
??????? {
??????????? InitializeComponent();
this.txtBxKey.Text = "澳大利亞的城市有: " + initParams["Australia"];
//遍歷InitParams的內(nèi)容
foreach (string key in initParams.Keys)
??????????? {
this.txtBxValue.Text += key + ": " + initParams[key] + "\n";
??????????? }
??????? }
??? }
}
程序執(zhí)行的效果如圖:
轉(zhuǎn)載于:https://www.cnblogs.com/slteam/archive/2009/11/18/1605332.html
《新程序員》:云原生和全面數(shù)字化實(shí)踐50位技術(shù)專家共同創(chuàng)作,文字、視頻、音頻交互閱讀總結(jié)
以上是生活随笔為你收集整理的在C#代码中获取Silverlight的初始化initparams参数的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 【原创】new和delete
- 下一篇: 简单XML文件C#操作方法