error casting one widget into another

239
2
10-31-2011 12:08 PM
ValentinaBoycheva
New Contributor III
Here's the scenario:

I have created 3 new widgets, all identical copies of the LayerListWidget component, except for the widget label. At some point I want to execute a public function on those 3 widgets; its definition remains the same.

Even after following Robert Scheitlin's advice from this old thread and forcing the same application domain on all widgets ( ESRI recommendation), I am still unable to cast an instance of the new widget to the generic LayerListWidget (the original LayerListWidget is loaded as a module.)


var id:Number = ViewerContainer.getInstance().widgetManager.getWidgetId(widgetLabel); //correct
var bWidget2:IBaseWidget;
bWidget2 = ViewerContainer.getInstance().widgetManager.getWidget(id,false);//returns LayerListParcelsWidget
var vLLW2:LayerListWidget = bWidget2 as LayerListWidget;//returns null



The application domain is defined in WidgetManager.mxml as:

wgtInfo.load(ApplicationDomain.currentDomain, null, null, moduleFactory);



Compiler arguments:
-locale en_US -source-path=locale/{locale} -keep-all-type-selectors=true


FlexViewer 2.4 (July 2011), Flex 4.1, ArcMap 10.0, SP1

It looks like casting is not possible between identical component copies. Is there another way to do the casting?

Thanks,
Valentina Boycheva
Tags (2)
0 Kudos
2 Replies
RobertScheitlin__GISP
MVP Esteemed Contributor
Valentina,

   I would love to know the solution to this as well... I do have a workaround though.

Just don't declare the variable type of the widget you are after.

var id:Number = ViewerContainer.getInstance().widgetManager.getWidgetId(widgetLabel); //correct var
bWidget2:IBaseWidget; bWidget2 = ViewerContainer.getInstance().widgetManager.getWidget(id,false);//returns
LayerListParcelsWidget var vLLW2:* = bWidget2;
//Call some var or public function on the widget
//of course you will not have code completion for it though.
vLLW2.someVarriable or public function
0 Kudos
ValentinaBoycheva
New Contributor III
Thank you, Robert! The type annotation (star type) helped, indeed. The code wrapping feature had jumbled a bit your snippet. I am including it again for anyone interested.

var id:Number = ViewerContainer.getInstance().widgetManager.getWidgetId(widgetLabel); //correct 
var bWidget2:IBaseWidget;
bWidget2 = ViewerContainer.getInstance().widgetManager.getWidget(id,false);//returns LayerListParcelsWidget
var vLLW2:* = bWidget2;
//Call some var or public function on the widget
//of course you will not have code completion for it though.
vLLW2.someVarriable or public function

There is another wrinkle, though: unless LayerListParcelsWidget is preloaded as open or at least minimized, bWidget2 will always be null. Which, of course, is not at all what I want.

In the meanwhile, I realized that my solution architecture can be optimized. So, instead of 3 widgets, I have one widget with a ViewStack. This worked just fine and I avoided widget communication. One thing to remember with ViewStacks: to have all views loaded at completion time, include <creationPolicy="all">.

As for the original question - I wonder whether the casting will be possible between inherited components; that is if the new widget is not an exact copy but rather an extension of the original. No time to test now - just a note to myself.

Thanks again!

Valentina Boycheva
0 Kudos