[GIS] How to define layer’s URL programmatically? (ESRI Silverlight API)

arcgis-silverlight-api

I have defined a simple tiled map service layer:

<esri:ArcGISTiledMapServiceLayer x:Name="BaseMap"
ID="BaseMap" Opacity="50" Initialized="MapServiceLayer_Initialized"
InitializationFailed="Layer_InitializationFailed" />

Then I assign its Url property programmatically:

BaseMap.Url = App._componentDict["BaseMapRestUrl"];

The app bombs with this message, and the entire Silverlight app goes blank:

Layer 'BaseMap' is currently unavailable. Error" Value cannot be null. Parameter name: Url

error

Is there a particular way to go about this that I'm missing? I suspect I'm setting the Url too late in the lifecycle.

Best Answer

Following Kirk's suggestion in the comments, I just did this:

// add base map layer
ArcGISTiledMapServiceLayer baseMap = new ArcGISTiledMapServiceLayer();
baseMap.Url = App._componentDict["BaseMapRestUrl"];
baseMap.ID = "BaseMap";
baseMap.Opacity = 50;
baseMap.Initialized += MapServiceLayer_Initialized;
baseMap.InitializationFailed += MapServiceLayer_InitializationFailed;
MyMap.Layers.Add(baseMap);

It worked great.