用VS2005创建一个Atlas Web应用程序(2)

来源:互联网 发布:java在线客服源码 编辑:程序博客网 时间:2024/04/30 07:12
用VS2005创建一个Atlas Web应用程序(2)
作者: dengsu888666 发表日期: 2006-09-16 15:32 文章属性: 原创 复制链接


    本文主要介绍微软新的Web开发技术中的客户端的Atlas的脚本声明语法,在这里你将要建立和前一篇文章中介绍的例子同样功能的Web程序,在前一篇文章中介绍了用客户端脚本调用Web服务,然而在这里,不是用客户端脚本,而是用客户端脚本声明来完成调用Web服务,你可以学到Atlas'声明块的基本语法、如何与Web页中的HTML元素建立关联和如何调用Web服务的基本知识。
    首先你要建立一个'Atlas'的Web应用程序,添加一个Web服务HelloWorldService.asmx和一个web页面Declarative.aspx,,具体做法和前一篇文章中介绍的一样。同样在Web服务代码里添加一个和前一篇文章中描述的一样的HelloWorld方法。
    现在我们用一个脚本声明块来替代以前的脚本功能,脚本声明标记代码如下,请复制并粘贴到你的代码里:

  <script type="text/xml-script">
<page xmlns:script="http://schemas.microsoft.com/xml-script/2005">
  <components>

  <textBox id="SearchKey" />

  <serviceMethod id="helloService"
      url="HelloWorldService.asmx"
      methodName="HelloWorld">
    <bindings>
      <binding dataContext="SearchKey"
        dataPath="text"
        property="parameters"
        propertyKey="query" />
    </bindings>
    <completed>
      <invokeMethod target="resultsBinding"
      method="evaluateIn" />
    </completed>
  </serviceMethod>

  <button id="SearchButton">
    <click>
      <invokeMethod target="helloService"
          method="invoke" />
    </click>
  </button>

  <label id="results">
    <bindings>
      <binding id="resultsBinding"
        dataContext="helloService"
        dataPath="result"
        property="text"
        automatic="false" />
    </bindings>
  </label>

  </components>
</page>
</script>
    'Atlas'脚本声明标记很像XHTML规范的风格,而'Atlas'脚本声明标记与它的不同之处是'Atlas'脚本声明标记引用了'Atlas'客户端脚本库中的对象和元素。'Atlas'描述块使用了标准的XHTML中的 <script>元素,所不同之处是该元素的type属性值为text/xml-script。'Atlas'描述块的根元素是<page>元素,它包含了描述块中所有的其他元素和它自身的命名空间,它的命名空间就是xmlns:script的属性值,第二个元素是<components>,它来自于'Atlas'声明块中的UI控件和组件,UI元素在Atlas中被声明为<components>元素的孩子元素,这样,页面中包含了三个Atlas的UI元素:一个<textBox>元素、一个<button>元素和一个<label>元素。这些Atlas元素和页面中的HTML元素相互关联。例如,你可以通过<textBox>元素的id属性把<textBox>元素和页面中HTML的textbox控件关联起来,<button>元素是相应HTML控件调用Web服务的控件,它的<click>元素是指相应的HTML的button控件的click事件行为。同样,<invokeMethod>孩子元素指明了在click时调用的一个远程方法,<invokeMethod>元素的target属性说明了<serviceMethod>元素的远程调用的具体方法名,<serviceMethod>元素提供了要调用的Web服务的URL和要调用的方法名(HelloWorld)。<bindings>孩子元素说明了客户端控件的值要作为参数传送给远程方法和远程方法中的参数名。在<completed>元素里说明了当<serviceMethod>元素里所规定的远程方法在执行完成后要做的事,同理,执行了在method属性中指明了的一个本地'Atlas'方法。它对从服务端的返回值进行了加工并把值赋给本地对象,这个本地对象就是<label>元素,它通过ID属性的值results和<span>关联起来。
    总的来说,Xml-Script是一个声明性脚本模型,它允许开发者使用一种类似于XML的编程模型书写客户端代码。Atlas声明性脚本一般使用下列语法定义在<script></script>标签内部,它的格式如下:
<script type="text/xml-script">
<page xmlns:script="http://schemas.microsoft.com/xml-script/2005">
<references>
</references>
<components>
</components>
</page>
</script>
    下面是Declarative.aspx的完整代码:
<% @ Page Language="C#" Title="Atlas Declarative Walkthrough" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

<head id="Head1" runat="server">
  <atlas:scriptmanager runat="server" id="scriptManager">
    <services>
    <atlas:servicereference path="~/HelloWorldService.asmx" />
    </services>
  </atlas:scriptmanager>
  <style type="text/css">
  body { font: 11pt Trebuchet MS;
    font-color: #000000;
    padding-top: 72px;
      text-align: center }

  .text { font: 8pt Trebuchet MS }
  </style>

</head>
<body>
  <form id="Form1" runat="server">
  <div>
    Search for
    <input id="SearchKey" type="text" />
    <input id="SearchButton" type="button"
        value="Search" />
  </div>
  </form>
  <hr style="width: 300px" />
  <div>
  <span id="results"></span>
  </div>
  <script type="text/xml-script">
  <page xmlns:script="http://schemas.microsoft.com/xml-script/2005">
  <components>

    <textBox id="SearchKey" />

    <serviceMethod id="helloService"
        url="HelloWorldService.asmx"
        methodName="HelloWorld">
      <bindings>
      <binding dataContext="SearchKey"
          dataPath="text"
          property="parameters"
          propertyKey="query" />
      </bindings>
      <completed>
      <invokeMethod target="resultsBinding"
        method="evaluateIn" />
      </completed>
    </serviceMethod>

    <button id="SearchButton">
      <click>
      <invokeMethod target="helloService"
            method="invoke" />
      </click>
    </button>

    <label id="results">
      <bindings>
      <binding id="resultsBinding"
          dataContext="helloService"
          dataPath="result"
          property="text"
          automatic="false" />
      </bindings>
    </label>

  </components>
  </page>
  </script>
</body>
</html>

    现在你可以运行Declarative.aspx看看效果了,你会发现本例程实现了第一篇文章的例子同样的功能,只是使用了Atlas的另外一种技术方法。下一篇文章将介绍创建具有自动下拉框功能的Atlas Web应用程序。
 
原创粉丝点击