父对象和子对象之间的空转换
我有一个包含两个对象的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
首先,确保 HelperWidget2 像这样实现 IBaseWidget
其次,我建议使用
is
关键字,而不是强制转换和检查 null:First off, make sure that HelperWidget2 implements IBaseWidget like so
Second, I would suggest using the
is
keyword instead of casting and checking for null:将返回的实例转换为对象,而不是 HelperWidget2。在设计时,您不会为方法提供智能感知,但更重要的是,它在运行时不会为空。
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.