在寫silverlight類庫的時候,遇到相關參數要配置完成,於是就想到了直接配置到web.config檔裡。但是,silverlight作為一個精簡版的.NET framework,沒有為設定檔提供相應的支援。我們無法像asp.net那樣用System.Web.Configuration使用來訪問app.config中的配置資訊。於是,自己寫了一個。參考的是:HTTP://www.codeproject.com/Articles/49490/Configure-Silverlight-3-Applications-using-the-Web
1. 首先,創建silverlight的工程預設的Default.aspx是沒有.cs檔的,那麼我們先將Default.aspx的HTML全部複製,然後刪除該頁面。再創建一個aspx頁面名字也叫Default.aspx.
然後再將剛才複製的內容粘貼到新建表單的html部分,現在新建的aspx是有.cs檔的。html部分內容如下:(只是在原有的基礎之上加入了<asp:Literal ID="ParamInitParams" runat="server"></asp:Literal>用來承載config資料)
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="SilverlightConfigurationDemoWeb.Default"%>

 

<!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>Silverlight Configuration Demonstration</title>

 

<!-- Link the Microsoft generated style -->
<link href="Styles/SilverlightDefault.css"
rel="stylesheet" type="text/css" />
<!-- Link the Microsoft generated JAVA scripts -->
<script src="JS/Silverlight.js" type="text/javascript"></script>
<script src="JS/SilverlightDefault.js" type="text/javascript"></script>
</head>
<body>
<form id="frmMain" runat="server">
<div>
<object data="data:application/x-silverlight-2,"
type="application/x-silverlight-2"
width="100%" height="100%">
<param name="source" value="ClientBin/SilverlightConfigurationDemo.xap"/>
<param name="onError" value="onSilverlightError" />
<param name="background" value="white" />
<param name="minRuntimeVersion" value="3.0.40624.0" />
<param name="autoUpgrade" value="true" />

 

<!-- This is ASP.NET code behind accessible object to add the configuration -->
<asp:Literal ID="ParamInitParams" runat="server"></asp:Literal>

 

<a href="HTTP://go.microsoft.com/fwlink/?LinkID=149156&v=3.0.40624.0" style="text-decoration:none">

 

<img src="HTTP://go.microsoft.com/fwlink/?LinkId=108181" alt="Get Microsoft Silverlight" style="border-style:none"/>
</a>
</object>

 

<iframe id="_sl_historyFrame"
style="visibility:hidden;height:0px;width:0px;border:0px">
</iframe>
</div>
</form>
</body>
</html>
而config部分主要是讀取<appSettings></appSettings>部分內的參數:
<appSettings>
<add key="ApplicationName" value="Silverlight Configuration Demonstration"/>
<add key="WCFEndPointAddress" value="HTTP://localhost/HelloService/MyService" />
<add key="AnotherWCFEndPointAddress"
value="HTTP://localhost/AnotherService/MyanotherService"/>
<add key="Author" value="Song Li"/>
<add key="Version" value="1.0.0.0"/>
<add key="DeploymentTime" value="01/01/2010"/>
<add key="CopyRight" value="The Code Project Open License (CPOL)"/>
<add key="OtherSilverlightApplicationInfo"
value="Whatever needed to send to Silverlight application @ deployment time"/>
</appSettings>

 

2.Default.aspx的後臺代碼:
using System;
using System.Web;
using System.Text; using System.Collections.Specialized;
using System.Configuration;
using System.Web.UI;
using System.Web.UI.WebControls;

 

namespace SilverlightConfigurationDemoWeb
{
public partial class Default : System.Web.UI.Page
{
//讀取config檔
private void SaveSilverlightDeploymentSettings(Literal litSettings)
{
NameValueCollection appSettings = ConfigurationManager.AppSettings;

 

StringBuilder SB = new StringBuilder();
SB.Append("<param name=\"InitParams\" value=\"");

 

int SettingCount = appSettings.Count;
for (int Idex = 0; Idex < SettingCount; Idex ++)
{
SB.Append(appSettings.GetKey(Idex));
SB.Append("=");
SB.Append(appSettings[Idex]);
SB.Append(",");
}
SB.Remove(SB.Length - 1, 1);
SB.Append("\" />");

 

litSettings.Text = SB.ToString();
}

 

protected void Page_Load(object sender, EventArgs e)
{
Response.Cache.SetCacheability(HttpCacheability.NoCache);
SaveSilverlightDeploymentSettings(ParamInitParams);
}
}
}
3.找到App.xaml ,定義 DeploymentConfigurations接受資料,e.InitParams返回的類型是 IDictionary<string, string>的。

 

public IDictionary<string, string> DeploymentConfigurations;

 

/// <summary>
/// 啟動入口
/// </summary>
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)
{
DeploymentConfigurations = e.InitParams;
this.RootVisual = new MainPage();
}
4.然後,在承載silverlight的主表單MainPage.xaml中接受config資料。DrillingResultViewModel是自己定義的一個類,哪裡需要用到了配置可以用其他方式接受,
主要看應用場景。
public MainPage()
{
InitializeComponent();
App _configurations = (App)Application.Current ;
DrillingResultViewModel._configurations = _configurations.DeploymentConfigurations;
}
創作者介紹
創作者 shadow 的頭像
shadow

資訊園

shadow 發表在 痞客邦 留言(0) 人氣()