[Wap]自定义asp.net mobile control

简介:

[Wap]自定义asp.net mobile control

编写者

日期

关键词

郑昀@ultrapower

2005-7-28

Wap ASP.NET Mobile control device adapter

 

Device Adapter概念

按照MSDNWalkthrough: Adding Support for Devices》的指示:

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

 

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

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

 

第一步,下载http://go.microsoft.com/fwlink/?LinkId=6350Device 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

{

    /// <summary>

    /// ListAdapter 的摘要说明。

    /// 首先利用下面的命令编译出一个ListAdapter.dll

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

    /// 其次,将以下的配置添加入web.configmobileControls节点下:

    /// <device name="UltraListDeviceAdapters"

    /// inheritsFrom="WmlListAdapter">

    /// <control name="System.Web.UI.MobileControls.Form"

    /// adapter="iUltraMobiles.ListAdapter, iUltraMobiles" />

    /// </device> 

/// <see cref="http://www.cnblogs.com/zhengyun_ustc/archive/2005/07/28/customcuildyourmobilecontrol.html"/>
///
 <seealso cref="http://msdn.microsoft.com/library/default.asp?url=/library/en-us/mwsdk/html/mwlrfdevice.asp"/>

    /// </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节点下

<!-- 指定没有 COOKIE 的数据字典类型

        这将使字典的内容出现在本地请求 URL 查询字符串中。

        这是在没有 Cookie 的设备上进行窗体身份验证所必需的。

    -->

    <!--在您使用像 useRandomID 这样的自定义属性时,必须在您的移动 Web 应用程序中启用自定义属性allowCustomAttributes

    -->

    <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>

    </mobileControls>

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

试用新控件

现在你已经修改了mobile:list控件的最终渲染方式,在它原本输出的诸多个<a> tag之前抢先输出了你的image的符合wml规范的tag

这样,你的aspx页面不需要做任何改动,重新编译你的工程后,新的渲染方式就生效了。

你可以在M3gate模拟器上试验。

 

附录  mobileControls例子:

下面给一个比较完整的例子做示范:

<!-- Adapter configuration for mobile controls used in the portal -->

        <mobileControls>

            <device name="PortalHtmlDeviceAdapters" inheritsFrom="HtmlDeviceAdapters">

                <control name="ASPNetPortal.MobileControls.TabbedPanel, Portal" adapter="ASPNetPortal.MobileControls.HtmlTabbedPanelAdapter,Portal" />

                <control name="ASPNetPortal.MobileControls.LinkCommand, Portal" adapter="ASPNetPortal.MobileControls.HtmlLinkCommandAdapter,Portal" />

            </device>

            <device name="PortalChtmlDeviceAdapters" inheritsFrom="ChtmlDeviceAdapters">

                <control name="ASPNetPortal.MobileControls.TabbedPanel, Portal" adapter="ASPNetPortal.MobileControls.ChtmlTabbedPanelAdapter,Portal"/>

            </device>

            <device name="PortalWmlDeviceAdapters" inheritsFrom="WmlDeviceAdapters">

                <control name="ASPNetPortal.MobileControls.TabbedPanel, Portal" adapter="ASPNetPortal.MobileControls.WmlTabbedPanelAdapter,Portal" />

            </device>

        </mobileControls>

 

附录  mobileControls属性定义表:

      mobileControls节点的具体各项属性定义可以参见:

      http://msdn.microsoft.com/library/default.asp?url=/library/en-us/mwsdk/html/mwlrfdevice.asp

     The <mobileControls> element has the attributes shown in the following table.

Attributes of the <device> subtag

Description

name

Specifies the unique name by which you must identify the device adapter set.

inheritsFrom

An optional reference to another device adapter set, from which this set must inherit. The specified device adapter set can either appear earlier in the same <mobileControls> section, or in the <mobileControls> section of a higher-level configuration file.

predicateClass

Specifies the class type that supplies the evaluator predicate. The name that you use for the class type must follow the .NET Framework standards for specifying a fully qualified type name.

ASP.NET searches the specified assembly for the type. If the adapter set inherits from another type, the predicateClass attribute is not necessary and will default to the parent set's value.

predicateMethod

Specifies the method that supplies the evaluator predicate. The method must be static, and of the following signature:

static bool EvaluatorMethod(HttpContext context)

If the adapter set inherits from another adapter set, the predicateMethodattribute is not necessary, and will default to the parent set's value.

pageAdapter

Specifies the class type of the page adapter for the adapter set. The specified class must implement the IPageAdapter interface. The name must follow the .NET standards for specifying a fully qualified type name.

ASP.NET searches the specified assembly for the type. If the adapter set inherits from another adapter set, the pageAdapter attribute is not necessary, and will default to the parent set's value.

      

附录实例代码:


        '*********************************************************************
        '
        ' HtmlTabbedPanelAdapter.Render Method
        '
        ' Renders the control. The TabbedPanel is rendered as one or more
        ' rows of tabs that the user can click on to move between tabs.
        '
        '*********************************************************************

        Public Overloads Overrides Sub Render(ByVal writer As HtmlMobileTextWriter)
        
            Dim _activePane As IPanelPane = Control.ActivePane
            Dim tabsPerRow As Integer = Control.TabsPerRow
            Dim panes As PanelPaneCollection = Control.Panes
            Dim paneCount As Integer = panes.Count

            ' Figure out the number of visible panes.
            Dim visiblePanes(paneCount) As Integer
            Dim visiblePaneCount As Integer = 0
            Dim As Integer

            For i = 0 To paneCount - 1

                If CType(panes(i), Control).Visible Then

                    visiblePanes(visiblePaneCount) = i
                    visiblePaneCount += 1

                End If

            Next i

            ' Calculate how many rows are necessary.
            Dim rows As Integer = (visiblePaneCount + tabsPerRow - 1) / tabsPerRow

            ' make sure tabsPerRow doesn't exceed the number of visible panes
            If Control.TabsPerRow > visiblePaneCount Then
                tabsPerRow = visiblePaneCount
            Else
                tabsPerRow = Control.TabsPerRow
            End If

            ' Open the table.
            writer.WriteBeginTag("table")
            writer.WriteAttribute("cellspacing", "0")
            writer.WriteAttribute("cellpadding", "2")
            writer.WriteAttribute("border", "0")
            writer.WriteLine(">")

            Dim row As Integer

            For row = rows - 1 To 0 Step -1

                writer.WriteFullBeginTag("tr")
                writer.WriteLine()

                Dim col As Integer

                For col = 0 To tabsPerRow - 1
                    writer.WriteBeginTag("td")
                    writer.WriteAttribute("width", "0")
                    writer.Write(">")
                    writer.WriteEndTag("td")

                    = row * tabsPerRow + col
                    If row > 0 And i >= visiblePaneCount Then

                        writer.WriteFullBeginTag("td")
                        writer.WriteEndTag("td")
                        Goto ContinueNextCol

                    End If

                    Dim index As Integer = visiblePanes(i)
                    Dim child As IPanelPane = panes(index)

                    If child Is _activePane Then

                        writer.WriteBeginTag("td")
                        writer.WriteAttribute("bgcolor", GetColorString(Control.ActiveTabColor, "#333333"))
                        writer.Write(">")

                        writer.WriteBeginTag("font")
                        writer.WriteAttribute("face", "Verdana")
                        writer.WriteAttribute("size", "-2")
                        writer.WriteAttribute("color", GetColorString(Control.ActiveTabTextColor, "#000000"))
                        writer.Write(">")

                        writer.WriteFullBeginTag("b")
                        writer.Write(" ")
                        writer.WriteText(child.Title, True)
                        writer.Write(" ")
                        writer.WriteEndTag("b")

                        writer.WriteEndTag("font")

                        writer.WriteEndTag("td")
                        writer.WriteLine()
                    Else
                        writer.WriteBeginTag("td")
                        writer.WriteAttribute("bgcolor", GetColorString(Control.TabColor, "#cccccc"))
                        writer.Write(">")

                        writer.WriteBeginTag("font")
                        writer.WriteAttribute("face", "Verdana")
                        writer.WriteAttribute("size", "-2")
                        writer.WriteAttribute("color", GetColorString(Control.TabTextColor, "#000000"))
                        writer.Write(">")

                        writer.Write(" ")
                        writer.WriteBeginTag("a")
                        RenderPostBackEventAsAttribute(writer, "href", index.ToString())
                        writer.Write(">")
                        writer.WriteText(child.Title, True)
                        writer.WriteEndTag("a")
                        writer.Write(" ")

                        writer.WriteEndTag("font")

                        writer.WriteEndTag("td")
                        writer.WriteLine()
                    End If
ContinueNextCol:
                Next col
                writer.WriteEndTag("tr")
                writer.WriteLine()

                If row > 0 Then
                    writer.WriteFullBeginTag("tr")
                    writer.WriteBeginTag("td")
                    writer.WriteAttribute("height", "1")
                    writer.Write(">")
                    writer.WriteEndTag("td")
                    writer.WriteEndTag("tr")
                    writer.WriteLine()
                End If
            Next row

            writer.WriteEndTag("table")
            writer.WriteLine()

            writer.WriteBeginTag("table")
            writer.WriteAttribute("width", "100%")
            writer.WriteAttribute("height", "2")
            writer.WriteAttribute("border", "0")
            writer.WriteAttribute("cellspacing", "0")
            writer.WriteAttribute("bgcolor", "#000000")
            writer.Write(">")
            writer.WriteFullBeginTag("tr")
            writer.WriteFullBeginTag("td")
            writer.WriteEndTag("td")
            writer.WriteEndTag("tr")
            writer.WriteEndTag("table")
            writer.WriteBreak()

            CType(_activePane, Control).RenderControl(writer)

        End Sub


编写者

日期

关键词

郑昀@ultrapower

2005-7-28

Wap ASP.NET Mobile control device adapter

 

目录
相关文章
|
开发框架 JSON .NET
ASP.NET Core 自定义配置警告信息
自定义配置警告信息需要在 startup 类中的 ConfigureService 方法中进行配置示例: // 注册 控制器服务 services.AddControllers(configure: setup => { setup.ReturnHttpNotAcceptable = true; ...
79 0
|
XML 存储 JSON
使用自定义XML配置文件在.NET桌面程序中保存设置
本文将详细介绍如何在.NET桌面程序中使用自定义的XML配置文件来保存和读取设置。除了XML之外,我们还将探讨其他常见的配置文件格式,如JSON、INI和YAML,以及它们的优缺点和相关的NuGet类库。最后,我们将重点介绍我们为何选择XML作为配置文件格式,并展示一个实用的示例。
123 0
|
5天前
|
Windows
.NET 隐藏/自定义windows系统光标
【10月更文挑战第20天】在.NET中,可以使用`Cursor`类来控制光标。要隐藏光标,可将光标设置为`Cursors.None`。此外,还可以通过从文件或资源加载自定义光标来更改光标的样式。例如,在表单加载时设置`this.Cursor = Cursors.None`隐藏光标,或使用`Cursor.FromFile`方法加载自定义光标文件,也可以将光标文件添加到项目资源中并通过资源管理器加载。这些方法适用于整个表单或特定控件。
|
2月前
|
开发框架 .NET Docker
【Azure 应用服务】App Service .NET Core项目在Program.cs中自定义添加的logger.LogInformation,部署到App Service上后日志不显示Log Stream中的问题
【Azure 应用服务】App Service .NET Core项目在Program.cs中自定义添加的logger.LogInformation,部署到App Service上后日志不显示Log Stream中的问题
|
4月前
|
安全 程序员 Shell
老程序员分享:NSIS自定义界面,下载并安装Net.Framework4.8
老程序员分享:NSIS自定义界面,下载并安装Net.Framework4.8
|
4月前
|
存储 分布式计算 大数据
MaxCompute操作报错合集之自定义udf的函数,引用了import net.sourceforge.pinyin4j.PinyinHelper;但是上传资源后,出现报错,是什么原因
MaxCompute是阿里云提供的大规模离线数据处理服务,用于大数据分析、挖掘和报表生成等场景。在使用MaxCompute进行数据处理时,可能会遇到各种操作报错。以下是一些常见的MaxCompute操作报错及其可能的原因与解决措施的合集。
|
5月前
|
XML API 数据库
七天.NET 8操作SQLite入门到实战 - 第六天后端班级管理相关接口完善和Swagger自定义配置
七天.NET 8操作SQLite入门到实战 - 第六天后端班级管理相关接口完善和Swagger自定义配置
124 0
|
11月前
|
Windows
基于.Net Core实现自定义皮肤WidForm窗口
基于.Net Core实现自定义皮肤WidForm窗口
105 0
|
12月前
|
开发框架 中间件 .NET
ASP.NET CORE 自定义中间件
ASP.NET CORE 自定义中间件
62 0
|
存储
.NET Core - 自定义配置数据源:低成本实现定制化配置方案
.NET Core - 自定义配置数据源:低成本实现定制化配置方案