[Silverlight 4] 參數的傳遞方法

Silverlight都會有一個專案叫 *.Web,有個ManagePage.aspx裝戴Silverlight元件,也是應用程式的入口

然後還會有一個專案(此處叫ManageBack),會編譯成Silverlight .xap元件,供上面裝戴

ManageBack的App.xaml可提供應用程事週期的各種事件,像Global.ascx

如果我們寫了Web.config,要讓App.xaml可以在Application_Startup取得

具體做法如下

1.在ManagePage.aspx.cs取得WebConfig的參數

hostType = ConfigurationManager.AppSettings["hostType"];
if (ConfigurationManager.AppSettings["MagURL"] != null) MagURL = ConfigurationManager.AppSettings["MagURL"];
if (ConfigurationManager.AppSettings["MagBakURL"] != null) MagBakURL = ConfigurationManager.AppSettings["MagBakURL"];

  

2.在ManagePage.aspx <object></object>內 以InitParm封資料

<object data="data:application/x-silverlight-2," type="application/x-silverlight-2" width="100%" height="100%" id="silverlightControl">
<param name="source" value="ClientBin/ManageBack.xap" />
<param name="onError" value="onSilverlightError" />
<param name="background" value="white" />
<param name="enableHtmlAccess" value="true" />
<param name="minRuntimeVersion" value="4.0.50826.0" />
<param name="autoUpgrade" value="true" />
<a href="http://go.microsoft.com/fwlink/?LinkID=149156&v=4.0.50826.0" style="text-decoration: none">
<img src="http://go.microsoft.com/fwlink/?LinkId=161376" alt="获取 Microsoft Silverlight"
style="border-style: none" />
</a>

<!--here-->
<param name="initParams" value="ModeType=<%=this.hostType %>,MagURL=<%=this.MagURL %>,MagBakURL=<%=this.MagBakURL %>" />
</object>

  

3.在App.xaml.cs 的Application_Startup,就可以用StartupEventArgs取得值

private void Application_Startup(object sender, StartupEventArgs e)
        {
            LoginPage page = new LoginPage();
            System.Windows.Browser.HtmlPage.RegisterScriptableObject("SilverlightApplication", page);

            this.RootVisual = rootGrid;
            rootGrid.Children.Add(page);

            bool registerResult = WebRequest.RegisterPrefix("http://", WebRequestCreator.ClientHttp);
            bool httpsResult = WebRequest.RegisterPrefix("https://", WebRequestCreator.ClientHttp);

            int mode = 0;
            if (e.InitParams.ContainsKey("ModeType"))
            {
                if (Int32.TryParse(e.InitParams["ModeType"], out mode) == false)
                {
                    mode = 2;
                }
            }
            if (e.InitParams.ContainsKey("MagURL"))
            {
                MagURL = e.InitParams["MagURL"];
            }
            if (e.InitParams.ContainsKey("MagBakURL"))
            {
                MagBakURL = e.InitParams["MagBakURL"];
            }
            App.Mode = mode;
        }

  

posted on 2019-11-15 10:10  seako  阅读(138)  评论(0编辑  收藏  举报