[Wap]自定义asp.net mobile control

[Wap] 自定义 asp.net mobile control

编写者

|

日期

|

关键词

---|---|---

郑昀 @ultrapower

|

2005-7-28

|

Wap ASP.NET Mobile control device adapter

Device Adapter 概念

按照 MSDN 《 Walkthrough: Adding Support for Devices 》的指示:

我们要想自定义 MMIT( Microsoft Mobile Internet Toolkit) 提供的控件,那么可以改变 Adapter 在最后关头的渲染工作。

首先,我们要说明 render 的概念,最好的动画教程就是 http://www.asp.net/mobile/2514A_01A001.swf ,它是 Mobile Web Application Architecture 的 flash 讲解 。

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

第一步,下载 http://go.microsoft.com/fwlink/?LinkId=6350 的 Device Adapter Code 源代码;

** 或者直接链接 ** ** MobileIT.exe ** **

**


http://www.microsoft.com/downloads/details.aspx?FamilyId=AE597F21-B8E4-416E-A28F-B124F41F9768&displaylang=en

第二步,编辑其中的 WmlTextBoxAdapter.cs 文件;

第三步,通过 csc.exe 生成出一个新的 Adapter DLL ;

第四步,配置 web.config ;

第五步,重新编译你的工程。

可惜呀, MobileIt.exe 下载不了。当然在 ASP.NET 2.0 中,是很容易地自定义 Adapter 的。

那么现在 ASP.NET 1.1 中,我们只好折衷采用下面的办法:

自定义一个 Adapter 类

在这里我们来定义一个继承自

System.Web.UI.MobileControls.Adapters.WmlListAdapter

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

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

** ListAdapter.cs ** **

**


using System;

using System.Collections;

using System.Web.UI.MobileControls.Adapters;

namespace iUltraMobiles

{

///

 1<summary>
 2
 3///  ListAdapter  的摘要说明。 
 4
 5///  首先利用下面的命令编译出一个  ListAdapter.dll  : 
 6
 7///  csc /t:library /r:System.Web.Mobile.dll ListAdapter.cs 
 8
 9///  其次,将以下的配置添加入  web.config  中  mobileControls  节点下: 
10
11///  <device inheritsfrom="WmlListAdapter" name="UltraListDeviceAdapters">
12
13///  <control adapter="iUltraMobiles.ListAdapter, iUltraMobiles" name="System.Web.UI.MobileControls.Form"></control>
14
15///  </device>   
16
17
18/// <see cref=" http://www.cnblogs.com/zhengyun_ustc/archive/2005/07/28/customcuildyourmobilecontrol.html"></see>   
19///  <seealso cref="http://msdn.microsoft.com/library/default.asp?url=/library/en-us/mwsdk/html/mwlrfdevice.asp"></seealso>
20
21///  </summary>

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

{

public override void Render(

System.Web.UI.MobileControls.Adapters.WmlMobileTextWriter writer)

{

// Add your attributes here.

writer.WriteBeginTag("img");

writer.WriteAttribute("src","Images/1.png");

writer.WriteAttribute("alt", " 欢迎您 !");

writer.WriteLine(" />");

base .RenderChildren(writer);

}

}

}

编译 ListAdapter

利用下面的命令编译出一个 ListAdapter.dll :

csc /t:library /r:System.Web.Mobile.dll ListAdapter.cs

将这个 ListAdapter.DLL 复制到你的 WAP 应用程序 bin 目录下。

修改 web.config 来提供 control mapping

在你的 web.config 文件中找到 mobileControls 节点,修改为以下:

** Web.config ** ** 中的 ** ** system.web ** ** 节点下 ** **

**


< mobileControls allowCustomAttributes ="true" cookielessDataDictionaryType ="System.Web.Mobile.CookielessData" >

< device name ="UltraListDeviceAdapters"

inheritsFrom ="WmlDeviceAdapters">

< control name ="System.Web.UI.MobileControls.List"

adapter ="iUltraMobiles.ListAdapter, iUltraMobiles" /> device 节点就声明了一个新的 Adapter ,名为“ UltraListDeviceAdapters ”,这个名字是可以随便定义的。 inheritsFrom 属性是指。你必须提供控件的 fully qualified control class name :“ iUltraMobiles.ListAdapter, iUltraMobiles ”, control 的 name 属性指的是“ 你重载的哪一个 mobile 控件 ” 。

试用新控件

现在你已经修改了 mobile:list 控件的最终渲染方式,在它原本输出的诸多个

 1<a> tag  之前抢先输出了你的  image  的符合  wml  规范的  tag  。 
 2
 3这样,你的  aspx  页面不需要做任何改动,重新编译你的工程后,新的渲染方式就生效了。 
 4
 5你可以在  M3gate  模拟器上试验。 
 6
 7###  附录  A  mobileControls  例子: 
 8
 9下面给一个比较完整的例子做示范: 
10    
11    
12    <!-- Adapter configuration for mobile controls used in the portal -->
13
14&lt;span   
15  
16---</a>
Published At
Categories with Web编程
Tagged with
comments powered by Disqus