CS控件适配使用

       Community Server新版本又发布了,不知道大家有没有在研究,对于我来说还是狂复杂的,以前看过一段时间,想着什么时候把看的过程写下来,但总是由于各种的原因而耽搁了.现在把点点滴滴都给记录下来.因为学起来不知道从何下手,所以学到哪算到哪吧.

这次讲的是简单控件适配器的使用.

在.net2.0中,文本控件都实现了 ITextControl接口,按钮系列的控件都实现了 IButtonControl接口

(1)一般情况下,我们创建一个对象的时候可以用new关键字来创建

None.gif             Label testLabel  =   new  Label();
None.gif            Button testBtn 
=   new  Button();

(2)另外,我们可以做一个简单的工厂来创建,代码如下

None.gif public   class  TextManager
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    
public static ITextControl CreateLiteral()
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
return new Literal();
ExpandedSubBlockEnd.gif    }

InBlock.gif
InBlock.gif    
public static ITextControl CreateLabel()
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
return new Label();
ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}


None.gif ITextControl label  =  TextManager.CreateLabel();
None.gif        label.Text 
=   " hello " ;
None.gif        Controls.Add(label 
as  Label);

方便在哪里,即以后你只需要使用TextManager类来创建就可以了,不用再使用new关键字了.

现在发生了一点点的变化,不需要创建一个新的对象,我要获取一个对象,然后 针对接口编程

None.gif public   class  TextManager
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    
public static ITextControl CreateLiteral(Literal lit)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
return lit;
ExpandedSubBlockEnd.gif    }

InBlock.gif
InBlock.gif    
public static ITextControl CreateLabel(Label label)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
return label;
ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

假设页面上存在一个Label控件,则

None.gif      protected   void  Page_Load( object  sender, EventArgs e)
ExpandedBlockStart.gifContractedBlock.gif    
dot.gif {
InBlock.gif 
//ITextControl mylabel = Label1;
InBlock.gif
        ITextControl mylabel = TextManager.CreateLabel(Label1);
InBlock.gif        mylabel.Text 
= "hello";
ExpandedBlockEnd.gif    }


接着你还可以创建一个通用的Create方法,以上的如CreateLabel这些方法说实话几乎没用.
None.gif      public   static  ITextControl Create(Control cntrl)
ExpandedBlockStart.gifContractedBlock.gif    
dot.gif {
InBlock.gif        
if (cntrl == null)
InBlock.gif            
return null;
InBlock.gif
InBlock.gif        ITextControl it 
= cntrl as ITextControl;
InBlock.gif        
if (it == null)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
if (cntrl is Literal)
InBlock.gif                it 
= cntrl as Literal;
InBlock.gif            
else if (cntrl is Label)
InBlock.gif                it 
= cntrl as Label;
ExpandedSubBlockEnd.gif        }

InBlock.gif        
return it;
ExpandedBlockEnd.gif    }

到了这里,我们就可以抛弃上面的两个方法了,使用Create方法.

问题来了

现在我需要扩展ITextControl接口,添加新的一些东西

ExpandedBlockStart.gif ContractedBlock.gif      /**//// <summary>
InBlock.gif    /// Interface identifying a text-containing control.
ExpandedBlockEnd.gif    /// </summary>
None.gif      public   interface IText : ITextControl
ExpandedBlockStart.gifContractedBlock.gif     dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif        
bool Visible dot.gif{get;set;}
ExpandedSubBlockStart.gifContractedSubBlock.gif        Control Control dot.gifget;}
ExpandedBlockEnd.gif    }

那么我也需要 扩展原来实现ITextControl的控件实现IText接口

ContractedBlock.gif ExpandedBlockStart.gif
ExpandedBlockStart.gifContractedBlock.gif    /**//// <summary>
InBlock.gif    
/// CS Literal implemenation of Literal with IText
ExpandedBlockEnd.gif    
/// </summary>

None.gif    public class CSLiteral : Literal, IText
ExpandedBlockStart.gifContractedBlock.gif    
dot.gif{
InBlock.gif        
public Control Control
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif            
getdot.gifreturn this;}
ExpandedSubBlockEnd.gif        }

ExpandedBlockEnd.gif    }

None.gif
ExpandedBlockStart.gifContractedBlock.gif    
/**//// <summary>
InBlock.gif    
/// CS Label implemenation of Literal with IText
ExpandedBlockEnd.gif    
/// </summary>

None.gif    public class CSLabel : Label, IText
ExpandedBlockStart.gifContractedBlock.gif    
dot.gif{
InBlock.gif        
public Control Control
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif            
getdot.gifreturn this;}
ExpandedSubBlockEnd.gif        }

ExpandedBlockEnd.gif    }

最难的问题在这里,我们页面上用的控件都是Label和Literal,难道我们要把页面上的控件全换成CSLabel和CSLiteral?

我们只想针对接口编程,不过还是有点麻烦,你想用IText接口的话,你还是得把ITextControl换成IText,在老系统中CSLabel等新控件暂时无法发挥作用,我们需要进行 适配

代码如下

ContractedBlock.gifExpandedBlockStart.gif
ExpandedBlockStart.gifContractedBlock.gif    /**//// <summary>
InBlock.gif    
/// Literal helper wrapper for IText
ExpandedBlockEnd.gif    
/// </summary>

None.gif    public class LiteralWrapper : IText
ExpandedBlockStart.gifContractedBlock.gif    
dot.gif
{
InBlock.gif
InBlock.gif        
internal
 LiteralWrapper(Literal literal)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif
{
InBlock.gif            _literal 
=
 literal;
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
private Literal _literal = null;
InBlock.gif
ContractedSubBlock.gifExpandedSubBlockStart.gif        
IText Members
#region IText Members
InBlock.gif
InBlock.gif        
public bool Visible
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif
{
ExpandedSubBlockStart.gifContractedSubBlock.gif            
get dot.gifreturn _literal.Visible; }

ExpandedSubBlockStart.gifContractedSubBlock.gif            
set dot.gif{ _literal.Visible = value; }
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
public string Text
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif
{
ExpandedSubBlockStart.gifContractedSubBlock.gif            
get dot.gifreturn _literal.Text; }

ExpandedSubBlockStart.gifContractedSubBlock.gif            
set dot.gif{ _literal.Text = value; }
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
public Control Control
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif
{
ExpandedSubBlockStart.gifContractedSubBlock.gif            
get dot.gifreturn _literal; }

ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockEnd.gif        
#endregion

InBlock.gif
ExpandedBlockEnd.gif    }

None.gif
ExpandedBlockStart.gifContractedBlock.gif    
/**//// <summary>
InBlock.gif    
/// Lable helper wrapper for IText
ExpandedBlockEnd.gif    
/// </summary>

None.gif    public class LabelWrapper : IText
ExpandedBlockStart.gifContractedBlock.gif    
dot.gif
{
InBlock.gif        
InBlock.gif        
internal
 LabelWrapper(Label label)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif
{
InBlock.gif            _label 
=
 label;
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
private Label _label = null;
InBlock.gif
ContractedSubBlock.gifExpandedSubBlockStart.gif        
IText Members
#region IText Members
InBlock.gif
InBlock.gif        
public bool Visible
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif
{
ExpandedSubBlockStart.gifContractedSubBlock.gif            
getdot.gif{   return _label.Visible;}

ExpandedSubBlockStart.gifContractedSubBlock.gif            
setdot.gif{_label.Visible = value;}
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
public string Text
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif
{
ExpandedSubBlockStart.gifContractedSubBlock.gif            
getdot.gif{return _label.Text;}

ExpandedSubBlockStart.gifContractedSubBlock.gif            
setdot.gif{_label.Text = value;}
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
public Control Control
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif
{
ExpandedSubBlockStart.gifContractedSubBlock.gif            
getdot.gif{return _label;}

ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockEnd.gif        
#endregion

InBlock.gif
ExpandedBlockEnd.gif    }

None.gif
None.gif    
#endregion
None.gif
ExpandedBlockStart.gifContractedBlock.gif    
/**//// <summary>
InBlock.gif    
/// Utility class that attempts to convert a given control to an IText-supporting control.
ExpandedBlockEnd.gif    
/// </summary>

None.gif    public class TextManager
ExpandedBlockStart.gifContractedBlock.gif    
dot.gif
{
ExpandedSubBlockStart.gifContractedSubBlock.gif        
private TextManager()dot.gif{}

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// Creates an IText version of a Literal control.
InBlock.gif        
/// </summary>

InBlock.gif        
/// <param name="lit">The literal control.</param>
ExpandedSubBlockEnd.gif        
/// <returns></returns>

InBlock.gif        public static IText CreateLiteral(Literal lit)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif
{
InBlock.gif            
return new
 LiteralWrapper(lit);
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// Creates an IText version of a Label control.
InBlock.gif        
/// </summary>

InBlock.gif        
/// <param name="label">The label control.</param>
ExpandedSubBlockEnd.gif        
/// <returns></returns>

InBlock.gif        public static IText CreateLabel(Label label)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif
{
InBlock.gif            
return new
 LabelWrapper(label);
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// Attempts to create an IText version of the given control, otherwise null.
InBlock.gif        
/// </summary>

InBlock.gif        
/// <param name="cntrl">The control.</param>
ExpandedSubBlockEnd.gif        
/// <returns></returns>

InBlock.gif        public static IText Create(Control cntrl)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif
{
InBlock.gif            
if(cntrl == null
)
InBlock.gif                
return null
;
InBlock.gif
InBlock.gif            IText it 
= cntrl as
 IText;
InBlock.gif            
if(it == null
)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif
{
InBlock.gif                
if(cntrl is
 Literal)
InBlock.gif                    it 
= new LiteralWrapper(cntrl as
 Literal);
InBlock.gif                
else if (cntrl is
 Label )
InBlock.gif                    it 
= new LabelWrapper(cntrl as
 Label);
ExpandedSubBlockEnd.gif            }

InBlock.gif
InBlock.gif            
return it;
ExpandedSubBlockEnd.gif        }

ExpandedBlockEnd.gif    }


来看下类图






此都是 针对接口进行编程,忽略其他你不关心的功能,Manager类则充当工厂,当然有时候你并不需要这些东西,看需要吧
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值