Web Mapping for Silverlight:拉框查询,并将结果输出到表格中: 1.xaml文件代码如下: <UserControl x:Class="SampleSL.MainPage" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:esri="clr-namespace:ESRI.ArcGIS.Client;assembly=ESRI.ArcGIS.Client" xmlns:esriSymbols="clr-namespace:ESRI.ArcGIS.Client.Symbols;assembly=ESRI.ArcGIS.Client" xmlns:esriToolkit="clr-namespace:ESRI.ArcGIS.Client.Toolkit;assembly=ESRI.ArcGIS.Client.Toolkit" xmlns:esriGeometry="clr-namespace:ESRI.ArcGIS.Client.Geometry;assembly=ESRI.ArcGIS.Client" xmlns:slData="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Data" xmlns:vsm="clr-namespace:System.Windows;assembly=System.Windows" mc:Ignorable="d" xmlns:controls="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls" > <Grid x:Name="LayoutRoot"> <Grid.Resources> <esriSymbols:SimpleLineSymbol x:Name="DrawLineSymbol" Color="Green" Width="2" /> <esriSymbols:SimpleFillSymbol x:Name="DrawFillSymbol" Fill="#3300FFFF" BorderBrush="Cyan" BorderThickness="2" /> <esriSymbols:SimpleFillSymbol x:Name="MeasureDrawFillSymbol" Fill="#00E0FFFF" BorderBrush="Green" BorderThickness="2" /> <esriSymbols:SimpleMarkerSymbol x:Name="DefaultMarkerSymbol" Color="Red" Size="12" Style="Circle" mce_Style="Circle" /> <esriSymbols:SimpleLineSymbol x:Name="DefaultLineSymbol" Color="Red" Width="4" /> <esriSymbols:PictureMarkerSymbol x:Name="DefaultClickSymbol" OffsetX="11" OffsetY="39" Source="/Assets/images/Pushpin.png" /> <esriSymbols:PictureMarkerSymbol x:Name="DefaultMaptipSymbol" OffsetX="5" OffsetY="20" Source="/Assets/images/Star.png" /> <esriSymbols:SimpleFillSymbol x:Name="DefaultFillSymbol" Fill="#660000FF" BorderBrush="Cyan" BorderThickness="2" > <esriSymbols:SimpleFillSymbol.ControlTemplate> <ControlTemplate> <Path x:Name="Element" IsHitTestVisible="False" Fill="{Binding Symbol.Fill}" Stroke="{Binding Symbol.BorderBrush}" StrokeThickness="{Binding Symbol.BorderThickness}"/> </ControlTemplate> </esriSymbols:SimpleFillSymbol.ControlTemplate> </esriSymbols:SimpleFillSymbol> <esriSymbols:SimpleFillSymbol x:Name="ResultsFillSymbol" Fill="#880000FF" BorderBrush="Blue" BorderThickness="2"> <esriSymbols:SimpleFillSymbol.ControlTemplate> <ControlTemplate x:Name="CustomPolygonTemplate"> <Grid> <vsm:VisualStateManager.VisualStateGroups> <vsm:VisualStateGroup x:Name="CommonStates"> <vsm:VisualState x:Name="Normal"> <Storyboard> <ColorAnimation Storyboard.TargetName="Element" Storyboard.TargetProperty="(Fill).(Color)" To="#880000FF" Duration="0:0:0.1" /> </Storyboard> </vsm:VisualState> <vsm:VisualState x:Name="MouseOver"> <Storyboard> <ColorAnimation Storyboard.TargetName="Element" Storyboard.TargetProperty="(Fill).(Color)" To="#8800FFFF" Duration="0:0:0.1" /> </Storyboard> </vsm:VisualState> </vsm:VisualStateGroup> </vsm:VisualStateManager.VisualStateGroups> <Path x:Name="Element" Stroke="{Binding Symbol.BorderBrush}" StrokeStartLineCap="Round" StrokeThickness="{Binding Symbol.BorderThickness}" StrokeLineJoin="Round" StrokeEndLineCap="Round" Fill="{Binding Symbol.Fill}" /> </Grid> </ControlTemplate> </esriSymbols:SimpleFillSymbol.ControlTemplate> </esriSymbols:SimpleFillSymbol> <Style x:Key="SortAscending" xmlns:primitives="clr-namespace:System.Windows.Controls.Primitives;assembly=System.Windows.Controls.Data" xmlns:vsm="clr-namespace:System.Windows;assembly=System.Windows" TargetType="primitives:DataGridColumnHeader"> <Setter Property="Template"> <Setter.Value> <ControlTemplate x:Name="sp" > <StackPanel x:Name="spasc" Orientation="Horizontal" Background="WhiteSmoke" Width="auto" > <TextBlock x:Name="txtasc" Loaded="SetColumnHeader" Width="auto" /> <Image x:Name="imgasc" Source="/Assets/images/Collapse.png" HorizontalAlignment="Right" /> </StackPanel> </ControlTemplate> </Setter.Value> </Setter> </Style> <Style x:Key="SortDescending" xmlns:primitives="clr-namespace:System.Windows.Controls.Primitives;assembly=System.Windows.Controls.Data" xmlns:vsm="clr-namespace:System.Windows;assembly=System.Windows" TargetType="primitives:DataGridColumnHeader"> <Setter Property="Template"> <Setter.Value> <ControlTemplate> <StackPanel x:Name="spdes" Orientation="Horizontal" Background="WhiteSmoke" Width="auto"> <TextBlock x:Name="txtDes" Loaded="SetColumnHeader" Width="auto" /> <Image x:Name="imgdes" Source="/Assets/images/Down.png" HorizontalAlignment="Right" /> </StackPanel> </ControlTemplate> </Setter.Value> </Setter> </Style> </Grid.Resources> <StackPanel x:Name="MainPanel" Orientation="vertical" Grid.ColumnSpan="2" > <esri:Map x:Name="slMap" IsLogoVisible="False" Height="400" HorizontalAlignment="Left" Width="900" Margin="0,0,0,0" Background="{x:Null}" Foreground="Black" > <esri:ArcGISTiledMapServiceLayer ID="StreetMapLayer" Url="http://pc1010/ArcGIS/rest/services/world/MapServer"/> <esri:ArcGISDynamicMapServiceLayer ID="DynamicLayer" Opacity="0.6" Url="http://pc1010/ArcGIS/rest/services/ChinaMapService/MapServer"/> </esri:Map> <Border Opacity="0.3" Background="Black" > <TextBlock x:Name="tbDataRuler" FontSize="9" FontWeight="Bold" Margin="0,0,8,0" Height="2" VerticalAlignment="Top" Foreground="WhiteSmoke" /> </Border> <StackPanel x:Name="stPanelData" Orientation="Vertical" Background="WhiteSmoke" > <StackPanel x:Name="stPanelDataExport" Orientation="Horizontal" VerticalAlignment="Center" Background="WhiteSmoke" Height="22" > <TextBlock x:Name="tbDisplayTitle" Text="Search Results" Foreground="Black" FontWeight="Bold" HorizontalAlignment="Left" Margin="0,0,0,0"/> </StackPanel> <Border Opacity="0.3" Background="Black" > <TextBlock x:Name="tbMapRuler" FontSize="9" FontWeight="Bold" Margin="0,0,8,0" Height="2" VerticalAlignment="Top" Foreground="WhiteSmoke" /> </Border> <StackPanel x:Name="stPanelButtons" Orientation="Horizontal" VerticalAlignment="Center" Background="WhiteSmoke" Height="22" HorizontalAlignment="right" > <TextBlock x:Name="txtRecordsDetails" Width="auto" HorizontalAlignment="Right" Margin="0,0,10,0"></TextBlock> <slData:DataPager x:Name="slPager" Source="{Binding ItemsSource, ElementName=grdQueryDetails}" PageSize="10" Foreground="Black" BorderThickness="0" NumericButtonCount="10" Background="WhiteSmoke" Visibility="Collapsed" DisplayMode="Numeric" Width="200" Margin="0,0,30,0" /> </StackPanel> <StackPanel x:Name="stPanelDataGrid" Orientation="Vertical" VerticalAlignment="Top" Background="WhiteSmoke" HorizontalAlignment="Stretch" > <ScrollViewer x:Name="scrollData" HorizontalScrollBarVisibility="Visible" VerticalScrollBarVisibility="Auto" Canvas.Left="10" Canvas.Top="10" Width="auto" Background="White" Foreground="#FFEAE2E2" Height="210" > <slData:DataGrid x:Name="grdQueryDetails" AutoGenerateColumns="False" HeadersVisibility="Column" CanUserReorderColumns="False" Background="White" CanUserSortColumns="True" Foreground="Black" MouseLeftButtonDown="grdQueryDetails_MouseLeftButtonDown" > </slData:DataGrid> </ScrollViewer> </StackPanel> </StackPanel> </StackPanel> <!-- Toolbar--> <Canvas x:Name="canvasToolbar" Height="50" VerticalAlignment="Top" HorizontalAlignment="Left" Margin="175,0,20,0"> <Grid Height="50" HorizontalAlignment="Right" VerticalAlignment="Top" Canvas.Top="20" Canvas.Left="64" Width="300" > <Rectangle Fill="#775C90B2" Stroke="Gray" RadiusX="10" RadiusY="10" Margin="-33,8,0,0" /> <Rectangle Fill="#66FFFFFF" Stroke="DarkGray" RadiusX="5" RadiusY="5" Margin="-28,16,8,9" /> <StackPanel Orientation="Vertical" VerticalAlignment="Top" Margin="-38,2,38,0" > <esriToolkit:Toolbar ToolbarItemClickEffect="Bounce" x:Name="MyToolbar" HorizontalAlignment="Left" MaxItemHeight="40" MaxItemWidth="40" Grid.Row="0" VerticalAlignment="Top" Height="40" Margin="15,0,-3,0" > <esriToolkit:Toolbar.Items> <esriToolkit:ToolbarItemCollection> <esriToolkit:ToolbarItem Text="Zoom In"> <esriToolkit:ToolbarItem.Content> <Image Source="/Assets/images/Zoomin.png" Stretch="UniformToFill" Margin="5,0,5,2" ToolTipService.ToolTip="Zoom In" VerticalAlignment="Bottom" /> </esriToolkit:ToolbarItem.Content> </esriToolkit:ToolbarItem> <esriToolkit:ToolbarItem Text="Zoom Out"> <esriToolkit:ToolbarItem.Content> <Image Source="/Assets/images/Zoomout.png" Stretch="UniformToFill" Margin="5,0,5,2" ToolTipService.ToolTip="Zoom Out" /> </esriToolkit:ToolbarItem.Content> </esriToolkit:ToolbarItem> <esriToolkit:ToolbarItem Text="Pan"> <esriToolkit:ToolbarItem.Content> <Image Source="/Assets/images/Pan.png" Stretch="UniformToFill" Margin="5,0,5,2" ToolTipService.ToolTip="Pan" /> </esriToolkit:ToolbarItem.Content> </esriToolkit:ToolbarItem> <esriToolkit:ToolbarItem Text="Previous Extent" > <esriToolkit:ToolbarItem.Content> <Image Source="/Assets/images/Previous.png" IsHitTestVisible="False" Opacity="0.3" Stretch="UniformToFill" Margin="5,0,5,2" ToolTipService.ToolTip="Back Extent"/> </esriToolkit:ToolbarItem.Content> </esriToolkit:ToolbarItem> <esriToolkit:ToolbarItem Text="Next Extent"> <esriToolkit:ToolbarItem.Content> <Image Source="/Assets/images/Next.png" IsHitTestVisible="False" Opacity="0.3" Stretch="UniformToFill" Margin="5,0,5,2" ToolTipService.ToolTip="Forward Extent"/> </esriToolkit:ToolbarItem.Content> </esriToolkit:ToolbarItem> <esriToolkit:ToolbarItem Text="Full Extent"> <esriToolkit:ToolbarItem.Content> <Image Source="/Assets/images/Globe.png" Stretch="UniformToFill" Margin="5,0,5,2" ToolTipService.ToolTip="Full Extent" /> </esriToolkit:ToolbarItem.Content> </esriToolkit:ToolbarItem> <esriToolkit:ToolbarItem Text="Clear Graphics"> <esriToolkit:ToolbarItem.Content> <Image Source="/Assets/images/DeleteGraphics.png" Stretch="UniformToFill" Margin="5,0,5,2" ToolTipService.ToolTip="Clear Graphics" /> </esriToolkit:ToolbarItem.Content> </esriToolkit:ToolbarItem> <esriToolkit:ToolbarItem Text="Identify"> <esriToolkit:ToolbarItem.Content> <Image Source="/Assets/images/Info.png" Stretch="UniformToFill" Margin="5,0,5,2" OpacityMask="Black" ToolTipService.ToolTip="Identify" Height="22" Width="22" /> </esriToolkit:ToolbarItem.Content> </esriToolkit:ToolbarItem> </esriToolkit:ToolbarItemCollection> </esriToolkit:Toolbar.Items> </esriToolkit:Toolbar> </StackPanel> </Grid> </Canvas> </Grid> </UserControl> 2.后台C#代码如下: using System; using System.Windows; using System.Windows.Controls; using System.Windows.Data; using System.Windows.Media; using ESRI.ArcGIS.Client; using ESRI.ArcGIS.Client.Symbols; using ESRI.ArcGIS.Client.Tasks; using System.Collections.Generic; namespace SampleSL { /// <summary> /// This class handles the identify operation performed /// </summary> public class MapIdentify { #region identify declaration private Map objMap = null; private MainPage objMapPage = null; private DataGrid objQueryResultsDataGrid = null; private MarkerSymbol objQueryMarkerSymbol = null; private FillSymbol objQueryFillSymbol = null; private GeometryType objSelectGeometry = GeometryType.Point; private FillSymbol objQueryPolygonSymbol = null; private LineSymbol objQueryPolylineSymbol = null; private DataPager objResultsDataPager = null; private TextBlock txtRecordsDetails = null; private string strAssetName = string.Empty; #endregion /// <summary> /// Initiate the identify operation with MainPage,querygeometry and asset name /// </summary> /// <param name="pageMapPage"></param> /// <param name="queryGeometry"></param> /// <param name="assetName"></param> public MapIdentify(MainPage pageMapPage, ESRI.ArcGIS.Client.Geometry.Geometry queryGeometry) { #region Local variables int intQueryLayerID; string strQueryServiceUrl = string.Empty; Query objIdentifyQuery = null; string strQueryFileds = string.Empty; string strQueryLayerName = string.Empty; ESRI.ArcGIS.Client.Graphic objFillGraphic = null; GraphicsLayer lyrSelectionGraphicslayer = null; GraphicsLayer lyrDrawGraphicslayer = null; QueryTask objQueryTask = null; GraphicsLayer lyrZoomGraphicslayer = null; #endregion try { #region Set Local variable objMapPage = pageMapPage; objQueryMarkerSymbol = objMapPage.DefaultClickSymbol; this.txtRecordsDetails = pageMapPage.txtRecordsDetails; this.objQueryPolygonSymbol = new FillSymbol(); this.objQueryPolygonSymbol.Fill = new SolidColorBrush(Colors.Yellow); this.objQueryPolylineSymbol = new LineSymbol(); this.objQueryPolylineSymbol.Color = new SolidColorBrush(Colors.Yellow); this.objQueryPolylineSymbol.Width = 3; // this.objQueryMarkerSymbol = pageMapPage.esriDefaultMarker_33; this.objQueryFillSymbol = pageMapPage.DefaultFillSymbol; this.objMap = pageMapPage.slMap; this.objResultsDataPager = pageMapPage.slPager; this.objQueryResultsDataGrid = pageMapPage.grdQueryDetails; lyrSelectionGraphicslayer = objMap.Layers["MySelectionGraphicsLayer"] as GraphicsLayer; lyrDrawGraphicslayer = objMap.Layers["MyDrawGraphicsLayer"] as GraphicsLayer; lyrZoomGraphicslayer = objMap.Layers["MyZoomGraphicsLayer"] as GraphicsLayer; #endregion lyrSelectionGraphicslayer.ClearGraphics(); lyrDrawGraphicslayer.ClearGraphics(); lyrDrawGraphicslayer.ClearGraphics(); objResultsDataPager.PageIndexChanged += ResultsDataPager_PageIndexChanged; lyrSelectionGraphicslayer = objMap.Layers["MySelectionGraphicsLayer"] as GraphicsLayer; lyrSelectionGraphicslayer.ClearGraphics(); lyrDrawGraphicslayer = objMap.Layers["MyDrawGraphicsLayer"] as GraphicsLayer; lyrDrawGraphicslayer.ClearGraphics(); objFillGraphic = new ESRI.ArcGIS.Client.Graphic() { Geometry = queryGeometry, Symbol = objQueryFillSymbol }; //http://sampleserver1.arcgisonline.com/ArcGIS/rest/services/Specialty/ESRI_StatesCitiesRivers_USA/MapServer/0 lyrDrawGraphicslayer.Graphics.Add(objFillGraphic);// strQueryServiceUrl = "http://pc1010/ArcGIS/rest/services/world/MapServer/0"; //intQueryLayerID = MapUtility.getQueryLayerId(objMap, assetName); objQueryTask = new QueryTask(strQueryServiceUrl);//strQueryServiceUrl + "/" + intQueryLayerID); objQueryTask.ExecuteCompleted += QueryTask_ExecuteCompleted; objQueryTask.Failed += QueryTask_Failed; objIdentifyQuery = new ESRI.ArcGIS.Client.Tasks.Query(); //strQueryLayerName = MapUtility.getQueryLayerName(objMap, assetName); objQueryResultsDataGrid.HeadersVisibility = DataGridHeadersVisibility.All; //strQueryFileds = strQueryFileds.Remove(strQueryFileds.Length - 1, 1); objIdentifyQuery.OutFields.AddRange(new string[] { "CITY_NAME, ADMIN_NAME, STATUS" }); objIdentifyQuery.OutFields.Add("*"); objIdentifyQuery.OutSpatialReferenceWKID = objMap.SpatialReference.WKID; objIdentifyQuery.Geometry = queryGeometry; objIdentifyQuery.ReturnGeometry = true; objQueryTask.ExecuteAsync(objIdentifyQuery); } catch (Exception Ex) { MessageBox.Show(Ex.Message, "Unable To Identify", MessageBoxButton.OK); } finally { } } /// <summary> /// Handler for ResultsDataPager page selection change /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void ResultsDataPager_PageIndexChanged(object sender, EventArgs e) { DataPager objPagerItem = sender as DataPager; if (objPagerItem.ItemCount > 0) { txtRecordsDetails.Text = "Displaying " + ((System.Windows.Data.PagedCollectionView)objPagerItem.Source).Count + " Records Out Of " + objPagerItem.ItemCount; } } /// <summary> /// Querytask completion event /// </summary> /// <param name="sender"></param> /// <param name="args"></param> private void QueryTask_ExecuteCompleted(object sender, ESRI.ArcGIS.Client.Tasks.QueryEventArgs args) { #region Local variables QueryTask identifyQueryTask = null; objQueryResultsDataGrid.ItemsSource = null; FeatureSet objFeatureSet = null; GraphicsLayer objSelectionGraphicsLayer = null; PagedCollectionView dataPageView = null; GraphicAttributeColumn objTextBoxColumn = null; string strQueryLayerName = string.Empty; DataPager objPagerItem = null; #endregion identifyQueryTask = sender as QueryTask; objFeatureSet = args.FeatureSet; if (objFeatureSet == null || objFeatureSet.Features.Count < 1) { MessageBox.Show("No records found"); txtRecordsDetails.Text = string.Empty; return; } List<string> strFields = new List<string>(); List<string> strFieldAlias = new List<string>(); strFields.Add("CITY_NAME"); strFields.Add("ADMIN_NAME"); strFields.Add("STATUS"); strFieldAlias.Add("CITY NAME"); strFieldAlias.Add("ADMIN_NAME"); strFieldAlias.Add("STATUS"); for (int objLayerCount = 0; objLayerCount < strFields.Count; objLayerCount++) { objTextBoxColumn = new GraphicAttributeColumn(); objTextBoxColumn.Header = strFieldAlias[objLayerCount].ToString(); objTextBoxColumn.AttributeName = strFields[objLayerCount].ToString(); objTextBoxColumn.IsReadOnly = true; objQueryResultsDataGrid.Columns.Add(objTextBoxColumn); } objSelectGeometry = args.FeatureSet.GeometryType; objSelectionGraphicsLayer = objMap.Layers["MySelectionGraphicsLayer"] as GraphicsLayer; objMap.Extent = objSelectionGraphicsLayer.FullExtent; dataPageView = new PagedCollectionView(identifyQueryTask.LastResult); objQueryResultsDataGrid.ItemsSource = dataPageView; objResultsDataPager.Visibility = Visibility.Visible; objResultsDataPager.DataContext = objQueryResultsDataGrid.ItemsSource; objMapPage.slPager.PageIndex = 0; objPagerItem = objMapPage.slPager; //objQueryResultsDataGrid.SizeChanged+=new SizeChangedEventHandler(objQueryResultsDataGrid_SizeChanged); if (objPagerItem.ItemCount > 0) { txtRecordsDetails.Text = "Displaying " + ((System.Windows.Data.PagedCollectionView)objPagerItem.Source).Count + " Records Out Of " + objPagerItem.ItemCount; } } /// <summary> /// Querytask Failed event /// </summary> /// <param name="sender"></param> /// <param name="args"></param> private void QueryTask_Failed(object sender, TaskFailedEventArgs args) { if (objQueryResultsDataGrid.Columns.Count > 1) { for (int intColcount = 1; intColcount < objQueryResultsDataGrid.Columns.Count; intColcount++) { objQueryResultsDataGrid.Columns.RemoveAt(intColcount); } } MessageBox.Show("Unable To Identify: " + args.Error); } /// <summary> /// Event to handle the query results selection in results displayed in QueryResultsDataGrid /// /// </summary> /// <param name="sender"></param> /// <param name="e"></param> } } 3.运行结果如下: