dot
Find task

The Find task allows you to search one or more layers in a map for features with attribute values that match or contain an input value. Once the matching features are returned, you can use .NET code to display their geometries and attributes in your Silverlight application. To use a Find task, you will need to include code to define its user interface and specify its execution logic.

An example of XAML and .NET code (in this case C#) for a simple Silverlight application that includes a Find task is shown below. This application defines a Find task that uses a TextBox control for specifying the input value and a button for executing the task. Result features are displayed in a GraphicsLayer that has MapTips enabled. The rest of this document will walk you through how the Find task is defined in the example.

[XAML]

<UserControl x:Class="SilverlightApp.Page"
	xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
	xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
	xmlns:esri="clr-namespace:ESRI.ArcGIS.Client;assembly=ESRI.ArcGIS.Client"
	xmlns:esriConverters="clr-namespace:ESRI.ArcGIS.Client.ValueConverters;assembly=ESRI.ArcGIS.Client"
	xmlns:esriSymbols="clr-namespace:ESRI.ArcGIS.Client.Symbols;assembly=ESRI.ArcGIS.Client">

	<Grid x:Name="LayoutRoot" Background="White">
		
		<!-- FIND TASK OUTPUT RESOURCES -->
		<Grid.Resources>
			<esriConverters:DictionaryConverter x:Name="MyDictionaryConverter" />
			<esriSymbols:SimpleFillSymbol x:Name="ResultsFillSymbol" Fill="#64FF0000" BorderBrush="Red"
				BorderThickness="2" />
		</Grid.Resources>

		<!-- MAP -->
		<esri:Map x:Name="MyMap" Extent="-125, 25, -65, 55" >
			<esri:Map.Layers>
				<esri:ArcGISTiledMapServiceLayer ID="StreetMapLayer" 
					Url="http://server.arcgisonline.com/ArcGIS/rest/services/ESRI_StreetMap_World_2D/MapServer"/>
				<esri:GraphicsLayer ID="MyGraphicsLayer">
					<esri:GraphicsLayer.MapTip>
						<Grid Background="LightYellow">
							<StackPanel Margin="5">
								<StackPanel Orientation="Horizontal">
									<TextBlock Text="{Binding Converter={StaticResource MyDictionaryConverter},
										ConverterParameter=NAME, Mode=OneWay}" FontWeight="Bold" />
									<TextBlock Text=" County, " FontWeight="Bold" />
									<TextBlock Text="{Binding Converter={StaticResource MyDictionaryConverter}, 
										ConverterParameter=STATE_NAME, Mode=OneWay}" FontWeight="Bold" />
								</StackPanel>
								<StackPanel Orientation="Horizontal">
									<TextBlock Text="Population (2007): " />
									<TextBlock Text="{Binding Converter={StaticResource MyDictionaryConverter}, 
										ConverterParameter=POP2007, Mode=OneWay}" />
								</StackPanel>
							</StackPanel>
							<Border BorderBrush="Black" BorderThickness="1" />
						</Grid>
					</esri:GraphicsLayer.MapTip>
				</esri:GraphicsLayer>
			</esri:Map.Layers>
		</esri:Map>

		<!-- FIND TASK INTERFACE -->
		<Canvas HorizontalAlignment="Right" VerticalAlignment="Top" Margin="0,15,7,0" Width="230" >
			<Rectangle Fill="#CC5C90B2" Stroke="Gray"  RadiusX="10" RadiusY="10" Width="210" Height="55" />
			<TextBlock Text="Find counties with names containing:" Foreground="White" FontSize="10" Margin="10,5,0,0" />
			<TextBox x:Name="FindTextBox" Width="150" Margin="15,22,0,0" Text="Wash" />
			<Button x:Name="FindButton" Content="Find" Margin="168,23,0,0" Click="FindButton_Click" />
		</Canvas>
	</Grid>
</UserControl>

[C#]

using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using ESRI.ArcGIS.Client;
using ESRI.ArcGIS.Client.Tasks;
using ESRI.ArcGIS.Client.Symbols;
using ESRI.ArcGIS.Client.ValueConverters;

namespace SilverlightApp
{
	public partial class Page : UserControl
	{
		public Page() { InitializeComponent(); }

		// Execute task when find button is clicked
		private void FindButton_Click(object sender, RoutedEventArgs e)
		{
			// Find task initialization
			FindTask findTask = new FindTask("http://sampleserver1.arcgisonline.com/ArcGIS/rest/services/" +
				"Demographics/ESRI_Census_USA/MapServer/");
			findTask.ExecuteCompleted += FindTask_ExecuteCompleted;
			findTask.Failed += FindTask_Failed;

			// Initialize find parameters. Specify NAME field in counties layer as search field 
			// and returning of feature geometry with find results.
			FindParameters findParameters = new FindParameters();
			findParameters.LayerIds.AddRange(new int[] { 3 });
			findParameters.SearchFields.AddRange(new string[] { "NAME" });
			findParameters.ReturnGeometry = true;

			// Use textbox text as value to search for.
			findParameters.SearchText = FindTextBox.Text;

			findTask.ExecuteAsync(findParameters);
		}

		// Draw results when find is complete
		private void FindTask_ExecuteCompleted(object sender, FindEventArgs args)
		{
			// Clear previous results
			GraphicsLayer graphicsLayer = MyMap.Layers["MyGraphicsLayer"] as GraphicsLayer;
			graphicsLayer.ClearGraphics();

			// Check for new results 
			if (args.FindResults.Count > 0)
			{
				// Add results to map
				foreach (FindResult result in args.FindResults)
				{
					result.Feature.Symbol = ResultsFillSymbol;
					graphicsLayer.Graphics.Add(result.Feature);
				}
			}
			else
			{
				MessageBox.Show("No features found");
			}
		}

		// Notify when find fails
		private void FindTask_Failed(object sender, TaskFailedEventArgs args)
		{
			MessageBox.Show("Find failed: " + args.Error);
		}
	}
}

How to create a Find task

The following steps assume you have created a Silverlight application with a map and a base layer as described in Creating a Map. The XAML view of your application's main page (e.g. Page.xaml) should look similar to the following:

<UserControl x:Class="SilverlightApp.Page"
	xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
	xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
	xmlns:esri="clr-namespace:ESRI.ArcGIS.Client;assembly=ESRI.ArcGIS.Client">

	<Grid x:Name="LayoutRoot" Background="White">

		<!-- MAP -->
		<esri:Map x:Name="MyMap" Extent="-125, 25, -65, 55" >
			<esri:Map.Layers>
				<esri:ArcGISTiledMapServiceLayer ID="StreetMapLayer" 
					Url="http://server.arcgisonline.com/ArcGIS/rest/services/ESRI_StreetMap_World_2D/MapServer"/>
			</esri:Map.Layers>
		</esri:Map>
	</Grid>
</UserControl>

The code in the main page's code-behind (e.g. Page.xaml.cs) should be unchanged from when you created your Silverlight application project in Visual Studio.

Creating an input interface for the Find task

Since tasks do not define user interfaces, you need to implement an interface for the Find task's input to allow your application's users to execute find operations. For this, the example simply includes a TextBox for defining the input value and a Button to execute the task.

  1. In XAML, define a Canvas to hold the task's input interface. Where possible, it is best to use a Canvas as the container element because Silverlight renders these most efficiently.
  2. <Canvas HorizontalAlignment="Right" VerticalAlignment="Top" Margin="0,15,7,0" Width="230" >
    </Canvas>

  3. Specify a Rectangle to use as the background for the input interface. This rectangle will be semi-transparent and have rounded corners.
  4. <Canvas HorizontalAlignment="Right" VerticalAlignment="Top" Margin="0,15,7,0" Width="230" >
    	<Rectangle Fill="#CC5C90B2" Stroke="Gray"  RadiusX="10" RadiusY="10" Width="210" Height="55" />
    </Canvas>

  5. Add a TextBlock to inform the user how to use the task.
  6. <Canvas HorizontalAlignment="Right" VerticalAlignment="Top" Margin="0,15,7,0" Width="230" >
    	<Rectangle Fill="#CC5C90B2" Stroke="Gray"  RadiusX="10" RadiusY="10" Width="210" Height="55" />
    	<TextBlock Text="Find counties with names containing:" Foreground="White" FontSize="10" Margin="10,5,0,0" />
    </Canvas>

  7. Define a TextBox for specifying the task's input value.
  8. <Canvas HorizontalAlignment="Right" VerticalAlignment="Top" Margin="0,15,7,0" Width="230" >
    	<Rectangle Fill="#CC5C90B2" Stroke="Gray"  RadiusX="10" RadiusY="10" Width="210" Height="55" />
    	<TextBlock Text="Find counties with names containing:" Foreground="White" FontSize="10" Margin="10,5,0,0" />
    	<TextBox x:Name="FindTextBox" Width="150" Margin="15,22,0,0" />
    </Canvas>

  9. Add a default value to the TextBox.
  10. <Canvas HorizontalAlignment="Right" VerticalAlignment="Top" Margin="0,15,7,0" Width="230" >
    	<Rectangle Fill="#CC5C90B2" Stroke="Gray"  RadiusX="10" RadiusY="10" Width="210" Height="55" />
    	<TextBlock Text="Find counties with names containing:" Foreground="White" FontSize="10" Margin="10,5,0,0" />
    	<TextBox x:Name="FindTextBox" Width="150" Margin="15,22,0,0" Text="Wash" />
    </Canvas>

  11. Add a Button to execute the task.
  12. <Canvas HorizontalAlignment="Right" VerticalAlignment="Top" Margin="0,15,7,0" Width="230" >
    	<Rectangle Fill="#CC5C90B2" Stroke="Gray"  RadiusX="10" RadiusY="10" Width="210" Height="55" />
    	<TextBlock Text="Find counties with names containing:" Foreground="White" FontSize="10" Margin="10,5,0,0" />
    	<TextBox x:Name="FindTextBox" Width="150" Margin="15,22,0,0" Text="Wash" />
    	<Button x:Name="FindButton" Content="Find" Margin="168,23,0,0" />
    </Canvas>

  13. Specify a handler for the Button's Click event. Later, you will implement this handler so that it executes the Find task.
  14. <Canvas HorizontalAlignment="Right" VerticalAlignment="Top" Margin="0,15,7,0" Width="230" >
    	<Rectangle Fill="#CC5C90B2" Stroke="Gray"  RadiusX="10" RadiusY="10" Width="210" Height="55" />
    	<TextBlock Text="Find counties with names containing:" Foreground="White" FontSize="10" Margin="10,5,0,0" />
    	<TextBox x:Name="FindTextBox" Width="150" Margin="15,22,0,0" Text="Wash" />
    	<Button x:Name="FindButton" Content="Find" Margin="168,23,0,0" Click="FindButton_Click" />
    </Canvas>

Creating an output interface for the Find task

To display the results of the Find task, you need to specify an output interface. For displaying the geometry of result features, you will define a GraphicsLayer within the Map element and a SimpleFillSymbol as a static resource. Then you will specify MapTips on the GraphicsLayer for displaying the features' attributes. The MapTips will use the DictionaryConverter class to enable binding to attributes on the Find task's results.

  1. Add two XML namespaces - one each to map to the ESRI.ArcGIS.Client.ValueConverters and ESRI.ArcGIS.Client.Symbols namespaces in the ESRI.ArcGIS.Client assembly.
  2. <UserControl x:Class="SilverlightApp.Page"
    	xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    	xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    	xmlns:esri="clr-namespace:ESRI.ArcGIS.Client;assembly=ESRI.ArcGIS.Client"
    	xmlns:esriConverters="clr-namespace:ESRI.ArcGIS.Client.ValueConverters;assembly=ESRI.ArcGIS.Client"
    	xmlns:esriSymbols="clr-namespace:ESRI.ArcGIS.Client.Symbols;assembly=ESRI.ArcGIS.Client">

  3. Specify a DictionaryConverter as a resource in your Silverlight application. The DictionaryConverter allows you specify binding to a particular Dictionary key.
  4. <Grid.Resources>
    	<esriConverters:DictionaryConverter x:Name="MyDictionaryConverter" />
    </Grid.Resources>

  5. Add a SimpleFillsymbol as a resource in your Silverlight application. The symbol specified here is semi-transparent with a red fill and outline. Later, you will apply this symbol to the task's results in the page's code-behind.
  6. <Grid.Resources>
    	<esriConverters:DictionaryConverter x:Name="MyDictionaryConverter" />
    	<esriSymbols:SimpleFillSymbol x:Name="ResultsFillSymbol" Fill="#64FF0000" BorderBrush="Red"
    		BorderThickness="2" />
    </Grid.Resources>

  7. Add a GraphicsLayer to the Map control XAML element. Note that the GraphicsLayer is specified below the map service layer in the XAML so that it is drawn above the map service layer at run time. For further information, refer to the Adding layers topic.
  8. <esri:Map x:Name="MyMap" Extent="-125, 25, -65, 55" >
    	<esri:Map.Layers>
    		<esri:ArcGISTiledMapServiceLayer ID="StreetMapLayer" 
    			Url="http://server.arcgisonline.com/ArcGIS/rest/services/ESRI_StreetMap_World_2D/MapServer"/>
    		<esri:GraphicsLayer ID="MyGraphicsLayer">
    		</esri:GraphicsLayer>
    	</esri:Map.Layers>
    </esri:Map>

  9. Insert a MapTip element within the GraphicsLayer. Inside the MapTip element, specify the background and border for the MapTip's content. For a simple rectangle with a border and margin around the MapTip text, you can use a StackPanel and Border nested inside a Grid as shown below. The background color will be that specified on the Grid, while the border color will be specified on the Border. With the container elements configured this way, the MapTip will automatically resize to fit its contents. In the following steps, you will configure the MapTip content to display the current county's name and state in bold and the county's population below its name.
  10. <esri:Map x:Name="MyMap" Extent="-125, 25, -65, 55" >
    	<esri:Map.Layers>
    		<esri:ArcGISTiledMapServiceLayer ID="StreetMapLayer" 
    			Url="http://server.arcgisonline.com/ArcGIS/rest/services/ESRI_StreetMap_World_2D/MapServer"/>
    		<esri:GraphicsLayer ID="MyGraphicsLayer">
    			<esri:GraphicsLayer.MapTip>
    				<Grid Background="LightYellow">
    					<StackPanel Margin="5">
    					</StackPanel>
    					<Border BorderBrush="Black" BorderThickness="1" />
    				</Grid>
    			</esri:GraphicsLayer.MapTip>
    		</esri:GraphicsLayer>
    	</esri:Map.Layers>
    </esri:Map>

  11. Add another StackPanel and specify a horizontal orientation. You will use this to format the county name and state.
  12. <esri:Map x:Name="MyMap" Extent="-125, 25, -65, 55" >
    	<esri:Map.Layers>
    		<esri:ArcGISTiledMapServiceLayer ID="StreetMapLayer" 
    			Url="http://server.arcgisonline.com/ArcGIS/rest/services/ESRI_StreetMap_World_2D/MapServer"/>
    		<esri:GraphicsLayer ID="MyGraphicsLayer">
    			<esri:GraphicsLayer.MapTip>
    				<Grid Background="LightYellow">
    					<StackPanel Margin="5">
    						<StackPanel Orientation="Horizontal">
    						</StackPanel>
    					</StackPanel>
    					<Border BorderBrush="Black" BorderThickness="1" />
    				</Grid>
    			</esri:GraphicsLayer.MapTip>
    		</esri:GraphicsLayer>
    	</esri:Map.Layers>
    </esri:Map>

  13. Add a TextBlock for the county name, specifying a font weight of bold.
  14. <esri:Map x:Name="MyMap" Extent="-125, 25, -65, 55" >
    	<esri:Map.Layers>
    		<esri:ArcGISTiledMapServiceLayer ID="StreetMapLayer" 
    			Url="http://server.arcgisonline.com/ArcGIS/rest/services/ESRI_StreetMap_World_2D/MapServer"/>
    		<esri:GraphicsLayer ID="MyGraphicsLayer">
    			<esri:GraphicsLayer.MapTip>
    				<Grid Background="LightYellow">
    					<StackPanel Margin="5">
    						<StackPanel Orientation="Horizontal">
    							<TextBlock FontWeight="Bold" />
    						</StackPanel>
    					</StackPanel>
    					<Border BorderBrush="Black" BorderThickness="1" />
    				</Grid>
    			</esri:GraphicsLayer.MapTip>
    		</esri:GraphicsLayer>
    	</esri:Map.Layers>
    </esri:Map>

  15. For the TextBlock text that will contain the county name, specify a data binding expression to bind to the graphic feature's NAME attribute. For this, use the DictionaryConverter resource you declared in step two. Within the MapTip element, the DataContext is the Attributes property of the current feature. This property is a Dictionary where the key is a field name and the value is the feature's attribute value for the field. Since the DictionaryConverter enables binding to a key within a Dictionary, and the DataContext within a MapTip element is a Dictionary, DictionaryConverter can be used within a MapTip element to specify data binding on field names.
  16. <esri:Map x:Name="MyMap" Extent="-125, 25, -65, 55" >
    	<esri:Map.Layers>
    		<esri:ArcGISTiledMapServiceLayer ID="StreetMapLayer" 
    			Url="http://server.arcgisonline.com/ArcGIS/rest/services/ESRI_StreetMap_World_2D/MapServer"/>
    		<esri:GraphicsLayer ID="MyGraphicsLayer">
    			<esri:GraphicsLayer.MapTip>
    				<Grid Background="LightYellow">
    					<StackPanel Margin="5">
    						<StackPanel Orientation="Horizontal">
    							<TextBlock Text="{Binding Converter={StaticResource MyDictionaryConverter},
    								ConverterParameter=NAME, Mode=OneWay}" FontWeight="Bold" />
    						</StackPanel>
    					</StackPanel>
    					<Border BorderBrush="Black" BorderThickness="1" />
    				</Grid>
    			</esri:GraphicsLayer.MapTip>
    		</esri:GraphicsLayer>
    	</esri:Map.Layers>
    </esri:Map>

  17. Add a TextBlock to append " County, " after the county name.
  18. <esri:Map x:Name="MyMap" Extent="-125, 25, -65, 55" >
    	<esri:Map.Layers>
    		<esri:ArcGISTiledMapServiceLayer ID="StreetMapLayer" 
    			Url="http://server.arcgisonline.com/ArcGIS/rest/services/ESRI_StreetMap_World_2D/MapServer"/>
    		<esri:GraphicsLayer ID="MyGraphicsLayer">
    			<esri:GraphicsLayer.MapTip>
    				<Grid Background="LightYellow">
    					<StackPanel Margin="5">
    						<StackPanel Orientation="Horizontal">
    							<TextBlock Text="{Binding Converter={StaticResource MyDictionaryConverter},
    								ConverterParameter=NAME, Mode=OneWay}" FontWeight="Bold" />
    							<TextBlock Text=" County, " FontWeight="Bold" />
    						</StackPanel>
    					</StackPanel>
    					<Border BorderBrush="Black" BorderThickness="1" />
    				</Grid>
    			</esri:GraphicsLayer.MapTip>
    		</esri:GraphicsLayer>
    	</esri:Map.Layers>
    </esri:Map>

  19. Add a TextBlock to display the state name. Use the DictionaryConverter to bind the TextBlock's text to the STATE_NAME field.
  20. <esri:Map x:Name="MyMap" Extent="-125, 25, -65, 55" >
    	<esri:Map.Layers>
    		<esri:ArcGISTiledMapServiceLayer ID="StreetMapLayer" 
    			Url="http://server.arcgisonline.com/ArcGIS/rest/services/ESRI_StreetMap_World_2D/MapServer"/>
    		<esri:GraphicsLayer ID="MyGraphicsLayer">
    			<esri:GraphicsLayer.MapTip>
    				<Grid Background="LightYellow">
    					<StackPanel Margin="5">
    						<StackPanel Orientation="Horizontal">
    							<TextBlock Text="{Binding Converter={StaticResource MyDictionaryConverter},
    								ConverterParameter=NAME, Mode=OneWay}" FontWeight="Bold" />
    							<TextBlock Text=" County, " FontWeight="Bold" />
    							<TextBlock Text="{Binding Converter={StaticResource MyDictionaryConverter}, 
    								ConverterParameter=STATE_NAME, Mode=OneWay}" FontWeight="Bold" />
    						</StackPanel>
    					</StackPanel>
    					<Border BorderBrush="Black" BorderThickness="1" />
    				</Grid>
    			</esri:GraphicsLayer.MapTip>
    		</esri:GraphicsLayer>
    	</esri:Map.Layers>
    </esri:Map>

  21. Add a StackPanel with a horizontal orientation to hold the population label and value.
  22. <esri:Map x:Name="MyMap" Extent="-125, 25, -65, 55" >
    	<esri:Map.Layers>
    		<esri:ArcGISTiledMapServiceLayer ID="StreetMapLayer" 
    			Url="http://server.arcgisonline.com/ArcGIS/rest/services/ESRI_StreetMap_World_2D/MapServer"/>
    		<esri:GraphicsLayer ID="MyGraphicsLayer">
    			<esri:GraphicsLayer.MapTip>
    				<Grid Background="LightYellow">
    					<StackPanel Margin="5">
    						<StackPanel Orientation="Horizontal">
    							<TextBlock Text="{Binding Converter={StaticResource MyDictionaryConverter},
    								ConverterParameter=NAME, Mode=OneWay}" FontWeight="Bold" />
    							<TextBlock Text=" County, " FontWeight="Bold" />
    							<TextBlock Text="{Binding Converter={StaticResource MyDictionaryConverter}, 
    								ConverterParameter=STATE_NAME, Mode=OneWay}" FontWeight="Bold" />
    						</StackPanel>
    						<StackPanel Orientation="Horizontal">
    						</StackPanel>
    					</StackPanel>
    					<Border BorderBrush="Black" BorderThickness="1" />
    				</Grid>
    			</esri:GraphicsLayer.MapTip>
    		</esri:GraphicsLayer>
    	</esri:Map.Layers>
    </esri:Map>

  23. Add a TextBlock for the population label
  24. <esri:Map x:Name="MyMap" Extent="-125, 25, -65, 55" >
    	<esri:Map.Layers>
    		<esri:ArcGISTiledMapServiceLayer ID="StreetMapLayer" 
    			Url="http://server.arcgisonline.com/ArcGIS/rest/services/ESRI_StreetMap_World_2D/MapServer"/>
    		<esri:GraphicsLayer ID="MyGraphicsLayer">
    			<esri:GraphicsLayer.MapTip>
    				<Grid Background="LightYellow">
    					<StackPanel Margin="5">
    						<StackPanel Orientation="Horizontal">
    							<TextBlock Text="{Binding Converter={StaticResource MyDictionaryConverter},
    								ConverterParameter=NAME, Mode=OneWay}" FontWeight="Bold" />
    							<TextBlock Text=" County, " FontWeight="Bold" />
    							<TextBlock Text="{Binding Converter={StaticResource MyDictionaryConverter}, 
    								ConverterParameter=STATE_NAME, Mode=OneWay}" FontWeight="Bold" />
    						</StackPanel>
    						<StackPanel Orientation="Horizontal">
    							<TextBlock Text="Population (2007): " />
    						</StackPanel>
    					</StackPanel>
    					<Border BorderBrush="Black" BorderThickness="1" />
    				</Grid>
    			</esri:GraphicsLayer.MapTip>
    		</esri:GraphicsLayer>
    	</esri:Map.Layers>
    </esri:Map>

  25. Add a TextBlock for the population value, using the DictionaryConverter to bind to the POP2007 field.
  26. <esri:Map x:Name="MyMap" Extent="-125, 25, -65, 55" >
    	<esri:Map.Layers>
    		<esri:ArcGISTiledMapServiceLayer ID="StreetMapLayer" 
    			Url="http://server.arcgisonline.com/ArcGIS/rest/services/ESRI_StreetMap_World_2D/MapServer"/>
    		<esri:GraphicsLayer ID="MyGraphicsLayer">
    			<esri:GraphicsLayer.MapTip>
    				<Grid Background="LightYellow">
    					<StackPanel Margin="5">
    						<StackPanel Orientation="Horizontal">
    							<TextBlock Text="{Binding Converter={StaticResource MyDictionaryConverter},
    								ConverterParameter=NAME, Mode=OneWay}" FontWeight="Bold" />
    							<TextBlock Text=" County, " FontWeight="Bold" />
    							<TextBlock Text="{Binding Converter={StaticResource MyDictionaryConverter}, 
    								ConverterParameter=STATE_NAME, Mode=OneWay}" FontWeight="Bold" />
    						</StackPanel>
    						<StackPanel Orientation="Horizontal">
    							<TextBlock Text="Population (2007): " />
    							<TextBlock Text="{Binding Converter={StaticResource MyDictionaryConverter}, 
    								ConverterParameter=POP2007, Mode=OneWay}" />
    						</StackPanel>
    					</StackPanel>
    					<Border BorderBrush="Black" BorderThickness="1" />
    				</Grid>
    			</esri:GraphicsLayer.MapTip>
    		</esri:GraphicsLayer>
    	</esri:Map.Layers>
    </esri:Map>

Implementing the Find task's execution logic

Now that you've specified the Find task's user interface, you need to define its execution logic. This execution logic can be divided into three parts:

You will implement these components in .NET code contained in the main page's code-behind. This code is linked to the XAML presentation layer by manipulating elements that you declared in XAML with "x:Name" or "ID" attributes and implementing methods that you declared in XAML as event handlers. The steps below assume that you are adding code to the Page class in the code-behind file for your Silverlight application's main page (e.g. Page.xaml.cs). In this example, C# is used.

Executing the task

To execute a Find task, you need to instantiate the task, specifying the map service that will be searched, wire the task's event handlers, initialize the task's search parameters, and call the task's execution method. The steps below will show you how to do this in the code-behind of your application's main page (e.g. Page.xaml.cs). The task is declared and initialized in the code-behind because tasks alone do not define any user interface, but rather encapsulate pieces of execution logic. In Silverlight, XAML is reserved for an application's presentation layer, while the code-behind is where business logic is implemented.

The code shown in these steps is written in C#.

  1. In the code-behind class of your application's main page, implement a handler for the FindButton's click event. Recall that you declared this handler when you defined the FindButton control in the page's XAML.
  2. private void FindButton_Click(object sender, RoutedEventArgs e)
    {
    }

  3. In the click handler, declare and instantiate a Find task. Set the map service layer that the task will search by passing the layer's URL to the Find task's constructor. To find the URL, you can use the ArcGIS Services Directory. See the Discovering Services topic for more information. This example uses the states layer of the ESRI_Census_USA service.
  4. private void FindButton_Click(object sender, RoutedEventArgs e)
    {
    	FindTask findTask = new FindTask("http://sampleserver1.arcgisonline.com/ArcGIS/rest/services/" +
    		"Demographics/ESRI_Census_USA/MapServer/");
    }

  5. Specify a handler for the task's ExecuteCompleted event. The method specified will be called when the Find task is done executing. You will implement this handler in the "Displaying results" section.
  6. private void FindButton_Click(object sender, RoutedEventArgs e)
    {
    	FindTask findTask = new FindTask("http://sampleserver1.arcgisonline.com/ArcGIS/rest/services/" +
    		"Demographics/ESRI_Census_USA/MapServer/");
    	findTask.ExecuteCompleted += FindTask_ExecuteCompleted;
    }

  7. Specify a handler for the task's Failed event, which fires when there is a problem executing the task. You will define this handler in the "Handling execution errors" section.
  8. private void FindButton_Click(object sender, RoutedEventArgs e)
    {
    	FindTask findTask = new FindTask("http://sampleserver1.arcgisonline.com/ArcGIS/rest/services/" +
    		"Demographics/ESRI_Census_USA/MapServer/");
    	findTask.ExecuteCompleted += FindTask_ExecuteCompleted;
    	findTask.Failed += FindTask_Failed;
    }

  9. Declare and instantiate a FindParameters object. The FindParameters object is used to define the execution parameters for Find tasks.
  10. private void FindButton_Click(object sender, RoutedEventArgs e)
    {
    	FindTask findTask = new FindTask("http://sampleserver1.arcgisonline.com/ArcGIS/rest/services/" +
    		"Demographics/ESRI_Census_USA/MapServer/");
    	findTask.ExecuteCompleted += FindTask_ExecuteCompleted;
    	findTask.Failed += FindTask_Failed;
    
    	FindParameters findParameters = new FindParameters();
    }

  11. On the FindParameters object, set the layer to be searched by adding a layer ID of 3 to the LayerIds property. This corresponds to the counties layer.
  12. private void FindButton_Click(object sender, RoutedEventArgs e)
    {
    	FindTask findTask = new FindTask("http://sampleserver1.arcgisonline.com/ArcGIS/rest/services/" +
    		"Demographics/ESRI_Census_USA/MapServer/");
    	findTask.ExecuteCompleted += FindTask_ExecuteCompleted;
    	findTask.Failed += FindTask_Failed;
    
    	FindParameters findParameters = new FindParameters();
    	findParameters.LayerIds.AddRange(new int[] { 3 });
    }

  13. Specify that the NAME field be searched in the find operation.
  14. private void FindButton_Click(object sender, RoutedEventArgs e)
    {
    	FindTask findTask = new FindTask("http://sampleserver1.arcgisonline.com/ArcGIS/rest/services/" +
    		"Demographics/ESRI_Census_USA/MapServer/");
    	findTask.ExecuteCompleted += FindTask_ExecuteCompleted;
    	findTask.Failed += FindTask_Failed;
    
    	FindParameters findParameters = new FindParameters();
    	findParameters.LayerIds.AddRange(new int[] { 3 });
    	findParameters.SearchFields.AddRange(new string[] { "NAME" });
    }

  15. Since you will draw the Find task's results on the map, specify that geometry be returned with the results.
  16. private void FindButton_Click(object sender, RoutedEventArgs e)
    {
    	FindTask findTask = new FindTask("http://sampleserver1.arcgisonline.com/ArcGIS/rest/services/" +
    		"Demographics/ESRI_Census_USA/MapServer/");
    	findTask.ExecuteCompleted += FindTask_ExecuteCompleted;
    	findTask.Failed += FindTask_Failed;
    
    	FindParameters findParameters = new FindParameters();
    	findParameters.LayerIds.AddRange(new int[] { 3 });
    	findParameters.SearchFields.AddRange(new string[] { "NAME" });
    	findParameters.ReturnGeometry = true;
    }

  17. Define the value to search for as the text in the FindTextBox control.
  18. private void FindButton_Click(object sender, RoutedEventArgs e)
    {
    	FindTask findTask = new FindTask("http://sampleserver1.arcgisonline.com/ArcGIS/rest/services/" +
    		"Demographics/ESRI_Census_USA/MapServer/");
    	findTask.ExecuteCompleted += FindTask_ExecuteCompleted;
    	findTask.Failed += FindTask_Failed;
    
    	FindParameters findParameters = new FindParameters();
    	findParameters.LayerIds.AddRange(new int[] { 3 });
    	findParameters.SearchFields.AddRange(new string[] { "NAME" });
    	findParameters.ReturnGeometry = true;
    
    	findParameters.SearchText = FindTextBox.Text;
    }

  19. Execute the find task with the paramters specified by the FindParameters member variable.
  20. private void FindButton_Click(object sender, RoutedEventArgs e)
    {
    	FindTask findTask = new FindTask("http://sampleserver1.arcgisonline.com/ArcGIS/rest/services/" +
    		"Demographics/ESRI_Census_USA/MapServer/");
    	findTask.ExecuteCompleted += FindTask_ExecuteCompleted;
    	findTask.Failed += FindTask_Failed;
    
    	FindParameters findParameters = new FindParameters();
    	findParameters.LayerIds.AddRange(new int[] { 3 });
    	findParameters.SearchFields.AddRange(new string[] { "NAME" });
    	findParameters.ReturnGeometry = true;
    
    	findParameters.SearchText = FindTextBox.Text;
    
    	findTask.ExecuteAsync(findParameters);
    }

Displaying results

  1. Declare a handler for the Find task's ExecuteCompleted event. This handler will be invoked when a find operation is complete. A list of FindResults containing information about the features with matching attributes is passed to the handler's args parameter. Each FindResult contains the feature found, the name and ID of the layer
  2. containing the feature, the name of the field containing the matching value, and other information.

    private void FindTask_ExecuteCompleted(object sender, FindEventArgs args)
    {
    }

  3. Get a reference to the results GraphicsLayer and clear any previously added graphics from it.
  4. private void FindTask_ExecuteCompleted(object sender, FindEventArgs args)
    {
    	GraphicsLayer graphicsLayer = MyMap.Layers["MyGraphicsLayer"] as GraphicsLayer;
    	graphicsLayer.ClearGraphics();
    }

  5. Check whether any results were found.
  6. private void FindTask_ExecuteCompleted(object sender, FindEventArgs args)
    {
    	GraphicsLayer graphicsLayer = MyMap.Layers["MyGraphicsLayer"] as GraphicsLayer;
    	graphicsLayer.ClearGraphics();
    
    	if (args.FindResults.Count > 0)
    	{
    	}
    	else
    	{
    	}
    }

  7. If results were found, loop through them. Apply the results fill symbol you declared in the page's XAML to each feature. Then add it to the results GraphicsLayer.
  8. private void FindTask_ExecuteCompleted(object sender, FindEventArgs args)
    {
    	GraphicsLayer graphicsLayer = MyMap.Layers["MyGraphicsLayer"] as GraphicsLayer;
    	graphicsLayer.ClearGraphics();
    
    	if (args.FindResults.Count > 0)
    	{
    		foreach (FindResult result in args.FindResults)
    		{
    			result.Feature.Symbol = ResultsFillSymbol;
    			graphicsLayer.Graphics.Add(result.Feature);
    		}
    	}
    	else
    	{
    	}
    }

  9. If no features were found, notify the user with a MessageBox.
  10. private void FindTask_ExecuteCompleted(object sender, FindEventArgs args)
    {
    	GraphicsLayer graphicsLayer = MyMap.Layers["MyGraphicsLayer"] as GraphicsLayer;
    	graphicsLayer.ClearGraphics();
    
    	if (args.FindResults.Count > 0)
    	{
    		foreach (FindResult result in args.FindResults)
    		{
    			result.Feature.Symbol = ResultsFillSymbol;
    			graphicsLayer.Graphics.Add(result.Feature);
    		}
    	}
    	else
    	{
    		MessageBox.Show("No features found");
    	}
    }

Handling execution errors

  1. Declare a handler for the Find task's Failed event. This handler will be invoked if there is a problem with executing a find operation.
  2. private void FindTask_Failed(object sender, TaskFailedEventArgs args)
    {
    }

  3. Notify the user of the problem with a MessageBox
  4. private void FindTask_Failed(object sender, TaskFailedEventArgs args)
    {
    	MessageBox.Show("Find failed: " + args.Error);
    }