父对象和子对象之间的空转换

发布于 2024-10-19 19:29:20 字数 1118 浏览 0 评论 0原文

我有一个包含两个对象的 Flex 应用程序:一个父窗口小部件(称为 IBaseWidget)和一个子窗口小部件(称为 HelperWidget2)。当用户单击帮助链接时,帮助器小部件将加载到基本小部件列表中,然后向用户显示。

但是,当我尝试通过将集合中的基本小部件转换为子小部件类型来访问此子小部件时,子小部件返回 null,并且我无法使用该小部件。

以下代码片段正确返回新添加的小部件的小部件 ID,并调度一个事件来加载小部件:

var id:Number = WidgetManager.getInstance().getWidgetId("Helper");
ViewerContainer.dispatchEvent(new AppEvent(AppEvent.WIDGET_RUN, id, openQuickQueryCanvas));

加载小部件后,名为 openQuickQueryCanvas() 的回调函数尝试对辅助小部件执行另一个操作:

private function openQuickQueryCanvas():void{
            var id:Number = WidgetManager.getInstance().getWidgetId("Helper");
            var bWidget:IBaseWidget = WidgetManager.getInstance().getWidget(id) as IBaseWidget;
            var helperWidget:HelperWidget2 = bWidget as HelperWidget2;
            if(helperWidget != null){
                helperWidget.quickQueryCanvas.dispatchEvent(new MouseEvent(MouseEvent.CLICK));//fire an event to open the quick query canvas
            }

        }

问题是 helperWidget上面总是返回 null,这意味着转换不成功。这对我来说没有意义,因为 bWidget 是 HelperWidget2 类型。

有什么想法吗?我很困惑...

I have a flex application with two objects: a parent widget (called an IBaseWidget) and a child widget (called a HelperWidget2). When the user clicks on a help link, a helper widget is loaded into the list of base widgets and then displayed for the user.

However, when I try to access this child widget by casting the base widget in the collection to the child widget type, the child widget returns null and I am unable to work with the widget.

The following snippet correctly returns the widget ID of the newly added widget and dispatches an event to load the widget:

var id:Number = WidgetManager.getInstance().getWidgetId("Helper");
ViewerContainer.dispatchEvent(new AppEvent(AppEvent.WIDGET_RUN, id, openQuickQueryCanvas));

Once the widget is loaded, a callback function called openQuickQueryCanvas() attempts to do another action with the helper widget:

private function openQuickQueryCanvas():void{
            var id:Number = WidgetManager.getInstance().getWidgetId("Helper");
            var bWidget:IBaseWidget = WidgetManager.getInstance().getWidget(id) as IBaseWidget;
            var helperWidget:HelperWidget2 = bWidget as HelperWidget2;
            if(helperWidget != null){
                helperWidget.quickQueryCanvas.dispatchEvent(new MouseEvent(MouseEvent.CLICK));//fire an event to open the quick query canvas
            }

        }

The problem is that helperWidget above always returns null, meaning the cast isn't successful. This doesn't make sense to me, because bWidget is of type HelperWidget2.

Any thoughts? I'm stumped...

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

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

发布评论

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

评论(2

妄断弥空 2024-10-26 19:29:20

首先,确保 HelperWidget2 像这样实现 IBaseWidget

public class HelperWidget2 implements IBaseWidget

其次,我建议使用 is 关键字,而不是强制转换和检查 null:

private function openQuickQueryCanvas():void {
                var id:Number = WidgetManager.getInstance().getWidgetId("Helper");
                var bWidget:IBaseWidget = WidgetManager.getInstance().getWidget(id) as IBaseWidget;

                if(bWidget is HelperWidget2)
                {
                  HelperWidget2(bWidget).doWhatever();
                }

            }

First off, make sure that HelperWidget2 implements IBaseWidget like so

public class HelperWidget2 implements IBaseWidget

Second, I would suggest using the is keyword instead of casting and checking for null:

private function openQuickQueryCanvas():void {
                var id:Number = WidgetManager.getInstance().getWidgetId("Helper");
                var bWidget:IBaseWidget = WidgetManager.getInstance().getWidget(id) as IBaseWidget;

                if(bWidget is HelperWidget2)
                {
                  HelperWidget2(bWidget).doWhatever();
                }

            }
Saygoodbye 2024-10-26 19:29:20

将返回的实例转换为对象,而不是 HelperWidget2。在设计时,您不会为方法提供智能感知,但更重要的是,它在运行时不会为空。

var bWidget:Object = WidgetManager.getInstance().getWidget(id);
bWidget.doWhatever();

Cast the returning instance as an object, instead of HelperWidget2. You won't have intellisense for the methods at design time, but more importantly, it won't be null at run time.

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