How to change a layer property with C# code?

2492
2
Jump to solution
03-21-2011 08:36 AM
by Anonymous User
Not applicable
Original User: huangs3

Hi Experts:

    I am using the Silverlight/WPF API to implement a magnifying glass on a map.
    Attached pictures are the XAML code and C# code I am using.
    The C# code (shown in errNewTiledMapServiceLyr.png file) tries to dynamically change the Layer property of the magnifying glass, but it failed. From the error message, seems it is because of unapporiate initialization of ArcGISTiledMapServiceLayer object. However, I follows the method in http://help.arcgis.com/en/webapi/silverlight/samples/start.htm#AddLayerDynamically
and I couldn't find out how I should fix it.
    Can you give me some suggestion?
    Thank you.
0 Kudos
1 Solution

Accepted Solutions
by Anonymous User
Not applicable
Original User: huangs3

Hi Jenniferdnery:

    Thank you for your test.
    I re-checked my code and found that the issue happens at program startup because at that time the MagnifyingGlass object is not initialized yet.
    I changed the event handler to the following and it works:

*************************************
private void RadioButton_Checked(object sender, RoutedEventArgs e)
{
   if (MagnifyingGlass != null)
   {
        ArcGISTiledMapServiceLayer newMagnifyingGlassLayer = new ArcGISTiledMapServiceLayer();
        newMagnifyingGlassLayer.Url = "http://xyz";
        MagnifyingGlass.Layer = newMagnifyingGlassLayer;
    }
}
**************************************

View solution in original post

0 Kudos
2 Replies
JenniferNery
Esri Regular Contributor
What version of the API are you using?

I cannot replicate the issue with the following code using v2.1 Final (Silverlight aplication):

xmlns:esri="http://schemas.esri.com/arcgis/client/2009">
<Grid x:Name="LayoutRoot" Background="White">
 <esri:Map x:Name="MyMap"/>
 <Grid HorizontalAlignment="Right" VerticalAlignment="Top" Margin="10" >
  <Rectangle Fill="#77919191" Stroke="Gray"  RadiusX="10" RadiusY="10" Margin="0" >
   <Rectangle.Effect>
    <DropShadowEffect/>
   </Rectangle.Effect>
  </Rectangle>
  <StackPanel Orientation="Horizontal" HorizontalAlignment="Right" Background="Transparent" Margin="10">
   <RadioButton x:Name="StreetsRadioButton" 
                        Tag="http://services.arcgisonline.com/ArcGIS/rest/services/World_Street_Map/MapServer"
                         IsChecked="true" Margin="5,0,0,0" Foreground="White"
                         GroupName="Layers" Content="Streets" Click="RadioButton_Click"/>
   <RadioButton x:Name="TopoRadioButton" 
                         Tag="http://services.arcgisonline.com/ArcGIS/rest/services/World_Topo_Map/MapServer" 
                         Margin="5,0,0,0" Foreground="White" 
                         GroupName="Layers" Content="Topo" Click="RadioButton_Click"/>
   <RadioButton x:Name="ImageryRadioButton" 
                         Tag="http://services.arcgisonline.com/ArcGIS/rest/services/World_Imagery/MapServer" 
                         Margin="5,0,0,0" Foreground="White" 
                         GroupName="Layers" Content="Imagery" Click="RadioButton_Click"/>
  </StackPanel>
 </Grid>
 <esri:MagnifyingGlass x:Name="MyMagnifyingGlass" Visibility="Visible" 
                                         HorizontalAlignment="Left" VerticalAlignment="Top"
                                         Map="{Binding ElementName=MyMap}" >
  <esri:MagnifyingGlass.Layer>
   <esri:ArcGISTiledMapServiceLayer ID="StreetMapLayer" 
                    Url="http://services.arcgisonline.com/ArcGIS/rest/services/World_Street_Map/MapServer"/>
  </esri:MagnifyingGlass.Layer>
 </esri:MagnifyingGlass>
</Grid>


private void RadioButton_Click(object sender, RoutedEventArgs e)
{   
 MyMagnifyingGlass.Layer = new ArcGISTiledMapServiceLayer() { Url = ((RadioButton)sender).Tag as string };   
}
0 Kudos
by Anonymous User
Not applicable
Original User: huangs3

Hi Jenniferdnery:

    Thank you for your test.
    I re-checked my code and found that the issue happens at program startup because at that time the MagnifyingGlass object is not initialized yet.
    I changed the event handler to the following and it works:

*************************************
private void RadioButton_Checked(object sender, RoutedEventArgs e)
{
   if (MagnifyingGlass != null)
   {
        ArcGISTiledMapServiceLayer newMagnifyingGlassLayer = new ArcGISTiledMapServiceLayer();
        newMagnifyingGlassLayer.Url = "http://xyz";
        MagnifyingGlass.Layer = newMagnifyingGlassLayer;
    }
}
**************************************
0 Kudos