Silverlight 实现session

由于silverlight运行在客户端,我们只能模拟出session  首先定义一个SessionManager类

 
using System;

using System.Net;

using System.Windows;

using System.Windows.Controls;

using System.Windows.Documents;

using System.Windows.Ink;

using System.Windows.Input;

using System.Windows.Media;

using System.Windows.Media.Animation;

using System.Windows.Shapes;

using System.Collections.Generic;



namespace WebUPGIS.controls

{

    public static class SessionManager

    {

        private static Dictionary<string, object> session = new Dictionary<string, object>();



        public static Dictionary<string, object> Session

        {

            get { return SessionManager.session; }

            set { SessionManager.session = value; }

        }



    }

}

再通过各种方式把asp.net的session传递给silverlight  例如
1.通过请求页面 **.apsx?key=value 传给一个包含silverlight的页面 然后 在sl中获取
 
           IDictionary<String, String> paras = HtmlPage.Document.QueryString;

            if (paras.ContainsKey("userName"))

            {

                this.button1.Content = paras["userName"];

            }
2.如果包含silverlight的页面是aspx页面 可以先在页面后台的load中获取session 存放在hidden里面
 
 
最后把session值保存到SessionManager中,例如
 
            HtmlDocument document = HtmlPage.Document;

            string hiddenStr = document.GetElementById("hiddenStr").GetAttribute("value");

            if(hiddenStr!="")

            {

                string[] userInfo = hiddenStr.Split(';');

                SessionManager.Session["userName"] = userInfo[0];

                SessionManager.Session["userID"] = userInfo[1];

            }



            this.button1.Content = SessionManager.Session["userName"].ToString();
 
在需要使用session的地方 用SessionManager.Session["sessionName"] 即可

你可能感兴趣的:(silverlight)