[Wap] 制作自定义WmlListAdapter来实现Mobile.List控件的各种效果

本文介绍如何通过自定义WML ListAdapter实现Mobile.List控件的横排与图片效果,包括添加图片属性及控制换行。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

<iframe align="center" marginwidth="0" marginheight="0" src="http://www.zealware.com/csdnblog336280.html" frameborder="0" width="336" scrolling="no" height="280"></iframe>

[Wap] 制作自定义WmlListAdapter来实现Mobile.List控件的各种效果

编写者

日期

关键词

郑昀@ultrapower

2005-8-18

Wap ASP.NET Mobile control device adapter

自定义的mobile.List的横排效果

现有的mobile.List输出效果,每一个Item之间一定会换行,如果你看了WmlListAdapter的源代码就知道了,这是因为他们在输出每一个Item渲染时调用了RenderLink(writer, item.Value, null, false, false, item.Text, true);,最后一个参数就是是否在本Item渲染后输出换行标记。

我们可以改变这种渲染方式,从而让mobile.List变成横排效果。

自定义的mobile.List的图片效果

现有的mobile.List输出效果,每一个Item前后无法插入一张image

我们可以改变这种渲染方式,从而让mobile.List变成可以自定义每一行的插入图片效果。

总的效果如下图所示:

<shapetype id="_x0000_t75" coordsize="21600,21600" o:spt="75" o:preferrelative="t" path="m@4@5l@4@11@9@11@9@5xe" filled="f" stroked="f"><stroke joinstyle="miter"></stroke><formulas><f eqn="if lineDrawn pixelLineWidth 0"></f><f eqn="sum @0 1 0"></f><f eqn="sum 0 0 @1"></f><f eqn="prod @2 1 2"></f><f eqn="prod @3 21600 pixelWidth"></f><f eqn="prod @3 21600 pixelHeight"></f><f eqn="sum @0 0 1"></f><f eqn="prod @6 1 2"></f><f eqn="prod @7 21600 pixelWidth"></f><f eqn="sum @8 21600 0"></f><f eqn="prod @7 21600 pixelHeight"></f><f eqn="sum @10 21600 0"></f></formulas><path o:extrusionok="f" gradientshapeok="t" o:connecttype="rect"></path><lock v:ext="edit" aspectratio="t"></lock></shapetype>



Device Adapter
概念

可以参考我的上一篇文章《[Wap]编译Adapter来自定义mobile control》,http://www.cnblogs.com/zhengyun_ustc/archive/2005/07/28/customcuildyourmobilecontrol.html。

所有的ASP.NET mobile device adapter都是通过text writerrender的。这些text writer均继承自MobileTextWriter它提供了Write, WriteLine, 以及WriteBeginTag等方法。对于WML来说,这个Text WriterSystem.Web.UI.MobileControls.Adapters.WmlMobileTextWriter

下面,我们来介绍一下步骤:

自定义一个Adapter

在这里我们新建一个类库项目,叫做“ListAdapter”,它将生成一个ListAdapter.Dll,我们的自定义控件以及Adapter就在这个Dll中。

在这个项目中,我们将定义一个继承自

System.Web.UI.MobileControls.Adapters.WmlListAdapter

Adapter,来准备改写mobile:list控件的输出方式。

将下面的代码保存为ListAdapter.cs

ListAdapter.cs

using System.Web.UI;

using System.Web.UI.MobileControls.Adapters;

using System.Web.UI.MobileControls;

namespace ultraWmlAdapter

{

public class ImageList : System.Web.UI.MobileControls.List

{

private string _img="";

public string Image

{

get{ return this._img; }

set{ this._img=value; }

}

}

public class WmlImgListAdapter :System.Web.UI.MobileControls.Adapters.WmlListAdapter

{

protected new ImageList Control

{

get

{

return (ImageList)base.Control;

}

}

public override void Render(

WmlMobileTextWriter writer)

{

log.DebugTrace("ImgListAdapter Render Begin:"

+ this.Control.UniqueID);

if(Control.HasControls())

{

writer.BeginCustomMarkup();

RenderChildren(writer);

writer.EndCustomMarkup();

return;

}

int pageStart = Control.FirstVisibleItemIndex;

int pageSize = Control.VisibleItemCount;

if (pageSize == 0)

{

return;

}

/*

* 我们在上面自定义了Image属性,那么这里我们可以通过Control.Image来获取它,

* aspx页面上,它可能是这么定义的,<imagelist image="Images/5.gif"></imagelist>

* 这样,我们就可以直接通过Control.Image获取字符串"Images/5.gif"

* zhengyun_ustc 20050819

*

*/

String strListImageUrl = (String)Control.Image;

if(strListImageUrl != null

&& strListImageUrl.Length > 0)

{

writer.WriteBeginTag("img");

writer.WriteAttribute("src", strListImageUrl);

writer.WriteLine(" />");

}

System.Web.UI.MobileControls.MobileListItemCollection items = Control.Items;

if (items.Count == 0)

{

return;

}

bool itemsAsLinks = Control.ItemsAsLinks;

bool hasCmdHandler = Control.HasItemCommandHandler;

writer.EnterStyle(Style);

for (int i = 0; i

{

System.Web.UI.MobileControls.MobileListItem item = items[pageStart + i];

/*

* 请注意这里不能用BreakAfter的名字作为本ItemCustomAttributes

* 因为好像和原先的BreakAfter冲突;所以这里的CustomAttributes名字最好都起一个

* 比较特殊的,不与原先mobile:List的属性们冲突!

* zhengyun_ustc 20050819

*/

String strBreakAfter = (String)item.CustomAttributes["BreakAfterItem"];

// 默认都换行!

bool bBreakAfterItem = true;

if(strBreakAfter != null

&& strBreakAfter.Length > 0)

{

strBreakAfter = strBreakAfter.ToLower();

switch(strBreakAfter)

{

case "false":

// 要求渲染时不换行

bBreakAfterItem = false;

break;

default:

break;

}

}

if (itemsAsLinks)

{

RenderLink(writer, item.Value, null, false, false, item.Text, bBreakAfterItem);

}

else if (hasCmdHandler)

{

RenderPostBackEvent(writer, item.Index.ToString(), null, true, item.Text, bBreakAfterItem);

}

else

{

writer.RenderText(item.Text, bBreakAfterItem);

}

/*

* 我们知道MobileListItem有一个CustomAttributes成员,

* Returns the set of custom attributes defined for the control.

* 也就是说我们通过"item.CustomAttributes",可以得到当前这个Control的自定义属性的集合;

* 这样如果我们这样定义List的某一个Item

* <item text="1" value="1" image="Images/2.png" breakafteritem="True"></item>

* 那么可以通过item.CustomAttributes["Image"]获取Image属性的值;

* zhengyun_ustc 20050819

*

*/

String strCurrentListItemImageUrl = (String)item.CustomAttributes["Image"];

if(strCurrentListItemImageUrl != null

&& strCurrentListItemImageUrl.Length > 0)

{

writer.WriteBeginTag("img");

writer.WriteAttribute("src", strCurrentListItemImageUrl);

writer.WriteLine(" />");

}

}

writer.ExitStyle(Style);

log.DebugTrace("ImgListAdapter Render End");

}

}

做一个说明,我们的ImageList类是从System.Web.UI.MobileControls.List继承下来的,增加了一个Image属性作准备。

另外,自定义的WmlImgListAdapter就是继承了

System.Web.UI.MobileControls.Adapters.WmlListAdapter,所以我们要重写

Render(WmlMobileTextWriter writer)函数,从而让它做到下面两件事情:

通过

Web.config中的system.web节点下

String strListImageUrl = (String)Control.Image;

if(strListImageUrl != null

&& strListImageUrl.Length > 0)

{

writer.WriteBeginTag("img");

writer.WriteAttribute("src", strListImageUrl);

writer.WriteLine(" />");

}

来获取在aspx页面上定义的List自定义属性。这里的知识点就是,你可以在aspx中这么定义<imagelist id="NewList1" runat="server"><span lang="EN-US">Image="Images/5.gif" &gt;</span><span style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">,那么直接就可以通过</span><span lang="EN-US" style="FONT-SIZE: 10pt; FONT-FAMILY: 新宋体; mso-hansi-font-family: 'Times New Roman'; mso-font-kerning: 0pt">Control.Image</span><span style="FONT-SIZE: 10pt; FONT-FAMILY: 新宋体; mso-hansi-font-family: 'Times New Roman'; mso-font-kerning: 0pt">来获取这个“</span><span lang="EN-US">Images/5.gif</span><span style="FONT-SIZE: 10pt; FONT-FAMILY: 新宋体; mso-hansi-font-family: 'Times New Roman'; mso-font-kerning: 0pt">”字符串。这是当前<span lang="EN-US">List</span>控件总的<span lang="EN-US">Image</span>图片显示。<span lang="EN-US"><p></p></span></span></imagelist>

我们还可以通过在每一个Item商定以自定义属性Image来在List的每一项后面都输出一张图片。

Web.config中的system.web节点下

String strCurrentListItemImageUrl = (String)item.CustomAttributes["Image"];

if(strCurrentListItemImageUrl != null

&& strCurrentListItemImageUrl.Length > 0)

{

writer.WriteBeginTag("img");

writer.WriteAttribute("src", strCurrentListItemImageUrl);

writer.WriteLine(" />");

}

这里的知识点是,MobileListItem有一个CustomAttributes成员,它代表“Returns the set of custom attributes defined for the control.”。也就是说我们通过"item.CustomAttributes",可以得到当前这个Control的自定义属性的集合。

所以,如果我们这样定义List的某一个Item

<item text="1" value="1" image="Images/2.png" breakafteritem="True"></item>

那么可以通过item.CustomAttributes["Image"]获取Image属性的值了。

编译ListAdapter

编译出一个ListAdapter.dll,并设置输出路径为你的WAP应用程序bin目录下。

添加对ListAdapter.Dll的引用

在你的Wap项目中,添加对LisAdapter.dll的引用。

或者你也可以修改web.config,添加如下配置,也能起到相同的作用:

Web.config中的system.web节点下

-- 动态调试编译

设置 compilation debug="true" 以启用 ASPX 调试。否则,将此值设置为

false 将提高此应用程序的运行时性能。

设置 compilation debug="true" 以将调试符号(.pdb 信息)

插入到编译页中。因为这将创建执行起来

较慢的大文件,所以应该只在调试时将此值设置为 true,而在所有其他时候都设置为

false。有关更多信息,请参考有关

调试 ASP.NET 文件的文档。

-->

compilation defaultLanguage="c#" debug="true">

assemblies>

add assembly="ListAdapter"/>

add assembly="MultiLineInput"/>

assemblies>

compilation>

试用新控件

现在你已经修改了mobile:List控件的最终渲染方式,让我们试试看吧。

新建一个移动Web窗体页面“ImgList.aspx”,填写如下:

Web.config中的system.web节点下

Inherits="iPower.ImgList" AutoEventWireup="false" %>

<meta content="Microsoft Visual Studio .NET 7.1" name="GENERATOR">

<meta content="C#" name="CODE_LANGUAGE">

<meta content="http://schemas.microsoft.com/Mobile/Page" name="vs_targetSchema">

测试ImageList控件" runat="server">

cc1:ImageList id="NewList1" runat="server" ItemsPerPage="0" ItemsAsLinks="True" BreakAfter="False"

StyleReference="title" EnableViewState="False" Image="Images/5.gif" >

<item text="1" value="1" style="COLOR: blue"> Image="Images/1.png" BreakAfterItem="True"</item>>

<item text="2" value="2" style="COLOR: blue">Image="Images/2.png" BreakAfterItem="False"</item>>

<item text="3" value="3" style="COLOR: blue">Image="Images/3.png" BreakAfterItem="False"</item>>

<item text="4" value="4" style="COLOR: blue">Image="Images/5.gif" BreakAfterItem="False"</item>>

<item text="5" value="5" style="COLOR: blue"> Image="Images/1.png" BreakAfterItem="True"</item>>

cc1:ImageList>

cc1:ImageList id="NewList1" runat="server" ItemsPerPage="0" ItemsAsLinks="True" BreakAfter="False"

StyleReference="title" EnableViewState="False" Image="Images/5.gif" >

<item text="1" value="1" style="COLOR: blue"> Image="Images/1.png" BreakAfterItem="True"</item>>

<item text="2" value="2" style="COLOR: blue">Image="Images/2.png" BreakAfterItem="False"</item>>

<item text="3" value="3" style="COLOR: blue">Image="Images/3.png" BreakAfterItem="False"</item>>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值