随笔 - 27  文章 - 1 评论 - 83 trackbacks - 2

赞助商广告

与我联系

搜索

 

常用链接

留言簿(1)

随笔分类

随笔档案

文章分类

好友的Blog

牛人的Blog

积分与排名

  • 积分 - 12862
  • 排名 - 2791

最新评论

阅读排行榜

评论排行榜

      持久化的问题是一个很古老的问题,看似和我们的日常应用没有什么联系,但是一些情况下还是很有用处的。
 场景描述:

      应用场景介绍:

                                   CS下 (Windows程序)

                                           BS下(Web页面)

         做原型时候,没有确定BS和CS结构的情况下,需要构建一个简单的模型,使得你构建的简单界面既能支持cs又能支持BS。我们的目的是在cs情况下编辑,然后在BS下生成编辑好的页面。


  简单实现:


        下面只提供整个应用场景的拙劣实现,单单为了说明序列化和反序列化的问题:
           将WebMatrixServer的两个Dll(Microsoft.Matrix.dll,Microsoft.Matrix.WebHost.dll)引入工程,使程序自己变成可以显示页面的Web服务器(相当于IIS)。用WebBrowser控件来展示页面。
           以Button为例子,在cs模式下将button序列化为二进制文件。在asp.net下将序列化的文件反序列化出来。获取编辑控件的信息。
         
       
    public interface ISoildControl
    
{
        Point ControlPos 
getset; }
        
bool EditState getset; }
    }
    ISolidControl的目的是控制所有实现ISolidContorl接口的控件,并判别是否为编辑状态,所有控件在编辑状态下是可以拖动的。 这与本实例无关,仅作辅助说明。


  让类支持序列化和反序列化:

    
 [Serializable]
    
public class SolidButton : Button, ISoildControl, ISerializable
    
{
        
private Point CurPos;
        
private bool MoveFlag = false;
        
private bool isEdit = false;

        
public SolidButton(SerializationInfo info, StreamingContext ctxt)
        
{
         
            
this.Location = new Point((int)info.GetValue("X"typeof(int)),(int)info.GetValue("Y"typeof(int)));
            
this.Text = (String)info.GetValue("Text"typeof(string));
            
this.Height = (int)info.GetValue("Height"typeof(int));
            
this.Width = (int)info.GetValue("Width"typeof(int));
            
        }


        
public SolidButton()
        
{
       
            
this.MouseDown +=new MouseEventHandler(ButtonMouseDown);
            
this.MouseMove +=new MouseEventHandler(ButtonMouseMove);
            
this.MouseUp   +=new MouseEventHandler(ButtonMouseUp);
        }


        
private void ButtonMouseDown(object sender, MouseEventArgs e)
        
{
            

            MoveFlag 
= true;     
            CurPos 
= new Point(e.X, e.Y);
        }

        
        
private void ButtonMouseMove(object sender, MouseEventArgs e)
        
{

            
if (MoveFlag && isEdit)
            
{
                
int PosX, PosY;
                PosX 
= CurPos.X - e.X;
                PosY 
= CurPos.Y - e.Y;
                
this.Location = new Point(this.Location.X - PosX, this.Location.Y - PosY);
            }

        }

        
private void ButtonMouseUp(object sender, MouseEventArgs e)
        
{
             MoveFlag 
= false;
        }

        
public Point ControlPos
        
{
            
get return this.Location; }
            
set this.Location = value; }
        }

        
public bool EditState
        
{
            
get return isEdit;}
            
set { isEdit = value; }
        }



        
ISerializable Members

        
      
      
      
    }

  1.单单在类上声明[Serializable],不能将SolideButton序列化,提示他的父类不能被序列化。于是实现ISerializable接口。
  2. 如果不声明 public SolidButton(SerializationInfo info, StreamingContext ctxt) 的构造函数,就会报出没有反序列化构造函数的错误。 一定要记住,如果支持反序列化,必须实现这一类型的反序列化构造函数。

  实际应用:

   应用场景:
    CS下:
  
            Stream stream = File.OpenWrite(@"c:\prep.bin");
            
try
            
{
                BinaryFormatter bf 
= new BinaryFormatter();
                bf.Serialize(stream, button);
                stream.Close();
            }

            
catch (Exception ex)
            
{
                stream.Close();
         
            }


BS下:
          FileStream stream = new FileStream(@"c:\\prep.bin", FileMode.Open);
            BinaryFormatter bf 
= new BinaryFormatter();
            SolidButton sd 
= bf.Deserialize(stream) as SolidButton;
            
            
this.Button1.Text = sd.Text;
            
this.Button1.Style.Add("top", sd.Location.Y.ToString()+"px");
            
this.Button1.Style.Add("left", sd.Location.X.ToString() + "px");
            
this.Button1.Style.Add("width",sd.Width.ToString()+"px");
            
this.Button1.Style.Add("height", sd.Height.ToString() + "px");


            stream.Close();
其中Button1为asp.net中的Button控件。
  
posted on 2008-04-08 14:56 最远距离 阅读(3172) 评论(10)  编辑 收藏 所属分类: Asp.net

FeedBack:
#1楼  2008-04-08 17:29 leadnt.org [未注册用户]

还是没看懂有什么用....
  回复  引用    
#2楼  2008-04-08 17:57 Alex wu      
他这个是特定的场景,不是对所有人都有用。
我也用过,情况比较特殊,说起来麻烦,不说了。
  回复  引用  查看    
#3楼  2008-04-08 18:42 李涛      
感觉不错
但情况是有点特殊
  回复  引用  查看    
#4楼  2008-04-08 22:04 李战      
路过,歇歇!
  回复  引用  查看    
#5楼  2008-04-08 22:47 没有昵称      
貌似题目跟文章内容不符呵呵。。。
  回复  引用  查看    
#6楼  2008-04-08 22:48 没有昵称      
序列化只是实现这种需求的千千万万种方法中的一个。
  回复  引用  查看    
#7楼 [楼主] 2008-04-09 08:38 最远距离      
看来我的表达出现了问题,这个题目是为了说明序列化和反序列化的东西,其他只是辅助,我一再强调,不要让那些细枝末节蒙蔽了主要的内容。本文主要说明的也是序列化和反序列化的注意问题和使用,前两节也只是一个辅助。
  回复  引用  查看    
#8楼  2008-04-09 19:14 一水 [未注册用户]
简单,但很易懂
  回复  引用    
#9楼  2008-04-09 22:01 啊啊 [未注册用户]
额,没怎么看明白
  回复  引用    
#10楼  2008-05-19 00:03 爱在戏院前      
感觉不错,有创意!~~~
  回复  引用  查看    

标题  
姓名  
主页
Email (博主才能看到) 
验证码 *  看不清,换一张 [登录][注册]
内容(请不要发表任何与政治相关的内容)  
  博客园首页

  新闻频道

  社区

  小组

  博问

  网摘

  闪存

  登录  使用高级评论  新用户注册  返回页首  恢复上次提交      
该文被作者在 2008-04-09 08:40 编辑过
成果网帮您增加网站收入


相关链接: