在我們的silverlight項目中,是被打包為xap zip檔案下載下傳到用戶端,是以silverlight中的app配置檔案我們不能直接修改,而在其宿主web host中的web.config在服務端我們也不能直接通路。在我們的項目中遇見了這個問題是以我就有了此部落格。
先說明解決這個問題的方案有:
1:調用wcf,webservice,asp.net頁面等服務端資料源,異步顯示在我們的ui。
2:利用silverlight項目的宿首頁面 object,傳入初始化參數,在silverlight app中擷取。
上面的方案都是針對于我們的少量有限配置資訊的擷取。我這裡做的是利用在服務端的xml配置檔案來模拟配置檔案(為什麼不用web.config?以為存在權限資訊的問題,是以我覺得盡量避免此檔案資訊暴露)。在silverlight的異步加載xml文檔并解析xml文檔。形成配置資訊。
為了全局使用,早些加載xml文檔,我們需要在app中加一句:
slconfigmanager.current.configpath = "../slconfig.xml";//配置檔案的路徑,相對于我們的xap檔案路徑。
我們先看一下測試xml:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<appsettings>
<add key="test1" value="123"></add>
<add key="test2" value="1"></add>
</appsettings>
<class classid="111">
<student age="123">
<name>ddddd</name>
</student>
<student age="28">
<name>111</name>
</class>
</configuration>

這是我們可以使用:

void page1_loaded(object sender, routedeventargs e)
{
messagebox.show(slconfigmanager.current.getsection<class>("class").classid + "");
messagebox.show(slconfigmanager.current.getappsettings("test1").tostring());
messagebox.show(slconfigmanager.current.getappsettings<sex>("test2").tostring());
}
public enum sex
man,woman
}

在這裡我們模拟了appsettings,和section(注:這裡的section,不需要預申明,在利用xml轉化形成的,更利于我們的配置擴充性,使用到了xmlroot,xmlelement等attribute),在看看我們的class類:

using system.xml.serialization;
namespace silverlightapplication2
{
[xmlroot("student")]
public class student
{
[xmlelement("name")]
public string name
{ get; set; }
[xmlattribute("age")]
public int age
{
get;
set;
}
}
[xmlroot("class")]
public class class
[xmlattribute("classid")]
public int classid
[xmlarray()]
[xmlarrayitem("students")]
public system.collections.generic.list<student> students
}

最後需要說明的是:在于我們的項目中可能存在xml檔案還沒有加載,的情況,是以加入了時間支援和isloaded屬性标示。
源碼:

using system;
using system.net;
using system.windows;
using system.windows.controls;
using system.windows.documents;
using system.windows.ink;
using system.windows.input;
using system.windows.media;
using system.windows.media.animation;
using system.windows.shapes;
using system.xml;
using system.xml.linq;
using system.linq;
using system.collections.generic;
using green.utility.utils;
public delegate void loadcomplete();
public class slconfigmanager
private xdocument document = null;
private static readonly slconfigmanager _slconfigmanager = new slconfigmanager();
private dictionary<string, object> dict = new dictionary<string, object>();
private dictionary<string, string> appsettings = new dictionary<string, string>();
private string _configpath = null;
public static slconfigmanager current
get
{
return _slconfigmanager;
}
public string configpath
get { return _configpath; }
set
if (_configpath != value)
{
_configpath = value;
loadresource();
}
public bool isloaded
private set;
public event loadcomplete loadcomplete;
public void ensureloadresource()
if (document == null)
loadresource();
protected virtual void loadresource()
if (string.isnullorempty(configpath))
throw new exception("configpath is required!");
dict.clear();
uri url = new uri(application.current.host.source, configpath);
webclient client = new webclient();
client.openreadcompleted += new openreadcompletedeventhandler(client_openreadcompleted);
client.openreadasync(url, client);
protected virtual void client_openreadcompleted(object sender, openreadcompletedeventargs e)
if (e.error != null)
throw e.error;
var sw = new system.io.streamreader(e.result);
var xml = sw.readtoend();
document = xdocument.parse(xml);
sw.close();
e.result.close();
sw = null;
getappsettings();
isloaded = true;
if (loadcomplete != null)
loadcomplete();
protected virtual void getappsettings()
if (document != null)
var appsettingsel = document.root.element("appsettings");
if (appsettings != null)
appsettingsel.elements("add").tolist().foreach(e =>
{
var key = e.attribute("key").value;
var value = e.attribute("value").value;
if (!string.isnullorempty(key)&&!this.appsettings.containskey(key))
{
this.appsettings.add(key, value);
}
});
public t getsection<t>(string section) where t : class ,new()
// throw new exception("config document is null!");
if (!dict.containskey(section))
var el = document.root.descendants().singleordefault(t => t.name == section);
var xml = el.tostring();
dict.add(section, xmlserializer<t>(xml));
return dict[section] as t;
protected virtual t xmlserializer<t>(string xml) where t : class ,new()
system.xml.serialization.xmlserializer serialzer = new system.xml.serialization.xmlserializer(typeof(t));
system.io.memorystream ms = new system.io.memorystream(system.text.encoding.utf8.getbytes(xml));
var obj = serialzer.deserialize(ms) as t;
ms.flush();
ms.close();
ms = null;
return obj;
public t getappsettings<t>(string key) where t : iconvertible
if (appsettings.containskey(key))
var val = this.appsettings[key];
if (string.isnullorempty(val))
return default(t);
if (typeof(t).isenum)
return (t)enum.parse(typeof(t), val, true);
return (t)convert.changetype(val, typeof(t), null);
return default(t);
public string getappsettings(string key)
return getappsettings<string>(key);
