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; // Uri
  3: using System.IO; // Stream
  4: using System.Windows; // Application
  5: using System.Windows.Controls; // TextBlock, Image
  6: using System.Windows.Media.Imaging; // BitmapImage
  7: using System.Net; // WebClient
  8: using System.Windows.Resources; // StreamResourceInfo
  9: 
 10: namespace SilverlightApplication
 11: {
 12:     public partial class PageLong : UserControl
 13:     {
 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

0 0
原创粉丝点击