Select to view content in your preferred language

NullReferenceException for Adding the Switch Map component

908
4
10-04-2010 11:24 AM
ShaningYu
Honored Contributor
I tried to customize a template received by adding the Switch Map component sourced from ESRI: http://help.arcgis.com/en/webapi/silverlight/samples/start.htm#SwitchMap.  The procedure I used is described below:

1) Create Switch.xaml as well as .cs file in \UserControls\.
2) Copy and paste the related source codes into the 2 files mentioned in 1).  E.g., in the xaml file:

    <Grid Name="LayoutRoot" Background="Transparent">
        <Grid.Resources>
            <esri:SimpleFillSymbol x:Name="DefaultFillSymbol" Fill="#33FF0000" BorderBrush="Red" BorderThickness="2" />
        </Grid.Resources>
        <unb:FloatingPanel x:Name="MyFloatingPanel" Floating="True" Margin="0,0,0,0" />
        <Grid Style="{StaticResource FloatingPanelItemsGrid}" Margin="10,10,10,10">
            <Rectangle Style="{StaticResource ForePanel}" HorizontalAlignment="Left" Width="220" />
            <StackPanel Orientation="Horizontal" HorizontalAlignment="Center" Margin="18,0,6,0" Width="196">
                <RadioButton x:Name="StreetsRadioButton" .../>
                <RadioButton x:Name="TopoRadioButton" .../>
                <RadioButton x:Name="ImageryRadioButton" ... />
            </StackPanel>
        </Grid>
    </Grid>

3) Add the following code into MainPage.xaml
            <esri:Map x:Name="MyMap"
                      Canvas.Left="0" Canvas.Top="0" Height="900" Width="1200"
                      IsLogoVisible="False">
                <esri:ArcGISTiledMapServiceLayer ID="AGOLayer" Visible="True"
                    Url="http://server.arcgisonline.com/ArcGIS/rest/services/ESRI_StreetMap_World_2D/MapServer"/>
                <esri:GraphicsLayer ID="ResultsGraphicsLayer" />
                <esri:GraphicsLayer ID="MeasureGraphicsLayer" />
                <esri:GraphicsLayer ID="IDGraphicsLayer" />
            </esri:Map>
4) Compile and run debugging.
I received NullReferenceException at
        private void RadioButton_Click(object sender, RoutedEventArgs e)
        {
            ArcGISTiledMapServiceLayer arcgisLayer = _Parent.MyMap.Layers["AGOLayer"] as ArcGISTiledMapServiceLayer;

I don't know how to fix it.  If you can share your experience, I will greatly appreciate.
0 Kudos
4 Replies
JenniferNery
Esri Regular Contributor
You say that the NullRef exception occurs in this line, right?

ArcGISTiledMapServiceLayer arcgisLayer = _Parent.MyMap.Layers["AGOLayer"] as ArcGISTiledMapServiceLayer;

Can you check if _Parent is not null? I don't see _Parent defined in your XAML. It's best to check this while debugging. Check also that none of these values are null:
_Parent.MyMap
_Parent.MyMap.Layers
_Parent.MyMap.Layers contains a layer with ID "AGOLayer" and
_Parent.MyMap.Layers["AGOLayer"] is ArcGISTiledMapServiceLayer
0 Kudos
ShaningYu
Honored Contributor
Jenniffer:
Thanks for your response.  As you pointed out, the _Parebt is null.  I added
_Parent = new MainPage();
then
arcgisLayer.Url = ((RadioButton)sender).Tag as string;
also shows right URL.  However, neither imagery nor topo map can be loaded.  Please advise me how to fix this problem.  Thanks again.
Shaning
0 Kudos
DominiqueBroux
Esri Frequent Contributor
If I understand well, your _Parent should be the current main page and not one created with the new keyword.


If you are sure that the map is inside the root control you can use
 _Parent = (Mainpage)System.Windows.Application.Current.RootVisual; 

Or, more elegant, you can add a 'Map' property to your switch control in order to get the map buddied to your control.

In switch.cs add :

 
#region Map
/// <summary>
/// Gets or sets the map that the switch control is buddied to.
/// </summary>
/// <value>The map.</value>
public ESRI.ArcGIS.Client.Map Map
{
get { return GetValue(MapProperty) as Map; }
set { SetValue(MapProperty, value); }
}
/// /// <summary>
/// Identifies the <see cref="Map"/> dependency property.
/// </summary>
public static readonly DependencyProperty MapProperty =
DependencyProperty.Register("Map", typeof(Map), typeof(Switch),null);
#endregion


In switch.cs, replace all '_Parent.MyMap' by 'Map'

In MainPage.xaml, bind the Map property of your Switch control :
 
<Switch Map={Binding ElementName=MyMap} ...... />
0 Kudos
ShaningYu
Honored Contributor
Dominique:
Great!  Using your code, it works well.  Thanks a LOT!!!
Shaning
0 Kudos