Creating a map behavior

Map behaviors provide a simple way to surface functionality in the ArcGIS Viewer for Silverlight that should always be enabled. Map behaviors provide functionality without the need for user interaction. If you are developing logic for the Viewer that should always be initiated whenever the Viewer is loaded, then the logic should be encapsulated as a behavior. Behaviors can be used, for instance, to keep the map at a certain extent.

See Best Practices: Getting Started and Extending the ArcGIS Viewer for Silverlight for more information.

Behaviors that are developed for use with the Viewer must inherit from System.Windows.Interactivity.Behavior<ESRI.ArcGIS.Client.Map>. The Behavior<T> base class provides a few simple members. In the context of the Viewer, these members can be understood as follows:

To make a behavior available to be added to the Application Builder, you must add two attributes to your behavior class. The first is System.ComponentModel.Composition.ExportAttribute, which is included in the System.ComponentModel.Composition assembly provided as part of Microsoft's Managed Extensibility Framework (MEF). This attribute informs the Application Builder that the behavior should be made available. When you include it on a behavior you implement, it will always take the following form:

[Export(typeof(Behavior<Map>))]
public class MyBehavior : Behavior<Map>

The other required attribute to add is ESRI.ArcGIS.Client.Extensibility.DisplayNameAttribute. This determines the name of the behavior as it appears to a designer when adding it to the Viewer. Additional optional attributes that you can specify from the ESRI.ArcGIS.Client.Extensibility assembly include CategoryAttribute and DescriptionAttribute. The attributes should be specified as follows:

[Export(typeof(Behavior<Map>))]
[DisplayName("Show Current Extent")]
[Category("My Behaviors")]
[Description("Shows a message box with the current extent when the extent changes")]
public class MyBehavior : Behavior<Map>

An example of a simple behavior is shown below. The behavior adds a handler to the map's ExtentChanged event when the behavior is attached to the map and removes it when the behavior is detached. The behavior shows a message box with the current extent when the extent changes.

[Export(typeof(Behavior<Map>))]
[DisplayName("Simple Behavior")]
[Category("My Behaviors")]
[Description("Shows a message box with the current extent when the extent changes")]
public class MyBehavior : Behavior<Map>
{
     protected override void OnAttached()
     {
          base.OnAttached();
 
          // Add a handler to the map's ExtentChanged event.
          this.AssociatedObject.ExtentChanged += OnExtentChanged;
     }
 
     private void OnExtentChanged(object s, ExtentEventArgs args)
     {
          // Show a message box with the new extent.
          MessageBox.Show(this.AssociatedObject.Extent.ToString());
     }
 
     protected override void OnDetaching()
     {
          // Remove the handler from the map's ExtentChanged event.
          this.AssociatedObject.ExtentChanged -= OnExtentChanged;
     }
}
1/26/2015