ArcGIS API for Silverlight - Library Reference
InitializationFailed Event
See Also  Example Send comments on this topic
ESRI.ArcGIS.Client Namespace > Layer Class : InitializationFailed Event

This Event fires when a Layer has been initialized and has encountered a problem.

Syntax

Visual Basic (Declaration) 
Public Event InitializationFailed As EventHandler(Of EventArgs)
C# 
public event EventHandler<EventArgs> InitializationFailed

Remarks

There are several reasons why a Layer may have problems and cause the InitializationFailed Event to execute. Some of these reasons include: ArcGIS Server may be down, the internet connection for the application may be down, an incorrect URL for the layer might have been used, the wrong Layer Type (i.e. ArcGISDynamicMapServiceLayer, ArcGISTiledMapServiceLayer, FeatureLayer, etc.) may have been specified for the URL that would have ordinarily worked properly if the correct Layer Type was specified, etc.

The code execution pattern for Layers is: first the Layer.Initialized Event fires and then if there is a problem the Layer.InitializationFailed Event fires. If the Layer loads correctly, the InitializationFailed Event will not execute. See the following screen shot:

Visual representaion of the flow of Layer.Initialize and Layer.InitializationFailed Events.

Because the Layer.Initialized Event always executes, it is a highly recommended coding practice to wrap any coding logic that will use Properties or Methods of the Layer within an If/Then statement that checks to make sure that the Layer.InitializationFailure Property is Nothing/null. See the code example in this document for a demonstration of this pattern.

If a Layer fails to initialize and the Layer.IntializationFailed Event stub code has not been generated in the code-behind, and Unhandled Exception will be thrown which will terminate your application. See the following screen shot:

Example of an 'Unhandled Exception' when the Layer.IntializationFailed Event is not used and an error is in the Layer.

Therefore, it is a best practice to always have the code-behind stubs for the Layer.IntializationFailed Event in place for every Layer in your application that loads in the Map. The Layer.IntializationFailed Event stubs are automatically generated in code-behind when the InitializedFailed attribute is specified in XAML. You can also dynamically add Layer.IntializationFailed Event stubs in the code-behind.

You can use a separate Layer.IntializationFailed Event for every Layer in your application or you can have multiple Layers use a single Layer.IntializationFailed Event. See the code example in this document for a demonstration of this pattern.

To access the System.Exception error that occurs when the Layer.InitializationFailed Event fires, use the Layer.InitializationFailure Property. Two very helpful Properties on the System.Exception are the .InnerException and .Message. Use the other System.Exception Properties as needed. In .NET there are numerous Exception Types and the System.Exception is the base. Some of the more common Exception Types that can be returned from the Layer.InitializationFailure Property include: System.NotSupportedException, System.Security.SecurityException, and System.Net.WebException.

Example

How to use:

When the application loads a couple of error message dialogs will appear for Layers that have some kind of problem. Other Layers will load appropriately and a Map will be generated. Read the notes in the XAML and code-behind to experiment with altering the sample to see how error handling occurs in the Initialized and InitializationFailed Events.

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.

Example of how the Layer.InitializationFailed Event can be used to trap errors.

XAMLCopy Code
<Grid x:Name="LayoutRoot">
    
  <!-- Add a Map Control and zoom the the continental US. -->
  <esri:Map Background="White" HorizontalAlignment="Left" Margin="12,186,0,0" Name="Map1" WrapAround="True"
        VerticalAlignment="Top" Height="402" Width="594" Extent="-14188202,2354135,-7405065,6944744">
    <esri:Map.Layers>
      <esri:LayerCollection>
           
        <!-- 
        This ArcGISTiledMapServiceLayer will display in the Map without any errors. If you want to make an error occur
        and see what the InitializationFailed Event does, insert some bad text in the Url.
        -->
        <esri:ArcGISTiledMapServiceLayer ID="World_Light_Gray_Base" 
              Url="http://server.arcgisonline.com/ArcGIS/rest/services/Canvas/World_Light_Gray_Base/MapServer" 
              Initialized="ArcGISTiledMapServiceLayer_Initialized"
              InitializationFailed="ArcGISTiledMapServiceLayer_InitializationFailed"/>
              
        <!-- 
        This ArcGISDynamicMapServiceLayer will display in the Map without any errors. If you want to make an error occur
        and see what the InitializationFailed Event does, insert some bad text in the Url.
        Note how you can have a seperate Initialized and InitializationFailed Events for every type of layer 
        (ArcGISDynamicMapServiceLayer, ArcGISTiledServiceLayer, FeatureLayer, etc.) if you desire for complete control 
        over your error handling.
        -->
        <esri:ArcGISDynamicMapServiceLayer ID="Population_Change"
              Url="http://server.arcgisonline.com/ArcGIS/rest/services/Demographics/USA_1990-2000_Population_Change/MapServer"
              Initialized="ArcGISDynamicMapServiceLayer_Initialized"
              InitializationFailed="ArcGISDynamicMapServiceLayer_InitializationFailed"/>
        
        <!-- 
        This FeatureLayer will display in the Map without any errors. If you want to make an error occur
        and see what the InitializationFailed Event does, insert some bad text in the Url.
        Note this FeatureLayer shares the Initialized and InitializationFailed Events with the next FeatureLayer.
        You could have all layers in a project share one Initialized Event and one InitializationFalied
        Event and use a bunch of If/Then, Case/Switch, etc. type of logic to interrogate the Layer Types and take
        action accordingly. 
        -->
        <esri:FeatureLayer ID="Earthquakes"
              Url="http://sampleserver3.arcgisonline.com/ArcGIS/rest/services/Earthquakes/Since_1970/MapServer/0"
              Initialized="Generic_Layer_Initialized"
              InitializationFailed="Generic_Layer_InitializationFailed"/>
        
        <!-- 
        This FeatureLayer will not display in the Map; it has errors. A MessageBox will appear when the application 
        is loading telling you something is wrong with this service. You could add code in the InitializedFailed Event 
        to handle things like re-loading the Map without the bad layer, switching to a different layer, etc.
        Note: this FeatureLayer shares the Initialized and InitializationFailed Events with the above FeatureLayer.
        -->
        <esri:FeatureLayer ID="BOGUS"
              Url="http://BOGUS.BOGUS.com/ArcGIS/rest/services/BOGUS/MapServer/0"
              Initialized="Generic_Layer_Initialized"
              InitializationFailed="Generic_Layer_InitializationFailed"/>
        
        <!-- 
        This ArcGISTiledMapServiceLayer will not display in the Map; it has errors. A MessageBox will appear when the 
        application is loading telling you something is wrong with this service. In this case no Cached Map Tiles were 
        created when the web service was initially published (Remember that ArcGISDynamicMapServiceLayers and 
        ArcGISTiledMapSericeLayers share the exact same URL syntax. It is when the Layer has Cached Map Tiles generated in
        the service as it is initially created that allows an ArcGISTiledMapServiceLayer to be used). You could change this 
        to an ArcGISDynamicMapServiceLayer and it should work fine.
        Note: this ArcGISTiledMapServiceLayer shares the Initialized and InitializationFailed Events with the above 
        other two FeatureLayers.
        -->
        <esri:ArcGISTiledMapServiceLayer ID="MyPoints_WithProblems" 
              Url="http://sampleserver3.arcgisonline.com/ArcGIS/rest/services/Earthquakes/Since_1970/MapServer" 
              Initialized="Generic_Layer_Initialized"
              InitializationFailed="Generic_Layer_InitializationFailed"/>
        
        <!-- 
        This FeatureLayer (which is currently commented out) will not display in the Map; it has errors. As a matter 
        of fact it will bring your application crashing down with an 'Unhandled Exception' in Visual Studio if you 
        were to uncomment out the next line of XAML - try it to see what happens. This is because no 
        InitializationFailed Event handler is wired up. The moral of the story is that in your production application, 
        always use appropriate error handling using the InitializationFailed Event to catch for potential problems 
        (never assume that a web service will always be running, it is the web after all).
        -->
        <!--<esri:FeatureLayer ID="BOGUS2" Url="http://BOGUS2.BOGUS2.com/ArcGIS/rest/services/BOGUS2/MapServer/0"/>-->
      
      </esri:LayerCollection>
    </esri:Map.Layers>
  </esri:Map>
  
  <!-- Provide the instructions on how to use the sample code. -->
  <TextBlock Height="142" HorizontalAlignment="Left" Name="TextBlock1" VerticalAlignment="Top" Width="606" 
             TextWrapping="Wrap" Text="When the application loads a couple of error message dialogs will appear
             for Layers that have some kind of problem. Other Layers will load appropriately and a Map will be
             generated. Read the notes in the XAML and code-behind to experiment with altering the sample to
             see how error handling occurs in the Initialized and InitializationFailed Events." />
    
</Grid>
C#Copy Code
#region TestCase1
  // **************************************************************************************************
  // This code block handles the first Layer in the LayerCollection defined in XAML. The Initialized
  // and InitializationFailed Events are for an ArcGITiledMapServiceLayer with the ID of 
  // "World_Light_Gray_Base". The Layer should load with out any errors, use break points to see how the
  // code execution occurs when the application loads initially. The Initialized Event should fire without 
  // issue. If there are no problems with the Layer loading the InitializationFailed should not execute.
  
  private void ArcGISTiledMapServiceLayer_Initialized(object sender, System.EventArgs e)
  {
    // Get the ArcGISTiledMapServiceLayer from what was defined in XAML. Using the 'sender' in this case.
    ESRI.ArcGIS.Client.ArcGISTiledMapServiceLayer myArcGISTiledMapServiceLayer = null;
    myArcGISTiledMapServiceLayer = (ESRI.ArcGIS.Client.ArcGISTiledMapServiceLayer)sender;
    
    // It is a good practice to check the ArcGISTiledMapServiceLayer.InitializationFailure Property to ensure 
    // that it is null and write your code to do whatever needs done within that if statement. The 
    // reason is that the ArcGISTiledMapServiceLayer.Initialized Event ALWAYS executes before the 
    // ArcGISTiledMapServiceLayer.InitializationFailed Event. By using the 
    // ArcGISTiledMapServiceLayer.InitializationFailure Property to test if no errors are occuring, your code
    // can execute as expected without runtime errors. Use the ArcGISTiledMapServiceLayer.InitializationFailed 
    // Event to trap for any error and handle appropriately. The ArcGISTiledMapServiceLayer.InitializationFailed 
    // Event will only execute if there is an error.
    if (myArcGISTiledMapServiceLayer.InitializationFailure == null)
    {
      string myID = myArcGISTiledMapServiceLayer.ID;
      string myMapName = myArcGISTiledMapServiceLayer.MapName;
    }
  }
  
  private void ArcGISTiledMapServiceLayer_InitializationFailed(object sender, System.EventArgs e)
  {
    // The ArcGISTiledMapServiceLayer.InitializationFailed Event only fires when there is a problem with
    // the Layer. This would be a good place to handle errors, display information about the issue to the
    // user, and take the corrective action if necessary.
    
    // Get the ArcGISTiledMapServiceLayer from what was defined in XAML. Using the 'sender' in this case.
    ESRI.ArcGIS.Client.ArcGISTiledMapServiceLayer myArcGISTiledMapServiceLayer = null;
    myArcGISTiledMapServiceLayer = (ESRI.ArcGIS.Client.ArcGISTiledMapServiceLayer)sender;
    
    // Display the error to the user in a MessageBox. The ArcGISTiledMapServiceLayer.InitializationFailure
    // Property returns a System.Exception object. It has many Properties that can be interrogated
    // to figure out what the issue is and take action. Two very helpful Properties on the 
    // System.Exception are the .InnerException and .Message. Use the other System.Exception
    // Properties as needed. Note: there is no need to test if we have an error using the 
    // ArcGISTiledMapServiceLayer.InitializationFailure Property because this whole function would not
    // execute unless we had an error.
    System.Exception myGeneralException = myArcGISTiledMapServiceLayer.InitializationFailure;
    string myMessage = myGeneralException.Message;
    string myInnerException = myGeneralException.InnerException.ToString();
    MessageBox.Show(myMessage + Environment.NewLine + myInnerException,
      "ArcGISTiledMapServiceLayer_InitializationFailed", MessageBoxButton.OK);
  }
  // **************************************************************************************************
#endregion
             
#region TestCase2
  // **************************************************************************************************
  // This code block handles the second Layer in the LayerCollection defined in XAML. The Initialized
  // and InitializationFailed Events are for an ArcGIDynamicMapServiceLayer with the ID of 
  // "Population_Change". The Layer should load with out any errors, use break points to see how the
  // code execution occurs when the application loads initially. The Initialized Event should fire without 
  // issue. If there are no problems with the Layer loading the InitializationFailed should not execute.
  
  private void ArcGISDynamicMapServiceLayer_Initialized(object sender, System.EventArgs e)
  {
    // Get the ArcGISDynamicMapServiceLayer from what was defined in XAML. Using the item ID in this case.
    ESRI.ArcGIS.Client.ArcGISDynamicMapServiceLayer myArcGISDynamicMapServiceLayer = null;
    myArcGISDynamicMapServiceLayer = (ESRI.ArcGIS.Client.ArcGISDynamicMapServiceLayer)Map1.Layers["Population_Change"];
    
    // It is a good practice to check the ArcGISDynamicMapServiceLayer.InitializationFailure Property to ensure 
    // that it is null and write your code to do whatever needs done within that if statement. The 
    // reason is that the ArcGISDynamicMapServiceLayer.Initialized Event ALWAYS executes before the 
    // ArcGISDynamicMapServiceLayer.InitializationFailed Event. By using the 
    // ArcGISDynamicMapServiceLayer.InitializationFailure Property to test if no errors are occuring, your code
    // can execute as expected without runtime errors. Use the ArcGISDynamicMapServiceLayer.InitializationFailed 
    // Event to trap for any error and handle appropriately. The ArcGISDynamicMapServiceLayer.InitializationFailed 
    // Event will only execute if there is an error.
    if (myArcGISDynamicMapServiceLayer.InitializationFailure == null)
    {
      string myID = myArcGISDynamicMapServiceLayer.ID;
      string myMapName = myArcGISDynamicMapServiceLayer.MapName;
    }
  }
  
  private void ArcGISDynamicMapServiceLayer_InitializationFailed(object sender, System.EventArgs e)
  {
    // The ArcGISDynamicMapServiceLayer.InitializationFailed Event only fires when there is a problem with
    // the Layer. This would be a good place to handle errors, display information about the issue to the
    // user, and take the corrective action if necessary.
    
    // Get the ArcGISDynamicMapServiceLayer from what was defined in XAML. Using the item ID in this case.
    ESRI.ArcGIS.Client.ArcGISDynamicMapServiceLayer myArcGISDynamicMapServiceLayer = null;
    myArcGISDynamicMapServiceLayer = (ESRI.ArcGIS.Client.ArcGISDynamicMapServiceLayer)Map1.Layers["Population_Change"];
    
    // Display the error to the user in a MessageBox. The ArcGISDynamicMapServiceLayer.InitializationFailure
    // Property returns a System.Exception object. It has many Properties that can be interrogated
    // to figure out what the issue is and take action. Two very helpful Properties on the 
    // System.Exception are the .InnerException and .Message. Use the other System.Exception
    // Properties as needed. Note: there is no need to test if we have an error using the 
    // ArcGISDynamicMapServiceLayer.InitializationFailure Property because this whole function would not
    // execute unless we had an error.
    System.Exception myGeneralException = myArcGISDynamicMapServiceLayer.InitializationFailure;
    string myMessage = myGeneralException.Message;
    string myInnerException = myGeneralException.InnerException.ToString();
    MessageBox.Show(myMessage + Environment.NewLine + myInnerException,
      "ArcGISDynamicMapServiceLayer_InitializationFailed", MessageBoxButton.OK);
  }
  // **************************************************************************************************
#endregion
             
#region TestCase3
  // **************************************************************************************************
  // This code block handles the third, fourth, and fifth Layers in the LayerCollection defined in XAML. The 
  // Initialized and InitializationFailed Events are for an FeatureLayer with the IDs of 
  // "Earthquakes" and "BOGUS" AND the ArcGISTiledMapServiceLayer with the ID of "MyPoints_WithProblems". 
  // The "Earthquakes" Layer should load with out any errors. The "BOGUS" and "MyPoints_WithProblems" Layers 
  // intentionally have problems with their URLs and will have runtime errors occuring. Use break points 
  // to see how the code execution occurs when the application loads initially. Remember the Initialized 
  // Event always fires first and then if there are problems with the Layer the InitializationFailed Event 
  // will fire.
  
  private void Generic_Layer_Initialized(object sender, System.EventArgs e)
  {
    // Get the Layer from what was defined in XAML. Using the 'sender' in this case. We are using
    // the generic Layer Type for the 'sender' at this point and will cast to a more specific Layer
    // Type after we have determined that we do not have any errors.
    ESRI.ArcGIS.Client.Layer myGenericLayer = (ESRI.ArcGIS.Client.Layer)sender;
    
    // It is a good practice to check the Layer.InitializationFailure Property to ensure that it is null 
    // and write your code to do whatever needs done within that if statement. The reason is that the 
    // Layer.Initialized Event ALWAYS executes before the Layer.InitializationFailed Event. By using the 
    // Layer.InitializationFailure Property to test if no errors are occuring, your code can execute as 
    // expected without runtime errors. Use the Layer.InitializationFailed Event to trap for any error and 
    // handle appropriately. The Layer.InitializationFailed Event will only execute if there is an error.
    if (myGenericLayer.InitializationFailure == null)
    {
      string myID = null;
      
      // Do 'is' testing to see exactly what Type of Layer we have and then branch the code accordingly.
      if (myGenericLayer is ESRI.ArcGIS.Client.FeatureLayer)
      {
        // We have a FeatureLayer.
        ESRI.ArcGIS.Client.FeatureLayer myFeatureLayer = null;
        myFeatureLayer = (ESRI.ArcGIS.Client.FeatureLayer)myGenericLayer;
        myID = myFeatureLayer.ID;
      }
      else if (myGenericLayer is ESRI.ArcGIS.Client.ArcGISDynamicMapServiceLayer)
      {
        // We have an ArcGISTiledMapServiceLayer. 
        ESRI.ArcGIS.Client.ArcGISTiledMapServiceLayer myArcGISTiledMapServiceLayer = null;
        myArcGISTiledMapServiceLayer = (ESRI.ArcGIS.Client.ArcGISTiledMapServiceLayer)myGenericLayer;
        myID = myArcGISTiledMapServiceLayer.ID;
      }
      else
      {
        // TODO: There are several other Layer Types. Write you own code accordingly.
      }
    }
  }
  
  private void Generic_Layer_InitializationFailed(object sender, System.EventArgs e)
  {
    // The Layer.InitializationFailed Event only fires when there is a problem with the Layer. This would 
    // be a good place to handle errors, display information about the issue to the user, and take the 
    // corrective action if necessary.
    
    // Get the Layer from what was defined in XAML. Using the 'sender' in this case. We are using
    // the generic Layer Type for the 'sender' at this point and could cast to a more specific Layer
    // Type if desired.
    ESRI.ArcGIS.Client.Layer myGenericLayer = (ESRI.ArcGIS.Client.Layer)sender;
    
    // Display the error to the user in a MessageBox. The Layer.InitializationFailure Property returns 
    // a System.Exception object. It has many Properties that can be interrogated to figure out what the 
    // issue is and take action. Two very helpful Properties on the System.Exception are the .InnerException 
    // and .Message. Use the other System.Exception Properties as needed. Note: there is no need to test if 
    // we have an error using the Layer.InitializationFailure Property because this whole function would not
    // execute unless we had an error.
    // In .NET there are numerous Exception Types. The System.Exception is the base for many other
    // Exception Types. This code block has been enhanced as compared to the other InitializationFailed 
    // functions above to cast the generic System.Exception into more specific Types like:
    // System.NotSupportedException, System.Security.SecurityException, System.Net.WebException, etc.
    // Modify the code to catch even more errors Types as needed.
    
    System.Exception myGeneralException = myGenericLayer.InitializationFailure;
    string myMessage = "";
    string myInnerException = "";
    
    if (myGeneralException is System.NotSupportedException)
    {
      System.NotSupportedException myNotSupportedException = (System.NotSupportedException)myGeneralException;
      if (myNotSupportedException.Message != null)
      {
        myMessage = myNotSupportedException.Message;
      }
      if (myNotSupportedException.InnerException != null)
      {
        myInnerException = myNotSupportedException.InnerException.ToString();
      }
    }
    else if (myGeneralException is System.Security.SecurityException)
    {
      System.Security.SecurityException mySecurityException = (System.Security.SecurityException)myGeneralException;
      if (mySecurityException.Message != null)
      {
        myMessage = mySecurityException.Message;
      }
      if (mySecurityException.InnerException != null)
      {
        myInnerException = mySecurityException.InnerException.ToString();
      }
    }
    else if (myGeneralException is System.Net.WebException)
    {
      System.Net.WebException myWebException = (System.Net.WebException)myGeneralException;
      if (myWebException.Message != null)
      {
        myMessage = myWebException.Message;
      }
      if (myWebException.InnerException != null)
      {
        myInnerException = myWebException.InnerException.ToString();
      }
    }
    MessageBox.Show(myMessage + Environment.NewLine + myInnerException, "Layer_InitializationFailed", MessageBoxButton.OK);
  }
  // **************************************************************************************************
#endregion
VB.NETCopy Code
#Region "TestCase1"
  ' **************************************************************************************************
  ' This code block handles the first Layer in the LayerCollection defined in XAML. The Initialized
  ' and InitializationFailed Events are for an ArcGITiledMapServiceLayer with the ID of 
  ' "World_Light_Gray_Base". The Layer should load with out any errors, use break points to see how the
  ' code execution occurs when the application loads initially. The Initialized Event should fire without 
  ' issue. If there are no problems with the Layer loading the InitializationFailed should not execute.
  
  Private Sub ArcGISTiledMapServiceLayer_Initialized(sender As System.Object, e As System.EventArgs)
    
    ' Get the ArcGISTiledMapServiceLayer from what was defined in XAML. Using the 'sender' in this case.
    Dim myArcGISTiledMapServiceLayer As ESRI.ArcGIS.Client.ArcGISTiledMapServiceLayer
    myArcGISTiledMapServiceLayer = CType(sender, ESRI.ArcGIS.Client.ArcGISTiledMapServiceLayer)
    
    ' It is a good practice to check the ArcGISTiledMapServiceLayer.InitializationFailure Property to ensure 
    ' that it is Nothing and write your code to do whatever needs done within that If/Then statement. The 
    ' reason is that the ArcGISTiledMapServiceLayer.Initialized Event ALWAYS executes before the 
    ' ArcGISTiledMapServiceLayer.InitializationFailed Event. By using the 
    ' ArcGISTiledMapServiceLayer.InitializationFailure Property to test if no errors are occuring, your code
    ' can execute as expected without runtime errors. Use the ArcGISTiledMapServiceLayer.InitializationFailed 
    ' Event to trap for any error and handle appropriately. The ArcGISTiledMapServiceLayer.InitializationFailed 
    ' Event will only execute if there is an error.
    If myArcGISTiledMapServiceLayer.InitializationFailure Is Nothing Then
      Dim myID As String = myArcGISTiledMapServiceLayer.ID
      Dim myMapName As String = myArcGISTiledMapServiceLayer.MapName
    End If
    
  End Sub
  
  Private Sub ArcGISTiledMapServiceLayer_InitializationFailed(sender As System.Object, e As System.EventArgs)
    
    ' The ArcGISTiledMapServiceLayer.InitializationFailed Event only fires when there is a problem with
    ' the Layer. This would be a good place to handle errors, display information about the issue to the
    ' user, and take the corrective action if necessary.
    
    ' Get the ArcGISTiledMapServiceLayer from what was defined in XAML. Using the 'sender' in this case.
    Dim myArcGISTiledMapServiceLayer As ESRI.ArcGIS.Client.ArcGISTiledMapServiceLayer
    myArcGISTiledMapServiceLayer = CType(sender, ESRI.ArcGIS.Client.ArcGISTiledMapServiceLayer)
    
    ' Display the error to the user in a MessageBox. The ArcGISTiledMapServiceLayer.InitializationFailure
    ' Property returns a System.Exception object. It has many Properties that can be interrogated
    ' to figure out what the issue is and take action. Two very helpful Properties on the 
    ' System.Exception are the .InnerException and .Message. Use the other System.Exception
    ' Properties as needed. Note: there is no need to test if we have an error using the 
    ' ArcGISTiledMapServiceLayer.InitializationFailure Property because this whole function would not
    ' execute unless we had an error.
    Dim myGeneralException As System.Exception = myArcGISTiledMapServiceLayer.InitializationFailure
    Dim myMessage As String = myGeneralException.Message
    Dim myInnerException As String = myGeneralException.InnerException.ToString
    MessageBox.Show(myMessage + vbCrLf + myInnerException,
                    "ArcGISTiledMapServiceLayer_InitializationFailed", MessageBoxButton.OK)
    
  End Sub
  ' **************************************************************************************************
#End Region
             
#Region "TestCase2"
  ' **************************************************************************************************
  ' This code block handles the second Layer in the LayerCollection defined in XAML. The Initialized
  ' and InitializationFailed Events are for an ArcGIDynamicMapServiceLayer with the ID of 
  ' "Population_Change". The Layer should load with out any errors, use break points to see how the
  ' code execution occurs when the application loads initially. The Initialized Event should fire without 
  ' issue. If there are no problems with the Layer loading the InitializationFailed should not execute.
  
  Private Sub ArcGISDynamicMapServiceLayer_Initialized(sender As System.Object, e As System.EventArgs)
    
    ' Get the ArcGISDynamicMapServiceLayer from what was defined in XAML. Using the item ID in this case.
    Dim myArcGISDynamicMapServiceLayer As ESRI.ArcGIS.Client.ArcGISDynamicMapServiceLayer
    myArcGISDynamicMapServiceLayer = Map1.Layers.Item("Population_Change")
    
    ' It is a good practice to check the ArcGISDynamicMapServiceLayer.InitializationFailure Property to ensure 
    ' that it is Nothing and write your code to do whatever needs done within that If/Then statement. The 
    ' reason is that the ArcGISDynamicMapServiceLayer.Initialized Event ALWAYS executes before the 
    ' ArcGISDynamicMapServiceLayer.InitializationFailed Event. By using the 
    ' ArcGISDynamicMapServiceLayer.InitializationFailure Property to test if no errors are occuring, your code
    ' can execute as expected without runtime errors. Use the ArcGISDynamicMapServiceLayer.InitializationFailed 
    ' Event to trap for any error and handle appropriately. The ArcGISDynamicMapServiceLayer.InitializationFailed 
    ' Event will only execute if there is an error.
    If myArcGISDynamicMapServiceLayer.InitializationFailure Is Nothing Then
      Dim myID As String = myArcGISDynamicMapServiceLayer.ID
      Dim myMapName As String = myArcGISDynamicMapServiceLayer.MapName
    End If
    
  End Sub
  
  Private Sub ArcGISDynamicMapServiceLayer_InitializationFailed(sender As System.Object, e As System.EventArgs)
    
    ' The ArcGISDynamicMapServiceLayer.InitializationFailed Event only fires when there is a problem with
    ' the Layer. This would be a good place to handle errors, display information about the issue to the
    ' user, and take the corrective action if necessary.
    
    ' Get the ArcGISDynamicMapServiceLayer from what was defined in XAML. Using the item ID in this case.
    Dim myArcGISDynamicMapServiceLayer As ESRI.ArcGIS.Client.ArcGISDynamicMapServiceLayer
    myArcGISDynamicMapServiceLayer = Map1.Layers.Item("Population_Change")
    
    ' Display the error to the user in a MessageBox. The ArcGISDynamicMapServiceLayer.InitializationFailure
    ' Property returns a System.Exception object. It has many Properties that can be interrogated
    ' to figure out what the issue is and take action. Two very helpful Properties on the 
    ' System.Exception are the .InnerException and .Message. Use the other System.Exception
    ' Properties as needed. Note: there is no need to test if we have an error using the 
    ' ArcGISDynamicMapServiceLayer.InitializationFailure Property because this whole function would not
    ' execute unless we had an error.
    Dim myGeneralException As System.Exception = myArcGISDynamicMapServiceLayer.InitializationFailure
    Dim myMessage As String = myGeneralException.Message
    Dim myInnerException As String = myGeneralException.InnerException.ToString
    MessageBox.Show(myMessage + vbCrLf + myInnerException,
                    "ArcGISDynamicMapServiceLayer_InitializationFailed", MessageBoxButton.OK)
    
  End Sub
  ' **************************************************************************************************
#End Region
             
#Region "TestCase3"
  ' **************************************************************************************************
  ' This code block handles the third, fourth, and fifth Layers in the LayerCollection defined in XAML. The 
  ' Initialized and InitializationFailed Events are for an FeatureLayer with the IDs of 
  ' "Earthquakes" and "BOGUS" AND the ArcGISTiledMapServiceLayer with the ID of "MyPoints_WithProblems". 
  ' The "Earthquakes" Layer should load with out any errors. The "BOGUS" and "MyPoints_WithProblems" Layers 
  ' intentionally have problems with their URLs and will have runtime errors occuring. Use break points 
  ' to see how the code execution occurs when the application loads initially. Remember the Initialized 
  ' Event always fires first and then if there are problems with the Layer the InitializationFailed Event 
  ' will fire.
  
  Private Sub Generic_Layer_Initialized(sender As System.Object, e As System.EventArgs)
    
    ' Get the Layer from what was defined in XAML. Using the 'sender' in this case. We are using
    ' the generic Layer Type for the 'sender' at this point and will cast to a more specific Layer
    ' Type after we have determined that we do not have any errors.
    Dim myGenericLayer As ESRI.ArcGIS.Client.Layer = CType(sender, ESRI.ArcGIS.Client.Layer)
    
    ' It is a good practice to check the Layer.InitializationFailure Property to ensure that it is Nothing 
    ' and write your code to do whatever needs done within that If/Then statement. The reason is that the 
    ' Layer.Initialized Event ALWAYS executes before the Layer.InitializationFailed Event. By using the 
    ' Layer.InitializationFailure Property to test if no errors are occuring, your code can execute as 
    ' expected without runtime errors. Use the Layer.InitializationFailed Event to trap for any error and 
    ' handle appropriately. The Layer.InitializationFailed Event will only execute if there is an error.
    If myGenericLayer.InitializationFailure Is Nothing Then
      
      Dim myID As String = Nothing
      
      ' Do TypeOf testing to see exactly what Type of Layer we have and then branch the code accordingly.
      If TypeOf myGenericLayer Is ESRI.ArcGIS.Client.FeatureLayer Then
        
        ' We have a FeatureLayer.
        Dim myFeatureLayer As ESRI.ArcGIS.Client.FeatureLayer
        myFeatureLayer = CType(myGenericLayer, ESRI.ArcGIS.Client.FeatureLayer)
        myID = myFeatureLayer.ID
        
      ElseIf TypeOf myGenericLayer Is ESRI.ArcGIS.Client.ArcGISDynamicMapServiceLayer Then
        
        ' We have an ArcGISTiledMapServiceLayer. 
        Dim myArcGISTiledMapServiceLayer As ESRI.ArcGIS.Client.ArcGISTiledMapServiceLayer
        myArcGISTiledMapServiceLayer = CType(myGenericLayer, ESRI.ArcGIS.Client.ArcGISTiledMapServiceLayer)
        myID = myArcGISTiledMapServiceLayer.ID
        
      Else
        
        ' TODO: There are several other Layer Types. Write you own code accordingly.
        
      End If
      
    End If
    
  End Sub
  
  Private Sub Generic_Layer_InitializationFailed(sender As System.Object, e As System.EventArgs)
    
    ' The Layer.InitializationFailed Event only fires when there is a problem with the Layer. This would 
    ' be a good place to handle errors, display information about the issue to the user, and take the 
    ' corrective action if necessary.
    
    ' Get the Layer from what was defined in XAML. Using the 'sender' in this case. We are using
    ' the generic Layer Type for the 'sender' at this point and could cast to a more specific Layer
    ' Type if desired.
    Dim myGenericLayer As ESRI.ArcGIS.Client.Layer = CType(sender, ESRI.ArcGIS.Client.Layer)
    
    ' Display the error to the user in a MessageBox. The Layer.InitializationFailure Property returns 
    ' a System.Exception object. It has many Properties that can be interrogated to figure out what the 
    ' issue is and take action. Two very helpful Properties on the System.Exception are the .InnerException 
    ' and .Message. Use the other System.Exception Properties as needed. Note: there is no need to test if 
    ' we have an error using the Layer.InitializationFailure Property because this whole function would not
    ' execute unless we had an error.
    ' In .NET there are numerous Exception Types. The System.Exception is the base for many other
    ' Exception Types. This code block has been enhanced as compared to the other InitializationFailed 
    ' functions above to cast the generic System.Exception into more specific Types like:
    ' System.NotSupportedException, System.Security.SecurityException, System.Net.WebException, etc.
    ' Modify the code to catch even more errors Types as needed.
    
    Dim myGeneralException As System.Exception = myGenericLayer.InitializationFailure
    Dim myMessage As String = ""
    Dim myInnerException As String = ""
    
    If TypeOf myGeneralException Is System.NotSupportedException Then
      
      Dim myNotSupportedException As System.NotSupportedException = myGeneralException
      
      If myNotSupportedException.Message IsNot Nothing Then
        myMessage = myNotSupportedException.Message
      End If
      
      If myNotSupportedException.InnerException IsNot Nothing Then
        myInnerException = myNotSupportedException.InnerException.ToString
      End If
      
    ElseIf TypeOf myGeneralException Is System.Security.SecurityException Then
      
      Dim mySecurityException As System.Security.SecurityException = myGeneralException
        
      If mySecurityException.Message IsNot Nothing Then
        myMessage = mySecurityException.Message
      End If
        
      If mySecurityException.InnerException IsNot Nothing Then
        myInnerException = mySecurityException.InnerException.ToString
      End If
      
    ElseIf TypeOf myGeneralException Is System.Net.WebException Then
      
      Dim myWebException As System.Net.WebException = myGeneralException
      
      If myWebException.Message IsNot Nothing Then
        myMessage = myWebException.Message
      End If
      
      If myWebException.InnerException IsNot Nothing Then
        myInnerException = myWebException.InnerException.ToString
      End If
      
    End If
    
    MessageBox.Show(myMessage + vbCrLf + myInnerException, "Layer_InitializationFailed", MessageBoxButton.OK)
    
  End Sub
  ' **************************************************************************************************
#End Region

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.