今天,在寫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;
}
最后,總結一下總體來說不是很復雜,個人覺得這種方式還是挺實用的。希望跟大家分享下。
