Silverlight读取web.config配置文件
来源:互联网 发布:data science 知乎 编辑:程序博客网 时间:2024/05/28 11:50
由于Silverlight会编译进一个xap文件,包含它的配置文件也会编译进xap文件,所以在silverlight工程里面是无法读取web.config文件的。但通常我们需要在silverlight中读取和设置一些配置文件配置项,怎么办?有两个方法:
方法1. 配置项写入ServiceReferences.ClientConfig文件(如果你让它存在的话),这样就简单了,通常的读取xml方法就可以。但问题是编译和部署后这个文件会消失,会编译进.xap文件,所以比较糟糕。
方法2. 用绕弯的方法,在托管的web网站里面读取web.config(或其它任何配置文件),然后放入Literal控件,最后在silverlight工程的App.xaml.cs中的Application_Startup事件处理方法中读取,具体方法有好多步骤,详细步骤可以参考CodeProject上的这篇文章。
方法3. 用webClient读取资源文件,例子:
1:2: using System; // Uri3: using System.IO; // Stream4: using System.Windows; // Application5: using System.Windows.Controls; // TextBlock, Image6: using System.Windows.Media.Imaging; // BitmapImage7: using System.Net; // WebClient8: using System.Windows.Resources; // StreamResourceInfo9:10: namespace SilverlightApplication
11: {12: public partial class PageLong : UserControl13: {14: public PageLong()
15: {16: InitializeComponent();17:18: // Load an image resource file that is included in a ZIP package
19: // at the site of origin.
20:21: // Specify the zip package with the image resource to get.
22: Uri uri = new Uri("ZIPPackageWithImage.zip", UriKind.Relative);23:24: // Download the zip package.
25: WebClient webClient = new WebClient();
26: webClient.OpenReadCompleted += new OpenReadCompletedEventHandler(
27: webClient_OpenReadCompleted);28: webClient.OpenReadAsync(uri);29: }30:31: void webClient_OpenReadCompleted(object sender,32: OpenReadCompletedEventArgs e)33: {34: // Extract the desired image from the zip package.
35: Stream zipPackageStream = e.Result;36: Image image = LoadImageFromZipPackage(37: "ImageInZipPackage.png", zipPackageStream);
38: this.stackPanel.Children.Add(image);
39: }40:41: public Image LoadImageFromZipPackage(
42: string relativeUriString, Stream zipPackageStream)
43: {44: // Get the image stream at the specified URI that
45: // is relative to the application package root.
46: Uri uri = new Uri(relativeUriString, UriKind.Relative);
47: StreamResourceInfo zipPackageSri =48: new StreamResourceInfo(zipPackageStream, null);49: StreamResourceInfo imageSri =50: Application.GetResourceStream(zipPackageSri, uri);51:52: // Convert the stream to an Image.
53: BitmapImage bi = new BitmapImage();
54: bi.SetSource(imageSri.Stream);55: Image img = new Image();
56: img.Source = bi;57:58: return img;
59: }60: }61: }
另外一个读取资源文件的例子:http://www.silverlightexamples.net/post/How-to-Get-Files-From-Resources-in-Silverlight-20.aspx
问题:如何在Silverligth Class Library项目中读取web.config?
答案:用上面的方法2,然后用 (Application.Current as App/*或者你自己的公共接口Interface*/).YourPublicParamDictionary[“key”] 的方法调用即可
本文来自Mainz的博客,原文地址:http://www.cnblogs.com/Mainz/archive/2011/02/22/1961576.html
- Silverlight读取Web.config配置文件
- Silverlight读取web.config配置文件
- web.config配置文件读取
- Silverlight——读取宿主web.config
- silverlight 实现 读取app.config 或 web.config
- Asp.net Web.config配置文件读取。
- Silverlight学习笔记——读取宿主web.config
- Silverlight读取webconfig配置文件
- 配置文件的读取(.config)
- 动态读取配置文件 config
- ConfigParser 读取 config 配置文件
- web和winform中读取config配置文件的方法
- atlas Web.Config 配置文件
- web.config配置文件示例
- web.config配置文件
- web.config 配置文件
- web.config 配置文件
- 配置Web.config配置文件
- 浅谈爱情
- Boost.bind 的使用
- 线性回归介绍之三——线性回归的使用条件
- Object-C基础—属性的介绍与使用
- 高IQ孩子的特征[图]
- Silverlight读取web.config配置文件
- 线性回归介绍之四——如何做散点图
- php通过socket向应用程序发数据
- 【javascript】javascript中的闭包
- CAT表中EMM PID 和 PMT表中ECM 的解析
- poj 1463
- NSObject:类与协议
- 正确使用Block避免Cycle Retain和Crash
- 线性回归介绍之五——回归与方差分析的关系