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 WPF application. To use a Find task, you'll need to include code to define its user interface (UI) and specify its execution logic.

An example of XAML and .NET code (in this case C#) for a simple WPF 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 remainder of this topic walks you through how the Find task is defined in the example.

NoteNote:

The code sections in this topic require that the project has a refererence to the ESRI.ArcGIS.Client assembly. The Window of the application's main window (for example, MainWindow.xaml) needs to have the following XML namespace declaration:

xmlns:esri="http://schemas.esri.com/arcgis/client/2009"

The code-behind (for example, MainWindow.xaml.cs) must include the following using statements:

using ESRI.ArcGIS.Client;
using ESRI.ArcGIS.Client.Tasks;

The Grid shown in the following code section replaces the main Grid in the XAML.

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

  <!-- FIND TASK OUTPUT RESOURCES -->
  <Grid.Resources>
    <esri:SimpleFillSymbol x:Key="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 [NAME]}" FontWeight="Bold" />
                <TextBlock Text=" County, " FontWeight="Bold" />
                <TextBlock Text="{Binding [STATE_NAME]}" FontWeight="Bold" />
              </StackPanel>
              <StackPanel Orientation="Horizontal">
                <TextBlock Text="Population (2007): " />
                <TextBlock Text="{Binding [POP2007]}" />
              </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>

The methods shown in the following code section follow the constructor in the code-behind.

// Execute the task when the 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 the 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 searchable value.
  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 the results to the map.
    foreach (FindResult result in args.FindResults)
    {
      result.Feature.Symbol = LayoutRoot.Resources["ResultsFillSymbol"] as ESRI.ArcGIS.Client.Symbols.Symbol;
      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);
}

NoteNote:

Additional examples of the Find functionality can be found in the Interactive SDK.

Creating a Find task

The following steps assume you have created a WPF application with a map and a base layer as described in Creating a map. The XAML view of your application's main window (for example, MainWindow.xaml) should look similar to the following:

<Window x:Class="WPFApp.MainWindow"
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  Title="MainWindow" Height="350" Width="525">
  <Grid x:Name="LayoutRoot" >

    <!-- 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>
</Window>

The code in the main window's code-behind (for example, MainWindow.xaml.cs) should be unchanged from when you created your WPF application 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 users of your application to execute find operations. The following example 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's best to use a Canvas as the container element because WPF renders these most efficiently.
    <Canvas HorizontalAlignment="Right" VerticalAlignment="Top" Margin="0,15,7,0" Width="230" >
    </Canvas>
    
  2. Specify a Rectangle to use as the background for the input interface. This rectangle will be semi-transparent and have rounded corners.
    <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>
    
  3. Add a TextBlock to inform the user how to use the task.
    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>
    
  4. Define a TextBox for specifying the task's input value.
    <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>
    
  5. Add a default value to the TextBox.
    <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>
    
  6. Add a Button to execute the task.
    <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>
    
  7. Specify a handler for the Button's Click event. Later in this topic, you'll implement this handler so that it executes the Find task.
    <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 bind to attributes on the task's results.

  1. Add a SimpleFillSymbol as a resource in your WPF application. The symbol specified here is semi-transparent with a red fill and outline. Later in this topic, you'll apply this symbol to the task's results in the window's code-behind.
    <Grid.Resources>
      <esri:SimpleFillSymbol x:Key="ResultsFillSymbol" Fill="#64FF0000" BorderBrush="Red"
        BorderThickness="2" />
    </Grid.Resources>
    
  2. Add a GraphicsLayer to the Map control XAML element.
    NoteNote:

    The GraphicsLayer is specified below the map service layer in the XAML so that it's drawn above the map service layer at run time. For further information, see Adding layers.

    <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>
    
  3. Insert a MapTip element within the GraphicsLayer. In 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, use a StackPanel and Border nested inside a Grid as shown in the following code example. The background color is specified on the Grid, while the border color is 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'll configure the MapTip content to display the current county's name and state in bold and the county's population below its name.
    <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>
    
  4. Add another StackPanel and specify a horizontal orientation. You'll use this to format the county name and state.
    <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>
    
  5. Add a TextBlock for the county name, specifying a font weight of bold.
    <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>
    
  6. For the TextBlock text that will contain the county name, specify a data binding expression to bind to the graphic feature's NAME attribute. 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.
    <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 [NAME]}" FontWeight="Bold" />
                </StackPanel>
              </StackPanel>
              <Border BorderBrush="Black" BorderThickness="1" />
            </Grid>
          </esri:GraphicsLayer.MapTip>
        </esri:GraphicsLayer>
      </esri:Map.Layers>
    </esri:Map>
    
  7. Add a TextBlock to append " County, " after the county name.
    <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 [NAME]}" 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>
    
  8. Add a TextBlock to display the state name. Bind the TextBlock's text to the STATE_NAME field.
    <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 [NAME]}" FontWeight="Bold" />
                  <TextBlock Text=" County, " FontWeight="Bold" />
                  <TextBlock Text="Binding [STATE_NAME]" FontWeight="Bold" />
                </StackPanel>
              </StackPanel>
              <Border BorderBrush="Black" BorderThickness="1" />
            </Grid>
          </esri:GraphicsLayer.MapTip>
        </esri:GraphicsLayer>
      </esri:Map.Layers>
    </esri:Map>
    
  9. Add a StackPanel with a horizontal orientation to hold the population label and value.
    <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 [NAME]}" FontWeight="Bold" />
                  <TextBlock Text=" County, " FontWeight="Bold" />
                  <TextBlock Text="Binding [STATE_NAME]" FontWeight="Bold" />
                </StackPanel>
                <StackPanel Orientation="Horizontal">
                </StackPanel>
              </StackPanel>
              <Border BorderBrush="Black" BorderThickness="1" />
            </Grid>
          </esri:GraphicsLayer.MapTip>
        </esri:GraphicsLayer>
      </esri:Map.Layers>
    </esri:Map>
    
  10. Add a TextBlock for the population label.
    <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 [NAME]}" FontWeight="Bold" />
                  <TextBlock Text=" County, " FontWeight="Bold" />
                  <TextBlock Text="Binding [STATE_NAME]" 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>
    
  11. Add a TextBlock for the population value, binding to the POP2007 field.
    <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 [NAME]}" FontWeight="Bold" />
                  <TextBlock Text=" County, " FontWeight="Bold" />
                  <TextBlock Text="Binding [STATE_NAME]" FontWeight="Bold" />
                </StackPanel>
                <StackPanel Orientation="Horizontal">
                  <TextBlock Text="Population (2007): " />
                  <TextBlock Text="{Binding [POP2007]}" />
                </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 UI, you need to define its execution logic. The execution logic can be divided into the following three parts:

  • Task execution
  • Task results display
  • Execution error handling

You'll implement these components in .NET code contained in the main window'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 following steps assume that you are adding code to the Window class in the code-behind file for your WPF application's main window (for example, MainWindow.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, then wire the task's event handlers, initialize the task's search parameters, and call the task's execution method. The following steps show you how to do this in the code-behind of your application's main window (for example, MainWindow.xaml.cs). The task is declared and initialized in the code-behind because tasks alone do not define any UI, but rather encapsulate pieces of execution logic. In WPF, 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 window, implement a handler for the FindButton's click event. You declared this handler when you defined the FindButton control in the window's XAML.
    private void FindButton_Click(object sender, RoutedEventArgs e)
    {
    }
    
  2. 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.
    private void FindButton_Click(object sender, RoutedEventArgs e)
    {
      FindTask findTask = new FindTask("http://sampleserver1.arcgisonline.com/ArcGIS/rest/services/" +
        "Demographics/ESRI_Census_USA/MapServer/");
    }
    
  3. Specify a handler for the task's ExecuteCompleted event. The method specified will be called when the Find task is done executing. You'll implement this handler in the Displaying results section.
    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;
    }
    
  4. Specify a handler for the task's Failed event, which fires when there is a problem executing the task. You'll define this handler in the Handling execution errors section.
    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;
    }
    
  5. Declare and instantiate a FindParameters object. The FindParameters object is used to define the execution parameters for Find tasks.
    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();
    }
    
  6. 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.
    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 });
    }
    
  7. Specify that the NAME field be searched in the find operation.
    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" });
    }
    
  8. Since you'll draw the Find task's results on the map, specify that geometry be returned with the results.
    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;
    }
    
  9. Define the value to search for as the text in the FindTextBox control.
    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;
    }
    
  10. Execute the Find task with the parameters specified by the FindParameters member variable.
    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 containing the feature, the name of the field containing the matching value, and other information.
    private void FindTask_ExecuteCompleted(object sender, FindEventArgs args)
    {
    }
    
  2. Get a reference to the results GraphicsLayer, and clear any previously added graphics from it.
    private void FindTask_ExecuteCompleted(object sender, FindEventArgs args)
    {
      GraphicsLayer graphicsLayer = MyMap.Layers["MyGraphicsLayer"] as GraphicsLayer;
      graphicsLayer.ClearGraphics();
    }
    
  3. Check whether any results were found.
    private void FindTask_ExecuteCompleted(object sender, FindEventArgs args)
    {
      GraphicsLayer graphicsLayer = MyMap.Layers["MyGraphicsLayer"] as GraphicsLayer;
      graphicsLayer.ClearGraphics();
    
      if (args.FindResults.Count > 0)
      {
      }
      else
      {
      }
    }
    
  4. If results were found, loop through them. Apply the results fill symbol you declared in the window's's XAML to each feature, then add it to the results GraphicsLayer.
    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 = LayoutRoot.Resources["ResultsFillSymbol"] as ESRI.ArcGIS.Client.Symbols.Symbol;
          graphicsLayer.Graphics.Add(result.Feature);
        }
      }
      else
      {
      }
    }
    
  5. If no features were found, notify the user with a MessageBox.
    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 = LayoutRoot.Resources["ResultsFillSymbol"] as ESRI.ArcGIS.Client.Symbols.Symbol;
          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.
    private void FindTask_Failed(object sender, TaskFailedEventArgs args)
    {
    }
    
  2. Notify the user of the problem with a MessageBox.
    private void FindTask_Failed(object sender, TaskFailedEventArgs args)
    {
      MessageBox.Show("Find failed: " + args.Error);
    }
    
1/23/2012