appstatemanager.cs

来自「c#中用MapXtreme开发的地理信息系统」· CS 代码 · 共 158 行

CS
158
字号
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using MapInfo.Engine;
using MapInfo.Geometry;
using MapInfo.Mapping;
using MapInfo.WebControls;

/// <summary>
/// Summary description for AppStateManager
/// </summary>
namespace ApplicationStateManager
{
    /// <summary>
    /// State management can be complex operation. It is efficient to save and restore what is needed.
    /// The method used here is described in the BEST PRACTISES documentation. This is a template application
    /// which changes zoom, center, default selection and layer visibility. Hence we save and restore only these objects.
    /// </summary>
    [Serializable]
    public class AppStateManager : StateManager
    {
        private ManualSerializer _session = null;

        public AppStateManager()
        {
            _session = new ManualSerializer();
        }

        private Map GetMapObj(string mapAlias)
        {
            Map map = null;
            if (mapAlias == null || mapAlias.Length <= 0)
            {
                map = MapInfo.Engine.Session.Current.MapFactory[0];
            }
            else
            {
                map = MapInfo.Engine.Session.Current.MapFactory[mapAlias];
                if (map == null) map = MapInfo.Engine.Session.Current.MapFactory[0];
            }
            return map;
        }

        // Restore the state
        public override void RestoreState()
        {
            string mapAlias = ParamsDictionary[ActiveMapAliasKey] as string;
            Map map = GetMapObj(mapAlias);

            // If it was user's first time and the session was not dirty then save this default state to be applied later.
            // If it was a users's first time and the session was dirty then apply the default state  saved in above step to give users a initial state.
            if (IsUsersFirstTime())
            {
                if (IsDirtyMapXtremeSession(map))
                {
                    RestoreDefaultState(map);
                }
                else
                {
                    SaveDefaultState(map);
                }
            }
            else
            {
                // If it is not user's first time then restore the last state they saved
                RestoreZoomCenterState(map);
                // Just by setting it to temp variables the objects are serialized into session. There is no need to set them explicitly.

                ManualSerializer.RestoreMapXtremeObjectFromHttpSession("Layers");
                ManualSerializer.RestoreMapXtremeObjectFromHttpSession("Selection");
                ManualSerializer.RestoreMapXtremeObjectFromHttpSession("tempTable");
                ManualSerializer.RestoreMapXtremeObjectFromHttpSession("tempLayer");
            }
        }

        // Save the state
        public override void SaveState()
        {
            string mapAlias = ParamsDictionary[ActiveMapAliasKey] as string;
            Map map = GetMapObj(mapAlias);

            if (map != null)
            {
                SaveZoomCenterState(map);
                ManualSerializer.SaveMapXtremeObjectIntoHttpSession(map.Layers, "Layers");
                ManualSerializer.SaveMapXtremeObjectIntoHttpSession(MapInfo.Engine.Session.Current.Selections.DefaultSelection, "Selection");
                MapInfo.Mapping.IMapLayer lyr = map.Layers["Continent Layer"];
                if (lyr != null)
                {
                    MapInfo.Mapping.FeatureLayer fLyr = lyr as MapInfo.Mapping.FeatureLayer;
                    ManualSerializer.SaveMapXtremeObjectIntoHttpSession(fLyr.Table, "tempTable");
                    ManualSerializer.SaveMapXtremeObjectIntoHttpSession(fLyr, "tempLayer");
                }
            }
        }


        // This method checks if the mapinfo session got from the pool is dirty or clean
        private bool IsDirtyMapXtremeSession(Map map)
        {
            // Check if the MapXtreme session is dirty
            
            return (MapInfo.Engine.Session.Current.CustomProperties["DirtyFlag"] != null);
        }

        // Check if this is user's first time accessing this page. IF there is a zoom value in the asp.net session then it is not user's first time.
        private bool IsUsersFirstTime()
        {
            return (HttpContext.Current.Session[StateManager.GetKey("Zoom")] == null);
        }

        // When the session is not dirty these values are initial state of the session.
        private void SaveDefaultState(Map map)
        {
            HttpApplicationState application = HttpContext.Current.Application;
            if (application["DefaultZoom"] == null)
            {
                // Store default selection
                application["DefaultSelection"] = ManualSerializer.BinaryStreamFromObject(MapInfo.Engine.Session.Current.Selections.DefaultSelection);
                // Store layers collection
                application["DefaultLayers"] = ManualSerializer.BinaryStreamFromObject(map.Layers);
                // Store the original zoom and center.
                application["DefaultCenter"] = map.Center;
                application["DefaultZoom"] = map.Zoom;
            }
            // This MapXtreme object should be marked as dirty
            MapInfo.Engine.Session.Current.CustomProperties["DirtyFlag"] = true;
        }

        // When session is dirty but it is first time for user, this will be applied to give users it's initial state
        private void RestoreDefaultState(Map map)
        {
            HttpApplicationState application = HttpContext.Current.Application;
            // Get the default layers, center, and zoomfrom the Application. Clear Layers first, 
            //this resets the zoom and center which we will set later
            map.Layers.Clear();

            //Just by deserializing the binary stream we reset the MapFactory Deault layers collection
            byte[] bytes = application["DefaultLayers"] as byte[];
            Object obj = ManualSerializer.ObjectFromBinaryStream(bytes);

            // For default selection
            bytes = application["DefaultSelection"] as byte[];
            obj = ManualSerializer.ObjectFromBinaryStream(bytes);

            // For zoom and center
            map.Zoom = (MapInfo.Geometry.Distance)application["DefaultZoom"];
            map.Center = (DPoint)application["DefaultCenter"];
        }
    }
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?