从 Dataservice Flex 获取并节省价值

发布于 2024-12-05 09:03:29 字数 1317 浏览 0 评论 0原文

我正在尝试学习 Flex,我使用 PHP 服务器作为数据服务设置了一个简单的 Air 应用程序...

在我的 php 类中,有一个函数 counttotal 返回一个简单的 int价值。

  <s:WindowedApplication xmlns:fx="http://ns.adobe.com/mxml/2009"
                   xmlns:s="library://ns.adobe.com/flex/spark"
                   xmlns:mx="library://ns.adobe.com/flex/mx"
                   xmlns:demologicaclass="services.demologicaclass.*"
                   width="682" height="397" showStatusBar="false" initialize="init()">
    <fx:Script>
    <![CDATA[
        import mx.controls.Alert;
        import mx.events.FlexEvent;
        protected var count:int = 0;
        protected function init():void
         {
            counttotalResult.token = logicaservice.counttotal();
                    count = counttotalResult.lastResult as int;
         }
         protected function get_count():void
         {              

                 Alert.show(count as String);
         }
  .....
  .....
<s:Label id="countitems" left="10" bottom="39" width="221" height="21" 
                 fontSize="20"
         fontWeight="bold" text="{counttotalResult.lastResult as String}"/>
    <s:Button right="10" bottom="39" label="Controlla" click="get_count();"/>

在标签中,我得到了正确的值,但我无法保存并将值显示到简单变量中/从简单变量中显示......

I'm trying to learn Flex, I setup a simple Air application with PHP server as dataserice...

In my php class there is a function counttotal that return a simple int value.

  <s:WindowedApplication xmlns:fx="http://ns.adobe.com/mxml/2009"
                   xmlns:s="library://ns.adobe.com/flex/spark"
                   xmlns:mx="library://ns.adobe.com/flex/mx"
                   xmlns:demologicaclass="services.demologicaclass.*"
                   width="682" height="397" showStatusBar="false" initialize="init()">
    <fx:Script>
    <![CDATA[
        import mx.controls.Alert;
        import mx.events.FlexEvent;
        protected var count:int = 0;
        protected function init():void
         {
            counttotalResult.token = logicaservice.counttotal();
                    count = counttotalResult.lastResult as int;
         }
         protected function get_count():void
         {              

                 Alert.show(count as String);
         }
  .....
  .....
<s:Label id="countitems" left="10" bottom="39" width="221" height="21" 
                 fontSize="20"
         fontWeight="bold" text="{counttotalResult.lastResult as String}"/>
    <s:Button right="10" bottom="39" label="Controlla" click="get_count();"/>

In the label I got the correct value, but I can't save and show the value into/from a simple variable...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

月朦胧 2024-12-12 09:03:29

这是因为使用关键字 as 将使 Flex 尝试将一种类型强制转换为另一种类型,从而存在强制转换失败的风险。在这里,您尝试将字符串转换为整数,这基本上意味着您执行以下操作:

取'blabla'并检查它是否是一个整数。如果是,则将其值放入
计数,否则将 null 放入其中。

您想要做的是将字符串转换为整数(尽管不确定这个词)。为此,请使用以下语法:

count = int(Number(counttotalResult.lastResult));

上面的意思是

获取lastResult,将其转换为Number,然后转换为int。

转换可能会失败(但它不会失败,只要 lastResult 是有效数字的字符串表示形式),并且语法可以更短,但简而言之,这就是将类型转换为另一种类型与将类型转换为其他。

That's because using the keyword as will make Flex to try to cast a type to another, with a risk that the cast fails. Here, you try to cast a String as an integer, which basically means you do the following :

take 'blabla' and check if it's an integer. If it is, put its value in
count, otherwise put null in it.

What you want to do is transtype (not sure about the word, though) a String to an integer. To do this, use the following syntax :

count = int(Number(counttotalResult.lastResult));

The above means

Take lastResult, and convert it into a Number, and then into an int.

The conversion can fail (but it wont, as long as lastResult is the string representation of a valid number), and the syntax could be shorter, but in a nutshell, that's the difference between casting a type to another, and converting a type to another.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文