This sample shows how to perform a point-based identify style operation on an HydrographicS57Layer using the HitTestAsync method. Note that HitTest operations take screen coordinates as opposed other query/search operations which typically take a spatial geometry object.
<UserControlx:Class="ArcGISWPFSDK.S57Identify"xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"xmlns:esri="http://schemas.esri.com/arcgis/client/2009"xmlns:local="clr-namespace:ArcGISWPFSDK"><Grid><Grid.Resources><StyleTargetType="ListBoxItem"><SetterProperty="Template"><Setter.Value><ControlTemplateTargetType="ListBoxItem"><Borderx:Name="ItemBorder"BorderBrush="Black"Background="LightGray"BorderThickness="2"Margin="3"><ContentPresenterMargin="2"/></Border><ControlTemplate.Triggers><TriggerProperty="IsMouseOver"Value="True"><SetterTargetName="ItemBorder"Property="BorderBrush"Value="Blue"/></Trigger><TriggerProperty="IsSelected"Value="True"><SetterTargetName="ItemBorder"Property="BorderBrush"Value="Red"/></Trigger><MultiTrigger><MultiTrigger.Conditions><ConditionProperty="IsMouseOver"Value="False"/><ConditionProperty="IsSelected"Value="False"/></MultiTrigger.Conditions><SetterTargetName="ItemBorder"Property="Opacity"Value="0.50"/></MultiTrigger></ControlTemplate.Triggers></ControlTemplate></Setter.Value></Setter></Style></Grid.Resources><Grid><esri:Mapx:Name="MyMap"UseAcceleratedDisplay="True"Extent="-15090086, 4141039, -13113925 , 5862778"WrapAround="True"MinimumResolution="5"><esri:ArcGISTiledMapServiceLayerID="Ocean_BaseMap"Url="http://services.arcgisonline.com/ArcGIS/rest/services/Ocean_Basemap/MapServer"/><esri:HydrographicS57Layerx:Name="S57Layer"ID="S57"Initialized="S57Layer_Initialized"><esri:S57CellPath="..\Data\s57_electronic_navigational_charts\US1WC01M\US1WC01M.000"/><esri:S57CellPath="..\Data\s57_electronic_navigational_charts\US1WC07M\US1WC07M.000"/></esri:HydrographicS57Layer><esri:GraphicsLayerx:Name="_identifyResults"ID="Identify Results"Opacity="0.5"/></esri:Map><GridWidth="300"Height="550"Margin="5,5,0,0"VerticalAlignment="Top"HorizontalAlignment="Left"><Grid.Resources><DataTemplatex:Key="identifyResults"><StackPanelOrientation="Vertical"Margin="5,5"><StackPanelOrientation="Horizontal"><TextBlockText="Acronym: "/><TextBlockText="{Binding Path=Acronym}"/></StackPanel><StackPanelOrientation="Horizontal"><TextBlockText="Long Name: "/><TextBlockText="{Binding Path=LongName}"/></StackPanel><StackPanelOrientation="Horizontal"><TextBlockText="Cell Name: "/><TextBlockText="{Binding Path=CellName}"/></StackPanel><StackPanelOrientation="Horizontal"><TextBlockText="Composition Scale: "/><TextBlockText="{Binding Path=CompositionScale}"/></StackPanel></StackPanel></DataTemplate></Grid.Resources><RectangleHeight="550"Grid.RowSpan="6"VerticalAlignment="Top"Fill="White"Stroke="Gray"><Rectangle.Effect><DropShadowEffect/></Rectangle.Effect></Rectangle><RectangleGrid.RowSpan="6"Margin="5"Fill="#DDFFFFFF"Stroke="DarkGray"/><StackPanel><TextBlockMargin="10,10,5,0">Click on the map to perform an Identify operation.</TextBlock><StackPanelOrientation="Horizontal"><TextBlockMargin="10,5,5,0">Found:</TextBlock><TextBlockMargin="5,5,5,0"x:Name="IdentifyResultsText"/></StackPanel><ListBoxx:Name="ResultItems"ItemTemplate="{StaticResource identifyResults}"SelectionChanged="ResultItems_SelectionChanged"Width="280"Height="350"HorizontalContentAlignment="Stretch"Margin="10,5,10,10"/><ScrollViewerHorizontalScrollBarVisibility="Auto"VerticalScrollBarVisibility="Auto"Width="280"Height="125"><DataGridx:Name="ResultAttributes"AutoGenerateColumns="True"HeadersVisibility="All"Background="White"></DataGrid></ScrollViewer></StackPanel></Grid></Grid></Grid></UserControl>
using System;
using System.Windows.Controls;
using ESRI.ArcGIS.Client.AdvancedSymbology;
using ESRI.ArcGIS.Client;
using ESRI.ArcGIS.Client.Geometry;
using ESRI.ArcGIS.Client.Symbols;
using System.Windows.Data;
using System.Collections.Generic;
namespace ArcGISWPFSDK
{
publicpartialclass S57Identify : UserControl
{
public S57Identify()
{
InitializeComponent();
}
// Handle the MouseClick event on the Mapprivatevoid MyMap_MouseClick(object sender, ESRI.ArcGIS.Client.Map.MouseEventArgs e)
{
// Call the HitTestAsync method passing in the screenpoint, the maximum number of results, a callback delegate, and pixel tolerance
S57Layer.HitTestAsync(e.ScreenPoint, 10,(results) =>
{
// Set the ItemSource property of the ListBox to the returned IList<S57FeatureObject> result set
ResultItems.ItemsSource = results;
// Convert the MapPoint from the MouseEventArgs to a formatted string in the Decimal Degree notationvar locString = CoordinateConversion.MapPointToDecimalDegrees(e.MapPoint, 3);
// Set the Identify results text
IdentifyResultsText.Text = string.Format("{1} item{2} at {0}", locString, results.Count, results.Count > 1 ? "s" : "");
},3);
}
// Handle the ListBox item selection changed eventprivatevoid ResultItems_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
// Get the selected ListBoxItem and cast to S57FeatureObjectvar item = ResultItems.SelectedItem as S57FeatureObject;
// Clear the results graphics
_identifyResults.Graphics.Clear();
// Generate a graphic with appropriate symbology from the S57FeatureObject geometryif (item != null)
_identifyResults.Graphics.Add(MakeGraphicFromGeom(item.Geometry));
// Display the attributes in the attributes DataGrid.int index = ResultItems.SelectedIndex;
if (index > -1)
{
/*
* Note:- S57FeatureObject.Attributes property has been deprecated. Instead use S57FeatureObject.AttributeItems.
*///ResultAttributes.ItemsSource = item.Attributes;
ResultAttributes.ItemsSource = item.AttributeItems;
}
else
ResultAttributes.ItemsSource = null;
}
// Utility function to generate a graphic, with symbol, from a Geometry object.private Graphic MakeGraphicFromGeom(ESRI.ArcGIS.Client.Geometry.Geometry geom)
{
if (geom == null)
returnnull;
Graphic graphic = new Graphic();
graphic.Geometry = geom;
// Check geometry type and create appropriate Simple...Symbol.if (geom is MapPoint || geom is MultiPoint)
graphic.Symbol = new SimpleMarkerSymbol() { Color = System.Windows.Media.Brushes.Red, Size = 20 };
elseif (geom is ESRI.ArcGIS.Client.Geometry.Polyline)
graphic.Symbol = new SimpleLineSymbol() { Color = System.Windows.Media.Brushes.Red, Width = 6 };
elseif (geom is ESRI.ArcGIS.Client.Geometry.Polygon || geom is Envelope)
graphic.Symbol = new SimpleFillSymbol() { Fill = System.Windows.Media.Brushes.Red };
return graphic;
}
// Event fired when the privatevoid S57Layer_Initialized(object sender, EventArgs e)
{
// Check if an error was encountered on initialization.if (S57Layer.InitializationFailure != null)
return;
// Register the Map MouseClick when the layer has initialized.
MyMap.MouseClick += MyMap_MouseClick;
}
}
}
Imports System.Windows.Controls
Imports ESRI.ArcGIS.Client.AdvancedSymbology
Imports ESRI.ArcGIS.Client
Imports ESRI.ArcGIS.Client.Geometry
Imports ESRI.ArcGIS.Client.Symbols
Namespace ArcGISWPFSDK
PartialPublicClass S57Identify
Inherits UserControl
PublicSubNew()
InitializeComponent()
EndSub' Handle the MouseClick event on the MapPrivateSub MyMap_MouseClick(sender AsObject, e As ESRI.ArcGIS.Client.Map.MouseEventArgs)
' Call the HitTestAsync method passing in the screenpoint, the maximum number of results, a callback delegate, and pixel tolerance
S57Layer.HitTestAsync(e.ScreenPoint, 10, _
Function(results)
' Set the ItemSource property of the ListBox to the returned IList<S57FeatureObject> result set
ResultItems.ItemsSource = results
' Convert the MapPoint from the MouseEventArgs to a formatted string in the Decimal Degree notationDim locString = CoordinateConversion.MapPointToDecimalDegrees(e.MapPoint, 3)
' Set the Identify results text
IdentifyResultsText.Text = String.Format("{1} item{2} at {0}", locString, results.Count, If(results.Count > 1, "s", ""))
EndFunction, 3)
EndSub' Handle the ListBox item selection changed eventPrivateSub ResultItems_SelectionChanged(sender AsObject, e As SelectionChangedEventArgs)
' Get the selected ListBoxItem and cast to S57FeatureObjectDim item = TryCast(ResultItems.SelectedItem, S57FeatureObject)
' Clear the results graphics
_identifyResults.Graphics.Clear()
' Generate a graphic with appropriate symbology from the S57FeatureObject geometryIf item IsNotNothingThen
_identifyResults.Graphics.Add(MakeGraphicFromGeom(item.Geometry))
EndIf' Display the attributes in the attributes DataGrid.Dim index AsInteger = ResultItems.SelectedIndex
If index > -1 Then
ResultAttributes.ItemsSource = item.Attributes
Else
ResultAttributes.ItemsSource = NothingEndIfEndSub' Utility function to generate a graphic, with symbol, from a Geometry object.PrivateFunction MakeGraphicFromGeom(geom As ESRI.ArcGIS.Client.Geometry.Geometry) As Graphic
If geom IsNothingThenReturnNothingEndIfDim graphic AsNew Graphic()
graphic.Geometry = geom
' Check geometry type and create appropriate Simple...Symbol.IfTypeOf geom Is MapPoint OrElseTypeOf geom Is MultiPoint Then
graphic.Symbol = New SimpleMarkerSymbol() With { _
.Color = System.Windows.Media.Brushes.Red, _
.Size = 20 _
}
ElseIfTypeOf geom Is ESRI.ArcGIS.Client.Geometry.Polyline Then
graphic.Symbol = New SimpleLineSymbol() With { _
.Color = System.Windows.Media.Brushes.Red, _
.Width = 6 _
}
ElseIfTypeOf geom Is ESRI.ArcGIS.Client.Geometry.Polygon OrElseTypeOf geom Is Envelope Then
graphic.Symbol = New SimpleFillSymbol() With { _
.Fill = System.Windows.Media.Brushes.Red _
}
EndIfReturn graphic
EndFunction' Event fired when the PrivateSub S57Layer_Initialized(sender AsObject, e As EventArgs)
' Check if an error was encountered on initialization.If S57Layer.InitializationFailure IsNotNothingThenReturnEndIf' Register the Map MouseClick when the layer has initialized.AddHandler MyMap.MouseClick, AddressOf MyMap_MouseClick
EndSubEndClassEndNamespace