Silverlight中使用配置文件的方法
2011-11-23 19:31
453 查看
Silverlight作为一个精简版的.NET framework,没有为配置文件提供相应的支持。我们无法像在winForm中那样使用System.Configuration.ConfigurationManager 来访问app.config中的配置信息。所以比较好的一个方式就只自己写一个配置文件管理类ConfigurationManager。以下构造方法参考了Andrew Tokeley上面的相关文章。
首先,在工程中创建一个XML文件,可以取名为app.config。
记得放在工程的根目录,方便后面设置路径。把Build Action设成Resource,关于Build Action的说明可以在这里找到。
随便在app.config中写点东西
然后要开始创建这个ConfigurationManager类了。
首先在工程上按右键点击Add Reference,之后选择System.Xml.Linq。(如果没加的话无法引入命名空间System.Xml.Linq)
然后在ConfigurationManager类中写入如下代码
之后你就可以通过调用这个类来获得配置信息了。例如:
首先,在工程中创建一个XML文件,可以取名为app.config。
记得放在工程的根目录,方便后面设置路径。把Build Action设成Resource,关于Build Action的说明可以在这里找到。
随便在app.config中写点东西
<?xml version="1.0" encoding="utf-8" ?> <configuration> <appSettings> <add key="name" value="srzhz"/> <add key="university" value="Tsinghua University"/> </appSettings> </configuration>
然后要开始创建这个ConfigurationManager类了。
首先在工程上按右键点击Add Reference,之后选择System.Xml.Linq。(如果没加的话无法引入命名空间System.Xml.Linq)
然后在ConfigurationManager类中写入如下代码
using System; using System.Windows; using System.Collections.Generic; using System.Windows.Resources; using System.IO; using System.Xml.Linq; using System.Reflection; namespace Model { /// <summary> /// Access appSettings from a configuration file /// </summary> /// <remarks>Your appConfig file must be in the root of your applcation</remarks> public static class ConfigurationManager { static ConfigurationManager() { AppSettings = new Dictionary<string, string>(); ReadSettings(); } public static Dictionary<string, string> AppSettings { get; set; } private static void ReadSettings() { // Get the name of the executing assemby - we are going to be looking in the root folder for // a file called app.config string assemblyName = Assembly.GetExecutingAssembly().FullName; assemblyName = assemblyName.Substring(0, assemblyName.IndexOf(',')); string url = String.Format("{0};component/app.config", assemblyName); StreamResourceInfo configFile = Application.GetResourceStream(new Uri(url, UriKind.Relative)); if (configFile != null && configFile.Stream != null) { Stream stream = configFile.Stream; XDocument document = XDocument.Load(stream); foreach (XElement element in document.Descendants("appSettings").DescendantNodes()) { AppSettings.Add(element.Attribute("key").Value, element.Attribute("value").Value); } } } } }
之后你就可以通过调用这个类来获得配置信息了。例如:
String name = ConfigurationManager.AppSettings["name"];
相关文章推荐
- Silverlight中使用配置文件方法
- Silverlight中使用配置文件的方法
- Silverlight中使用配置文件方法
- 【2012-08-15】ArcGIS Server for Silverlight【1】 Silverlight中使用配置文件方法
- WCF服务控制台托管方法(不使用配置文件)
- .net简单使用Log4net的方法(多个日志配置文件)
- 不需要配置文件就可以使用Quartz.NET的方法
- Sping2.5配置文件的单例共享模式与独立产生实例,同步方法使用
- mysql 查看当前使用的配置文件my.cnf的方法
- 文件格式配置文件weka频繁模式挖掘使用方法
- 文件方法Android NDK开发的环境配置和使用
- 09_通过读取配置文件的方式来使用反射完成对实例对象方法调用
- spring的配置文件中mvc:view-controller path使用方法
- H5+Mui文件配置 vue-resource基本使用方法
- java提供的把中文转化为ascII码(*.properties 文件配置使用)的方法
- 解决windows7下使用IIS配置的PHP无法上传文件的方法
- win 7 删除了用户文件夹后出现的&quot;你已使用临时配置文件登录&quot;提示的解决方法
- apache中使用.htaccess文件缓存图片的配置方法
- Spring中使用Map、Set、List、数组、属性集合的注入方法配置文件
- 插件式开发中配置文件解析和使用方法