Silverlight访问宿主WebSit的Web.config文件
Silverlight Application是客户端程序,没有也无法访问服务端的web.config,它自己也不允许添加.config文件, 因此Silverlight中的代码就无法直接像aspx中一样读取web.config。
Silverlight App是由一个Web Application来host的,而那个Web Application是可以方便地配置的,于是,可以考虑由网站来把配置传给Silverlight,宿主Silverlight的Page文件中会有这样一段代码:<param name="onError" value="onSilverlightError" />
<param name="background" value="white" />
<param name="minRuntimeVersion" value="4.0.50826.0" />
<param name="autoUpgrade" value="true" />
在Web Application中的web.config文件中添加如下内容:
<add key="GisMapUrl" value="http://lzy/CEIRAMap"/>
可以看到其中有一个param标签的name为InitParams,其值可以在App.xaml.cs中的Application_Startup事件处理方法中,使用传入StartupEventArgs参数的InitParams属性取得,类型为IDictionary<string, string>。下面来看一下具体的方法:
1. 首先,我们需要在Page文件中加入一个Literal控件,以便能够动态地将<param name="InitParams" value="" />赋到Page中,但是在Visual Studio 2010里,默认为我们创建的ASPX页面没有.cs文件,需要我们创建一个新的ASPX页面,把原先<HTML></HTML>当中的内容Copy过来,并加上Literal控件,如下方代码所示:
<asp:Literal ID="litInitParams" runat="server"></asp:Literal>
后台代码:
{
public partial class WebForm1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
DataSet data = new CEIRADataManager.CommonManager.UserManager().Role_getAll();
WriteInitParams();
}
private string _seperator = ",";
private void WriteInitParams()
{
NameValueCollection appSettings = WebConfigurationManager.AppSettings;
StringBuilder stringBuilder = new StringBuilder();
stringBuilder.Append("<param name=\"InitParams\" value=\"");
string paramContent = string.Empty;
for (int i = 0; i < appSettings.Count; i++)
{
if (paramContent.Length > 0)
{
paramContent += _seperator;
}
paramContent += string.Format("{0}={1}", appSettings.GetKey(i), appSettings[i]);
}
stringBuilder.Append(paramContent);
stringBuilder.Append("\" />");
this.litInitParams.Text = stringBuilder.ToString();
}
}
}
3. 在App.xaml.cs中的Application_Startup事件处理方法中,使用传入StartupEventArgs参数的InitParams属性取得,类型为IDictionary<string, string>:
public partial class App : Application
private IDictionary<string , string> _configurations;
public IDictionary<string , string> Configurations
{
get
{
return _configurations;
}
}
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 )
{
_configurations = e.InitParams;
this.RootVisual = new MainPage();
}
private void Application_Exit( object sender , EventArgs e )
{
}
private void Application_UnhandledException( object sender , ApplicationUnhandledExceptionEventArgs e )
{
if( !System.Diagnostics.Debugger.IsAttached )
{
e.Handled = true;
Deployment.Current.Dispatcher.BeginInvoke( delegate { ReportErrorToDOM( e ); } );
}
}
private void ReportErrorToDOM( ApplicationUnhandledExceptionEventArgs e )
{
try
{
string errorMsg = e.ExceptionObject.Message + e.ExceptionObject.StackTrace;
errorMsg = errorMsg.Replace( '"' , '\'' ).Replace( "\r\n" , @"\n" );
System.Windows.Browser.HtmlPage.Window.Eval( "throw new Error(\"Unhandled Error in Silverlight Application " + errorMsg + "\");" );
}
catch( Exception )
{
}
}
}
}
4. 最后就是在Silverlight页面当中获取InitParams的值并显示到页面上:
private IDictionary<string, string> _configurations;
public EvaluateResultMapView()
{
InitializeComponent();
_configurations = (Application.Current as App).Configurations;
GetRichContent(_configurations["GisMapUrl"], UriKind.Absolute);
}
短发