在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頁面內容),其代碼是:
<%@ 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信息將放置在其中,因為它有一個InitParams參數,設置格式為:
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文件的內容如下:
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)
??????? {}
}
???? 在此文件中,我們要特別關注Application_Startup 方法,它是在當Silverlight應用程序Startup事件觸發時開始執行。其StartupEventArgs傳入參數中含有一個名為InitParams的屬性,其所含信息就是我們在第一步驟時設置的信息。而且,目前我們只有這么一個途徑來取得InitParams中的信息。
下面我們有兩個途徑來實現在Page.xaml.cs后臺代碼中訪問到此處的InitParams信息。
(一)途徑一:通過在App.xaml.cs中添加一個屬性來實現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程序的初始化參數
this.MyInitParams = e.InitParams; //在此處給我們上面建立的MyInitParams屬性賦值
??????? }
??????? 經過上面兩步修改后的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程序的初始化參數
this.MyInitParams = e.InitParams;
??????? }
private void Application_Exit(object sender, EventArgs e)
??????? {
??????? }
private void Application_UnhandledException(object sender, ApplicationUnhandledExceptionEventArgs e)
??????? {
// 如果應用程序是在調試器外運行的,則使用瀏覽器的
// 異常機制報告該異常。在 IE 上,將在狀態欄中用一個
// 黃色警報圖標來顯示該異常,而 Firefox 則會顯示一個腳本錯誤。
if (!System.Diagnostics.Debugger.IsAttached)
??????????? {
// 注意: 這使應用程序可以在已引發異常但尚未處理該異常的情況下
// 繼續運行。
// 對于生產應用程序,此錯誤處理應替換為向網站報告錯誤
// 并停止應用程序。
??????????????? 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的內容
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值來訪問對應的Value值
this.txtBxKey.Text = "澳大利亞的城市有: " + myApp.MyInitParams["Australia"];
//遍歷InitParams的內容
foreach (string key in myApp.MyInitParams.Keys)
??????????? {
this.txtBxValue.Text += key + ": " + myApp.MyInitParams[key] + "\n";
??????????? }
??????? }
??? }
}
(二)途徑二:通過改造Page.xaml.cs的Page()構造函數來實現InitParameters信息的可訪問性。
? 1、在App.xaml.cs代碼的Application_Startup方法中修改代碼如下(把e.InitParams做為參數傳遞到Page構造函數中):
this.RootVisual = new Page(e.InitParams);
? 2、修改Page.xaml.cs中的Page()構造函數,使其帶有一個傳入參數
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的內容
foreach (string key in initParams.Keys)
??????????? {
this.txtBxValue.Text += key + ": " + initParams[key] + "\n";
??????????? }
??????? }
??? }
}
程序執行的效果如圖:
轉載于:https://www.cnblogs.com/slteam/archive/2009/11/18/1605332.html
《新程序員》:云原生和全面數字化實踐50位技術專家共同創作,文字、視頻、音頻交互閱讀總結
以上是生活随笔為你收集整理的在C#代码中获取Silverlight的初始化initparams参数的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 【原创】new和delete
- 下一篇: Linq to xml API中 XNa