Flex 中的同步对话框?

发布于 2024-07-07 04:53:18 字数 650 浏览 11 评论 0原文

如何在 Flex 中打开同步对话框? 我需要从外部接口 (JavaScript) 调用一个函数,该函数将在 Flex 应用程序中打开一个简单的对话框,并根据用户单击的按钮(确定/取消)返回一个值。

因此,它应该通过同步调用对话框,即调用等待,直到用户像这样关闭对话框。

//This function is called by JavaScript
function onApplicationUnload():Boolean
{
  var result:Boolean;
  result = showDialogAndWaitForResult();
  return result
}

有人知道我该怎么做吗? 我可以编写一个循环,等待对话框设置标志,然后读取结果以返回它,但必须有一些更优雅和可重用的方法来等待其他异步调用的完成。

编辑: 不幸的是,回调不起作用,因为调用 onApplicationUnload() 的 JavaScript 函数本身必须返回一个值(类似于 Flex 中的 onApplicationUnload() 函数)。 这个 JavaScript 函数有一个固定的签名,因为它是由框架调用的,我无法更改它。 或者换句话说:从 JavaScript 到 Flex 的调用也必须是同步的。

How can I open a synchronous dialog in Flex? I need to call a function from an External Interface (JavaScript) that will open a simple dialog in the Flex application and returns an value according to the button the user has clicked (OK/Cancel).

So it should by a synchronous call to a dialog, i.e. the call waits until the user has closed the dialog like this.

//This function is called by JavaScript
function onApplicationUnload():Boolean
{
  var result:Boolean;
  result = showDialogAndWaitForResult();
  return result
}

Does anybody know how I can do this? I could write a loop that waits until the dialog has set a flag and then reads the result to return it, but there must be something that is way more elegant and reusable for waiting of the completion of other asynchronous calls.

EDIT:
Unfortunately a callback does not work as the JavaScript function that calls onApplicationUnload() itself has to return a value (similar to the onApplicationUnload() function in Flex). This JavaScript function has a fixed signature as it is called by a framework and I cannot change it. Or in other words: The call from JavaScript to Flex must also be synchronous.

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

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

发布评论

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

评论(7

乞讨 2024-07-14 04:53:18

Flex 不以同步方式工作,因为它是单线程应用程序,因此需要您的代码将执行交回“核心”,以便处理用户输入等。

做到这一点的方法是使对话的行为异步:

function onApplicationUnload():void
{
    showDialog(resultMethod);
}

function resultMethod(result:Boolean):void
{
    ExternalInterface.call("javaScriptCallback", [result]);
}

Flex doesn't work in a synchronous fashion, as it is a single thread application and so needs your code to hand execution back to the "core" in order to handle user input etc.

The way to do it is to make your dialogue's behaviour asynchronous:

function onApplicationUnload():void
{
    showDialog(resultMethod);
}

function resultMethod(result:Boolean):void
{
    ExternalInterface.call("javaScriptCallback", [result]);
}
永不分离 2024-07-14 04:53:18

在 Flex 中您无法做到这一点。 正如 David 提到的,Flex 是单线程的,因此在处理对话框时您无法使用功能块。

最好的选择可能是使用 Javascript 弹出窗口。 您对窗口的控制会少得多,但它应该按照您想要的方式运行(阻止该函数直到它被关闭)。

You can't do that in Flex. As David mentioned, Flex is single-threaded, so you can't have your function block while the dialog is being processed.

Your best bet might be to use a Javascript popup. You'll have a lot less control over the window, but it should behave the way you want (blocking the function until it's been closed).

离鸿 2024-07-14 04:53:18

让您的 Flex 代码使用事件来等待对话框。 在主线程中,注册一个等待对话框关闭的事件处理程序。 在对话框中的“确定”上,调度对话框完成事件。

对于 Cairngorm,这类似于:

在主线程中:

CairngormEventDispatcher.getInstance().addEventListener(ClosingDialogCompleteEvent.DIALOG_COMPLETE, onClosingDialogComplete);

(如果您想避免在完成之前返回,请在计时器上循环并全局变量。)

在对话框关闭处理程序中:

CairngormEventDispatcher.dispatchEvent(new ClosingDialogCompleteEvent());

事件处理程序:


public function onClosingDialogComplete (e: ClosingDialogCompleteEvent):void
{
   param1 = e.param1;
   param2 = e.param2;
   // etc.
   // Continue processing or set the global variable that signals the main thread to continue.
}

为此,必须定义类 ClosingDialogCompleteEvent。 该类的部分代码为:


package com. ... .event  // You define where the event lives.
{
import com.adobe.cairngorm.control.CairngormEvent;

public class ClosingDialogCompleteEvent extends CairngormEvent
{
    // Event type.
    public static const DIALOG_COMPLETE:String = "dialogComplete";

    public var param1:String;
    public var param2:String;

    public function ClosingDialogCompleteEvent(param1:String, param2:String)
    {
        super(DIALOG_COMPLETE);
        this.param1 = param1;
        this.param2 = param2;
    }
}
}

等待事件是在 Flex 中同步的最佳方式。 它也适用于启动对话框。 在仅使用 Flex 的应用程序中,它的效果特别好。

Have your Flex code use an event to wait for the dialog. In the main thread, register an event handler that waits for the dialog to close. On OK in the dialog, dispatch the dialog complete event.

With Cairngorm, this is something like:

In the main thread:

CairngormEventDispatcher.getInstance().addEventListener(ClosingDialogCompleteEvent.DIALOG_COMPLETE, onClosingDialogComplete);

(if you want to avoid returning until complete, loop on a timer and global variable.)

In the dialog closing handler:

CairngormEventDispatcher.dispatchEvent(new ClosingDialogCompleteEvent(<parameters>));

The event handler:


public function onClosingDialogComplete (e: ClosingDialogCompleteEvent):void
{
   param1 = e.param1;
   param2 = e.param2;
   // etc.
   // Continue processing or set the global variable that signals the main thread to continue.
}

For this to work, the class ClosingDialogCompleteEvent has to be defined. Partial code for the class is:


package com. ... .event  // You define where the event lives.
{
import com.adobe.cairngorm.control.CairngormEvent;

public class ClosingDialogCompleteEvent extends CairngormEvent
{
    // Event type.
    public static const DIALOG_COMPLETE:String = "dialogComplete";

    public var param1:String;
    public var param2:String;

    public function ClosingDialogCompleteEvent(param1:String, param2:String)
    {
        super(DIALOG_COMPLETE);
        this.param1 = param1;
        this.param2 = param2;
    }
}
}

Waiting on an event is the best way to synchronize in Flex. It works well for startup dialogs too. In a flex-only application it works especially well.

爱冒险 2024-07-14 04:53:18

中创建同步警报的解决方法

我已经解释了在 Flex http ://reallypseudorandom.blogspot.com/2010/05/flash-asynchronous-alert-and-pause.html

I have explained a workaround to create synchronous alert in flex

http://reallypseudorandom.blogspot.com/2010/05/flash-asynchronous-alert-and-pause.html

夜吻♂芭芘 2024-07-14 04:53:18

好吧...毕竟我找到了一个可能的解决方案。 但我想几乎每个人都会认真地这样做:-(

解决方案的重点是使用 while 循环来检查结果,然后返回 JavaScript 调用的函数。但是,我们需要一种在 while 循环中休眠的方法,当我们等待结果时,对 JavaScript 的调用是同步的。现在的技巧是在 JavaScript 中进行睡眠,这在这里也不能直接使用,但可以使用同步 XML Http 请求来完成,如本 < 中所述。 a href="http://narayanraman.blogspot.com/2005/12/javascript-sleep-or-wait.html" rel="nofollow noreferrer">博客

正如我所说 - 我不会推荐 。对于我的问题,这只是最后的手段,我求助于丑陋的 JavaScript 弹出窗口。

OK... after all I found a possible solution. But I guess hardly everybody is going to do that seriously :-(

The solution focuses around using a while loop to check for a result and then return the function that is being called by JavaScript. However we need a way to sleep in the while loop, while we are waiting for the result. However calls to JavaScript are synchronous. Now the trick is to make a sleep in JavaScript, which is also not directly available here, but can be done using a synchronous XML Http Request like described on this blog.

As I said - I won't recommend this only as last resort. For my problem I have resorted to ugly JavaScript popups.

烟酉 2024-07-14 04:53:18

让您的对话框调用 Flex 中的另一个函数来处理用户选择的结果:

private function deleteFileCheck():void
{
  Alert.show("Are you sure you want to delete this file?",
             "Confirm Delete",
             Alert.YES| Alert.NO,
             this, deleteFileHandler, null, Alert.NO);
}

private function deleteFileHandler(event:CloseEvent):void
{
    if (event.detail == Alert.YES)
    {
        ...do your processing here
    }
}

Have your dialog call another function in flex to process the result of the user selection:

private function deleteFileCheck():void
{
  Alert.show("Are you sure you want to delete this file?",
             "Confirm Delete",
             Alert.YES| Alert.NO,
             this, deleteFileHandler, null, Alert.NO);
}

private function deleteFileHandler(event:CloseEvent):void
{
    if (event.detail == Alert.YES)
    {
        ...do your processing here
    }
}
风和你 2024-07-14 04:53:18

您可以通过弹出一个对话框然后禁用后台的所有内容来伪造 Flex 中的同步对话框。 如果您在应用程序中执行Alert.show("Hello World");,您就可以看到它的实际效果。 背景将变灰,用户将无法单击背景中的任何 UI。 应用程序将“等待”,直到用户单击“确定”按钮。

You can fake a synchronous dialog in flex by popping up a dialog then disabling everything in the background. You can see this in action if you do Alert.show("Hello World"); in an application. The background will grey out and the user won't be able to click on any UI in the background. The app will "wait" until the user clicks the OK button.

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