ArcGIS API for Silverlight - Library Reference
Initialize Method
See Also  Example Send comments on this topic
ESRI.ArcGIS.Client Namespace > FeatureLayer Class : Initialize Method

Initializes the FeatureLayer by which invokes the FeatureLayer.Initialized Event.

Syntax

Visual Basic (Declaration) 
Public Overrides Sub Initialize() 
C# 
public override void Initialize()

Remarks

If you want to obtain FeatureLayerInfo information about a FeatureLayer before adding it to the Map, consider calling this Method rather than adding using the Map.Layers.Add Method (which will automatically add the FeatureLayer to the Map).

Obtaining information (i.e. get/Read) for the various Properties of a FeatureLayer should occur in the FeatureLayer.Initialized Event or any time after the Initialized Event occurs. This ensures that information retrieved about the FeatureLayer has been obtained after a complete round trip from ArcGIS Server. Do not be tempted to try and access FeatureLayer Property information from generic application Events like: MainPage.Loaded or the Constructor, etc. as the FeatureLayer has not been Initialized and erroneous information will be returned. Likewise, FeatureLayer Methods should not be invoked until after the FeatureLayer Initialized Event has fired or from within the Initialized Event to avoid erroneous results.

Override this method if your resource requires asynchronous requests to initialize, and call the base method when initialization is completed.

Upon completion of initialization, check the Layer.InitializationFailure for any possible errors.

Example

How to use:

Click the Button to create a FeatureLayer and add a Graphic to the Map based upon the FeatureLayer's Extent. Then click the second button to add the already created FeatureLayer to the Map. This demonstrates that you can call the FeatureLayer.Initialize Method to obtain information about the FeatureLayer before deciding to add it the Map.

The XAML code in this example is used in conjunction with the code-behind (C# or VB.NET) to demonstrate the functionality.

The following screen shot corresponds to the code example in this page.

Calling the FeatureLayer.Initialize Method to obtain information before displaying it in a Map.

XAMLCopy Code
<Grid x:Name="LayoutRoot" >
  
  <!-- Add a Map Control to the application. -->
  <esri:Map x:Name="Map1" WrapAround="True" HorizontalAlignment="Left" Margin="5,235,0,115" Width="665" Background="LightGray">
  
    <!-- Add an empty GraphicsLayer. -->
    <esri:GraphicsLayer ID="MyGraphicsLayer" />
  </esri:Map>
  
  <!-- Add a Button that will allow the user to create a FeatureLayer via code-behind. It will display the FeatureLayer's
  Extent as a Graphic in the Map. NOTE: the FeatureLayer itself will not be added to the Map at this time. -->
  <Button  Height="23" HorizontalAlignment="Left" Width="665" Margin="5,107,0,0" Name="Button_CreateFeatureLayer" VerticalAlignment="Top" 
           Content="Create a FeatureLayer. Display its Extent as a Graphic." Click="Button_CreateFeatureLayer_Click" />
  
  <!-- Add a Button to now add the FeatureLayer to the Map. -->
  <Button Height="23" HorizontalAlignment="Left" Margin="5,136,0,0" Name="Button_AddFeatureLayer" VerticalAlignment="Top" Width="665" 
          Content="Add the FeatureLayer (just created) to the Map." Click="Button_AddFeatureLayer_Click" IsEnabled="False"/>
  
  <!-- TextBox to display information about about the FeatureLayer's Extent. -->
  <TextBox Height="64" HorizontalAlignment="Left" Margin="5,165,0,0" Name="TextBox1" VerticalAlignment="Top" Width="665" />
  
  <!-- Provide the instructions on how to use the sample code. -->
  <TextBlock Height="72" HorizontalAlignment="Left" Name="TextBlock1" VerticalAlignment="Top" Width="788" 
             TextWrapping="Wrap" Text="Click the Button to create a FeatureLayer and add a Graphic to the Map based upon
             the FeatureLayer's Extent. Then click the second button to add the already created FeatureLayer to the Map. 
             This demonstrates that you can call the FeatureLayer.Initialize Method to obtain information about the 
             FeatureLayer before deciding to add it the Map." />
</Grid>
C#Copy Code
// A global (aka. Member) variable for the FeatureLayer to be created. 
public ESRI.ArcGIS.Client.FeatureLayer _MyFeatureLayer = new ESRI.ArcGIS.Client.FeatureLayer();
            
private void Button_CreateFeatureLayer_Click(object sender, System.Windows.RoutedEventArgs e)
{
  // Create an FeatureLayer. 
  
  // Set the Url of the FeatureLayer to a public service. 
  _MyFeatureLayer.Url = "http://server.arcgisonline.com/ArcGIS/rest/services/Demographics/USA_Unemployment_Rate/MapServer/4";
  
  // Setting the IgnoreServiceScaleRange = True is helpful if the defined map service has restrictions on
  // different scale levels for which the features can be viewed (ex: in the ArcGIS Services Directory REST 
  // documentation these would be the 'Min. Scale' and 'Max. Scale' metadata items). The value of 
  // True always draws the features regardless of the current map scale.
  _MyFeatureLayer.IgnoreServiceScaleRange = true;
  
  // Set the ID of the FeatureLayer.
  _MyFeatureLayer.ID = "USA_Unemployment_Rate";
  
  // Wire-up the Initialized Event of the FeatureLayer. 
  _MyFeatureLayer.Initialized += FeatureLayer_Initialized;
  
  // Cause the FeatureLayer.Initialized Event to fire.
  // NOTE: The FeatureLayer is not added to the map at this time; although it is created.
  _MyFeatureLayer.Initialize();
  
  // Now let the user click the button to add the FeatureLayer to the Map.
  Button_AddFeatureLayer.IsEnabled = true;
}
            
private void FeatureLayer_Initialized(object sender, EventArgs e)
{
  
  // This function will execute as a result of the FeatureLayer that was defined in code-behind
  // being Initialized.
  
  // Get the FeatureLayer.
  ESRI.ArcGIS.Client.FeatureLayer myFeatureLayer = (ESRI.ArcGIS.Client.FeatureLayer)sender;
  
  // Get the ID of the FeatureLayer.
  string myID = myFeatureLayer.ID;
  
  // Get the FeatureLayerInfo object as a result of the FeatureLayer.LayerInfo Property. It that contains lots 
  // of valuable information about the FeatureLayer.
  ESRI.ArcGIS.Client.FeatureService.FeatureLayerInfo myFeatureLayerInfo = myFeatureLayer.LayerInfo;
  
  // Get the Extent of the FeatureLayer from the FeatureLayerInfo object.
  ESRI.ArcGIS.Client.Geometry.Envelope myExtent = myFeatureLayerInfo.Extent;
  
  // Create a new instance of a SimpleFillSymbol and set its BorderBrush and BorderThickness Properties.
  ESRI.ArcGIS.Client.Symbols.SimpleFillSymbol mySimpleFillSymbol = new ESRI.ArcGIS.Client.Symbols.SimpleFillSymbol();
  mySimpleFillSymbol.BorderBrush = new System.Windows.Media.SolidColorBrush(Colors.Green);
  mySimpleFillSymbol.BorderThickness = 5;
  
  // Create a new Polygon Graphic based upon the Extent of the FeatureLayer and set its symbology.
  ESRI.ArcGIS.Client.Graphic myGraphic = new ESRI.ArcGIS.Client.Graphic();
  myGraphic.Geometry = myExtent;
  myGraphic.Symbol = (ESRI.ArcGIS.Client.Symbols.Symbol)mySimpleFillSymbol;
  
  // Get the GraphicsLayer defined in XAML.
  ESRI.ArcGIS.Client.GraphicsLayer myGraphicsLayer = (ESRI.ArcGIS.Client.GraphicsLayer)Map1.Layers["MyGraphicsLayer"];
  
  // Add the FeatureLayer's Extent Graphic to the Map.
  myGraphicsLayer.Graphics.Add(myGraphic);
  
  // Zoom the Map extent out just a little to see the newly added Graphic.
  Map1.Extent = myExtent.Expand(1.1);
  
  // Create a StringBuilder object to hold information about the FeatureLayer and add the information to it.
  System.Text.StringBuilder myStringBuilder = new System.Text.StringBuilder();
  myStringBuilder.Append("The FeatureLayer.LayerInfo Results for: " + myID + Environment.NewLine);
  myStringBuilder.Append("============================================================" + Environment.NewLine);
  myStringBuilder.Append("Extent: " + myFeatureLayerInfo.Extent.ToString() + Environment.NewLine);
  
  // Display the results of the StringBuilder text to the user.
  TextBox1.Text = myStringBuilder.ToString();
}
            
private void Button_AddFeatureLayer_Click(object sender, System.Windows.RoutedEventArgs e)
{
  // Display the existing FeatureLayer in the Map. 
  // NOTE: The FeatureLayer.Initialized Event will NOT fire a second time. It only executes once.
  Map1.Layers.Add(_MyFeatureLayer);
}
VB.NETCopy Code
' A global (aka. Member) variable for the FeatureLayer to be created. 
Public _MyFeatureLayer As New ESRI.ArcGIS.Client.FeatureLayer
            
Private Sub Button_CreateFeatureLayer_Click(sender As System.Object, e As System.Windows.RoutedEventArgs)
  
  ' Create an FeatureLayer. 
  
  ' Set the Url of the FeatureLayer to a public service. 
  _MyFeatureLayer.Url = "http://server.arcgisonline.com/ArcGIS/rest/services/Demographics/USA_Unemployment_Rate/MapServer/4"
  
  ' Setting the IgnoreServiceScaleRange = True is helpful if the defined map service has restrictions on
  ' different scale levels for which the features can be viewed (ex: in the ArcGIS Services Directory REST 
  ' documentation these would be the 'Min. Scale' and 'Max. Scale' metadata items). The value of 
  ' True always draws the features regardless of the current map scale.
  _MyFeatureLayer.IgnoreServiceScaleRange = True
  
  ' Set the ID of the FeatureLayer.
  _MyFeatureLayer.ID = "USA_Unemployment_Rate"
  
  ' Wire-up the Initialized Event of the FeatureLayer. 
  AddHandler _MyFeatureLayer.Initialized, AddressOf FeatureLayer_Initialized
  
  ' Cause the FeatureLayer.Initialized Event to fire.
  ' NOTE: The FeatureLayer is not added to the map at this time; although it is created.
  _MyFeatureLayer.Initialize()
  
  ' Now let the user click the button to add the FeatureLayer to the Map.
  Button_AddFeatureLayer.IsEnabled = True
  
End Sub
            
Private Sub FeatureLayer_Initialized(sender As Object, e As EventArgs)
  
  ' This function will execute as a result of the FeatureLayer that was defined in code-behind
  ' being Initialized.
  
  ' Get the FeatureLayer.
  Dim myFeatureLayer As ESRI.ArcGIS.Client.FeatureLayer = CType(sender, ESRI.ArcGIS.Client.FeatureLayer)
  
  ' Get the ID of the FeatureLayer.
  Dim myID As String = myFeatureLayer.ID
  
  ' Get the FeatureLayerInfo object as a result of the FeatureLayer.LayerInfo Property. It that contains lots 
  ' of valuable information about the FeatureLayer.
  Dim myFeatureLayerInfo As ESRI.ArcGIS.Client.FeatureService.FeatureLayerInfo = myFeatureLayer.LayerInfo
  
  ' Get the Extent of the FeatureLayer from the FeatureLayerInfo object.
  Dim myExtent As ESRI.ArcGIS.Client.Geometry.Envelope = myFeatureLayerInfo.Extent
  
  ' Create a new instance of a SimpleFillSymbol and set its BorderBrush and BorderThickness Properties.
  Dim mySimpleFillSymbol As New ESRI.ArcGIS.Client.Symbols.SimpleFillSymbol
  mySimpleFillSymbol.BorderBrush = New System.Windows.Media.SolidColorBrush(Colors.Green)
  mySimpleFillSymbol.BorderThickness = 5
  
  ' Create a new Polygon Graphic based upon the Extent of the FeatureLayer and set its symbology.
  Dim myGraphic As ESRI.ArcGIS.Client.Graphic = New ESRI.ArcGIS.Client.Graphic
  myGraphic.Geometry = myExtent
  myGraphic.Symbol = CType(mySimpleFillSymbol, ESRI.ArcGIS.Client.Symbols.Symbol)
  
  ' Get the GraphicsLayer defined in XAML.
  Dim myGraphicsLayer As ESRI.ArcGIS.Client.GraphicsLayer = Map1.Layers("MyGraphicsLayer")
  
  ' Add the FeatureLayer's Extent Graphic to the Map.
  myGraphicsLayer.Graphics.Add(myGraphic)
  
  ' Zoom the Map extent out just a little to see the newly added Graphic.
  Map1.Extent = myExtent.Expand(1.1)
  
  ' Create a StringBuilder object to hold information about the FeatureLayer and add the information to it.
  Dim myStringBuilder As New System.Text.StringBuilder
  myStringBuilder.Append("The FeatureLayer.LayerInfo Results for: " + myID + vbCrLf)
  myStringBuilder.Append("============================================================" + vbCrLf)
  myStringBuilder.Append("Extent: " + myFeatureLayerInfo.Extent.ToString + vbCrLf)
  
  ' Display the results of the StringBuilder text to the user.
  TextBox1.Text = myStringBuilder.ToString
  
End Sub
            
Private Sub Button_AddFeatureLayer_Click(sender As System.Object, e As System.Windows.RoutedEventArgs)
  
  ' Display the existing FeatureLayer in the Map. 
  ' NOTE: The FeatureLayer.Initialized Event will NOT fire a second time. It only executes once.
  Map1.Layers.Add(_MyFeatureLayer)
  
End Sub

Requirements

Target Platforms: Windows XP Professional, Windows Server 2003 family, Windows Vista, Windows Server 2008 family, Windows 7

See Also

© ESRI, Inc. All Rights Reserved.