WSS3SDK之:演练--在mobile页面中创建自定义字段渲染控件

本演练将展示如何通过自定义字段渲染控件关联一个RenderingTemplate来定制mobile页面上的字段渲染。下面的样例展示了如何定制通知列表项的标题字段在mobile的显示项目,新建项目和编辑项目页面中的渲染。根据3类页面的不同,定制化的内容也不一样:

  • 显示窗体— 添加了一个搜索链接,使得用户可以导航到MSN新闻搜索结果页面。
  • 编辑窗体— 当到期日期一栏的值小于当前日期时添加了默认文本。
  • 新建窗体— 添加了默认文本来为用户展示待输入的值的特定格式。

必要要求

完成了前面的关于定制Mobile显示页面中列表项标题的演练。

 

准备自定义字段渲染控件的开发环境

  1. 在 Visual Studio里,选择Tools菜单下的External Tools
  2. External Tools 对话框中,点击Add 并在Title处输入 Get Assembly Public Key

  3. 通过浏览到sn.exe来填写 Command 文本框。他通常放在 C:\Program Files\Microsoft Visual Studio 8\SDK\v2.0\Bin\sn.exe.

  4. Arguments 文本框中,输入如下内容(区分大小写) -Tp "$(TargetPath)"

  5. 启用 Use Output window 选择框。

  6. 点击 OK。新的命令就添加到了 Tools 菜单。

 

新建自定义字段项目

  1. 在 Visual Studio中,选择File菜单下的 New Project

  2. New Project 对话框中,Project types选择 Visual C#Templates选择 Class Library ,并在Name 中输入ItemTitleField 。点击 OK

  3. Solution Explorer中右击 References 结点,点 Add Reference,然后按住 CTRL 键,在 Add Reference 对话框的.NET标签下选择 System.WebSystem.Web.Mobile ,和 Microsoft SharePoint Services 。点击 OK

  4. Solution Explorer中右击项目名称并选择 Properties

  5. Properties 对话框中的Application标签下,输入MyCompany.SharePoint.MobileControls.ItemTitleField 作为 Assembly name 以及 MyCompany.SharePoint.MobileControls 作为 Default namespace。将其中的 MyCompany 替换成您公司的名称。在整个演练中,都把MyCompany 替换成您公司的名称

  6. 打开 Signing 标签并选择 Sign the assembly

  7. Choose a strong name key file下拉列表框中选取<New...>
  8. Create Strong Name Key 对话框中,Key file name输入 ItemTitleField.snk ,并确保 Protect ... 选择框没有选中。点 OK。打开 Build Events 标签,在 Post-build event command line框中输入下面的内容。该代码确保了每次您编译该项目时,项目文件的最新版本会被拷贝到相应的位置,并且重启 Windows SharePoint Services 3.0 以便装载最新版本的组件文件。

     

    cd "$(ProjectDir)"
    "%programfiles%\microsoft visual studio 8\sdk\v2.0\bin\gacutil" /"$(TargetPath)" /nologo /f
    %systemroot%
    \system32\iisapp.vbs /"SharePoint_App_Pool" /r
    xcopy 
    *.ascx "C:\Program Files\Common Files\Microsoft Shared\web server extensions\12\TEMPLATE\CONTROLTEMPLATES\" /y

     

  9. 将其中的 SharePoint_App_Pool 替换成实际的分配给您的 Windows SharePoint Services Web 应用程序的IIS应用程序池名称。通常该名称与宿主应用程序的IIS网站名称相同;比如, "SharePoint - 80"。(如果名称中不含有空格的话引号可以省略)

  10. 点击标签上任何其他可用的控件,这样Visual Studio 会检测到变化并在标签文字后显示一个星号。
  11. 点击工具栏上的 Save all files 按钮。

  12. Solution Explorer里,将 Class1.cs重命名为ItemTitleField.cs

 

创建渲染控件

  1. 如果ItemTitleField.cs文件没有打开的话将其打开,然后添加下列using 语句:

    using System.Web.UI.MobileControls;
    using Microsoft.SharePoint;
    using Microsoft.SharePoint.MobileControls;
  2. 将命名空间改成 MyCompany.SharePoint.MobileControls

  3. 将整个 Class1 的定义替换为如下代码:

    public class ItemTitleField : SPMobileBaseTextField
    {

    }
    // end ItemTitleField class

     

    请注意,您新建的类继承自SPMobileBaseTextField
  4. 添加下面的代码来覆写 CreateControlForDisplay 方法:
    protected override MobileControl CreateControlForDisplay()
    {
       
    string title = Convert.ToString(this.ItemFieldValue);
       
    if (!String.IsNullOrEmpty(title))
       {
          
    this.LabelControl.BreakAfter = false;
          
    this.LabelControl.Text = title + " ";

          
    this.LinkControl.BreakAfter = false;
          
    this.LinkControl.Text = "Search";
          
    this.LinkControl.NavigateUrl = "http://search.msn.com/results.aspx?q=" + title.Replace(' ''+');

          Panel panel 
    = new Panel();
          panel.BreakAfter 
    = false;
          panel.Controls.Add(
    this.LabelControl);
          panel.Controls.Add(
    this.LinkControl);

          
    return panel;
       }
       
    return null;
    }

    请注意,该方法开头是获取当前列表项的Title字段当前值。该当前值存放在ItemFieldValue中。
  5. 添加下面的内容来覆写 CreateControlForNew 方法:
    protected override MobileControl CreateControlForNew()
    {
       MobileControl myNewControl 
    = null;
       
    if (this.Field != null)
       {
          
    string text = "Group: Project Name";
          
    if (!this.Page.IsPostBack)
          {
             
    this.TextBoxControl.Text = text;
          }
          myNewControl 
    = this.TextBoxControl;
       }
       
    return myNewControl;
    }
  6. 添加下面的内容来覆写 CreateControlForEdit方法:

    protected override MobileControl CreateControlForEdit()
    {
       MobileControl myEditControl 
    = null;
       
    if (this.Item != null && this.Field != null)
       {
          
    if (this.NeedEllipsisRendering)
          {
             myEditControl 
    = this.CreateControlForDisplay();
          }
          
    else
          {
             
    if (!this.Page.IsPostBack)
             {
                
    string strEdit = this.Field.GetFieldValueForEdit(this.ItemFieldValue);
                
    string overDue = "OVERDUE: ";

                SPListItem item 
    = this.ListItem;
                
    if (item["Expires"!= null)
                {
                   System.DateTime date 
    = (DateTime)item["Expires"];
                   
    if (date.CompareTo(System.DateTime.Today) < 0)
                   {
                      
    this.TextBoxControl.Text = overDue + strEdit;
                   }
                   
    else
                   {
                      
    this.TextBoxControl.Text = strEdit;
                   }
                }
             }
             myEditControl 
    = this.TextBoxControl;
          }
       }
       
    return myEditControl;
    }
  7. 选择Build 菜单中的Build 。您的演练还未完成,但是您需要在这里先编译一下该组件以便生成一个公钥令牌(Public Key Token)。

 

创建渲染模板

  1. Solution Explorer里,右击项目名称,ItemTitleField,然后选择AddNew Item

  2. Categories 中选择 Visual C# Project Items,并在Templates窗口中选Text File

  3. Name 框中,输入 AnnouncementsItemTitleField.ascx 然后点 Add。 (不要将文件放在项目的子文件夹中,否则之前您创建的 Post-build命令会找不到该文件)

  4. 在刚才创建的 AnnouncementsItemTitleField.ascx 文件中添加下列标记:

    <%@ Control Language="C#" %>
    <%@ Assembly Name="Microsoft.SharePoint, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %> 
    <%@ Register TagPrefix="mobile" Namespace="System.Web.UI.MobileControls" Assembly="System.Web.Mobile, Version=1.0.3300.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" %> 
    <%@ Register TagPrefix="SharePoint" Namespace="Microsoft.SharePoint.WebControls" Assembly="Microsoft.SharePoint, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %> 
    <%@ Register TagPrefix="SPMobile" Namespace="Microsoft.SharePoint.MobileControls" Assembly="Microsoft.SharePoint, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
    <%@ Register TagPrefix="CustomMobile" Namespace="MyCompany.SharePoint.MobileControls" Assembly="MyCompany.SharePoint.MobileControls.ItemTitleField, Version=1.0.0.0, Culture=neutral, PublicKeyToken=Token" %> 
    <%@ Import Namespace="Microsoft.SharePoint" %>

    <SharePoint:RenderingTemplate RunAt="Server" ID="MobileCustomListField_Announcements_Text_Title" >
        
    <Template>
          
    <CustomMobile:ItemTitleField RunAt="Server" /> 
        
    </Template>
    </SharePoint:RenderingTemplate>
  5. MyCompany 都替换成您公司的名称。

  6. Token 替换成实际的公钥令牌,您可以通过点击Tools 菜单下的Get Assembly Public Key 来得到。该密钥令牌将显示在输出窗口的最后一行。只需使用密钥的 token即可,而不是整个密钥。

    请注意,该文件与在上一个演练中的文件很类似。不同之处在:
         (1)上一个演练的这一行 
              <mobile:Label Text="Title field in Announcements List" RunAt="Server" /> 
              在本例中被替换成:
              <CustomMobile:ItemTitleField RunAt="Server" /> 
              这样,渲染模板将调用您在这个演练中前面创建的字段渲染控件。
         (2)一个新的Register 指向用来注册 “CustomMobile” 标签前缀。

  7. 保存并关闭该文件。

  8. 选择Build 菜单的Rebuild

 

测试渲染控件

用您的mobile设备或模拟器导航到您的Web应用程序中的一个包含通知列表的网站。导航到通知列表。点击新建项目链接。您将看到类似下面的界面:

图1. 在新建窗体中为标题字段指定默认文字

新建一个列表项并指定一个过去的日期作为过期日期。点击保存。您将会回到列表视图页面。点击新建的列表项下面的显示链接。您将看到如下界面。注意标题后面添加的 Search 链接。

图2. 搜索链接添加到显示窗体

点击编辑链接。您将看到如下界面。注意在当前标题前增加了“过期(OVERDUE)”的文字信息。

图3. 在编辑窗体中的标题字段文字的条件渲染

posted @ 2008-07-23 23:29  Sunmoonfire  阅读(729)  评论(1编辑  收藏  举报