Shaofh

Shaofh

SilverLight中Page也可使用泛型基类

最近在用SilverLight+MVC做项目时碰到问题,以往用Asp.net中WebForm做项目时都会在项目框架中使用继承自Page的泛型基类来实现增、删、查、改等基础方法。

但在SilverLight却无法使用,如以下代码无法成功编译

 public partial class CodeClassManage: SysPageBase<CodeClass>

{

//.......

}

//其基类SysPageBase实现了增、删、查、改等基础方法

public partial class SysPageBase<T> : Page
        where T:Entity,new()

{

//实现增、删、查、改等基础方法的过程略过

//...

}

因为CodeClassManage页面文件如下

<Bis:SysPageBase  x:Class="SLApp.CodeClassManage" 

Bis="clr-namespace:SLApp;assembly=SLApp"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d" >

 </Bis:SysPageBase>

 

红色代码部分无法使用泛型。

 

 在网上找了很久也未找到解决办法。于是想到了使用中间类来转一下,以上代码变为

public partial class CodeClassManage : TCodeClassManage
 {

//.....

}

//以下为多出来的中间类,其内容可为空

 public partial class TCodeClassManage: SysPageBase<CodeClass>

{

}

//其基类SysPageBase实现了增、删、查、改等基础方法

public partial class SysPageBase<T> : Page
        where T:Entity,new()

{

//实现增、删、查、改等基础方法的过程略过

//...

}

CodeClassManage页面文件如下

<Bis:TCodeClassManage x:Class="SLApp.CodeClassManage" 

Bis="clr-namespace:SLApp;assembly=SLApp"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d" >

 

 </Bis:TCodeClassManage >

 

 以上达到使用泛型基类的目的,虽然多出了个中间类TCodeClassManage ,但其内容可为空,还是可以忽略不记的。

 

posted on 2011-10-18 11:58  sh37  阅读(220)  评论(0编辑  收藏  举报

导航