• SilverLight读取web.config配置文件


          今天,在写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;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;
            }

           最后,总结一下总体来说不是很复杂,个人觉得这种方式还是挺实用的。希望跟大家分享下。

  • 相关阅读:
    在Excel中查找/替换时使用换行符
    用fieldset标签轻松实现Tab选项卡效果
    用JSFL将Flash中的元件导出为PNG
    PHP学习笔记之PDO
    网页中的数学公式
    解决fl.findObjectInDocByType/fl.findObjectInDocByName的毛病
    HTML+CSS 网页中鼠标滚轮失效的解决办法
    jQuery 离开页面时提示
    ASP 计算出指定年份生肖
    au3创建Access数据库表例子
  • 原文地址:https://www.cnblogs.com/fan101113/p/2739225.html
Copyright © 2020-2023  润新知