This sample demonstrates using the Event-based Asynchronous Pattern (EAP) for a QueryTask using local data.
Download Sample Application
< UserControl x:Class = " ArcGISWPFSDK.LocalEventBasedAsync "
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 " >
< Grid >
< Grid.Resources >
< esri : SimpleFillSymbol x:Key = " RedFillSymbol "
Fill = " #66FF0000 "
BorderBrush = " Red "
BorderThickness = " 2 " />
< esri : SimpleRenderer x:Key = " FillRenderer "
Symbol = " {StaticResource RedFillSymbol} " />
</ Grid.Resources >
< esri : Map x:Name = " MyMap " UseAcceleratedDisplay = " True " WrapAround = " True " Background = " #FFE3E3E3 " >
< esri : ArcGISLocalTiledLayer ID = " BaseMap " Path = " ..\Data\TPKs\Topographic.tpk " />
< esri : GraphicsLayer ID = " MyGraphics "
Renderer = " {StaticResource FillRenderer} " />
</ esri : Map >
< Border HorizontalAlignment = " Right " VerticalAlignment = " Top " Margin = " 0,10,133,0 " Padding = " 5 " Background = " White "
BorderBrush = " Black " BorderThickness = " 1 " Width = " 229 " >
< Border.Effect >
< DropShadowEffect />
</ Border.Effect >
< StackPanel Orientation = " Horizontal " VerticalAlignment = " Top " HorizontalAlignment = " Left "
Width = " 330 " Height = " 50 " Margin = " 5 " >
< Button x:Name = " button1 " Content = " QueryTask using Event Based Async " Margin = " 5 " Width = " 199 " Click = " Button_Click_1 " IsEnabled = " False " />
</ StackPanel >
</ Border >
</ Grid >
</ UserControl >
using System.Windows.Controls;
using ESRI.ArcGIS.Client;
using ESRI.ArcGIS.Client.Tasks;
using System.Collections.Generic;
using ESRI.ArcGIS.Client.Local;
using System.Windows;
namespace ArcGISWPFSDK
{
public partial class LocalEventBasedAsync : UserControl
{
LocalMapService _localMapService;
public LocalEventBasedAsync()
{
InitializeComponent();
_localMapService = new LocalMapService(@"..\Data\MPKS\USCitiesStates.mpk" );
_localMapService.StartCompleted += _localMapService_StartCompleted;
_localMapService.StartAsync();
}
// Explicit named event handles the StartCompleted event.
void _localMapService_StartCompleted(object sender, System.ComponentModel.AsyncCompletedEventArgs e)
{
if (_localMapService.Error != null )
{
MessageBox.Show(_localMapService.Error.Message);
}
else
button1.IsEnabled = true ;
}
private void Button_Click_1(object sender, System.Windows.RoutedEventArgs e)
{
ClearResults();
QueryTask queryTask = new QueryTask(_localMapService.UrlMapService + "/2" );
// Declare event handlers
queryTask.ExecuteCompleted += QueryTask_ExecuteCompleted;
queryTask.Failed += QueryTask_Failed;
ESRI.ArcGIS.Client.Tasks.Query query = new ESRI.ArcGIS.Client.Tasks.Query();
query.Where = "1=1" ;
query.ReturnGeometry = true ;
query.OutSpatialReference = MyMap.SpatialReference;
queryTask.ExecuteAsync(query, "initial" );
}
// Explicit named event handles QueryTask Execute Completed Event.
private void QueryTask_ExecuteCompleted(object sender, ESRI.ArcGIS.Client.Tasks.QueryEventArgs args)
{
FeatureSet featureSet = args.FeatureSet;
AddResultsToMap(featureSet.Features);
}
// Explicit named event handles QueryTask Failed Event.
private void QueryTask_Failed(object sender, TaskFailedEventArgs args)
{
MessageBox.Show("Query failed: " + args.Error);
}
private void ClearResults()
{
GraphicsLayer graphicsLayer = MyMap.Layers["MyGraphics" ] as GraphicsLayer;
graphicsLayer.Graphics.Clear();
}
private void AddResultsToMap(IList<Graphic> features)
{
GraphicsLayer graphicsLayer = MyMap.Layers["MyGraphics" ] as GraphicsLayer;
graphicsLayer.Graphics.AddRange(features);
MyMap.ZoomTo(graphicsLayer.FullExtent);
}
}
}
Imports System.Windows.Controls
Imports ESRI.ArcGIS.Client
Imports ESRI.ArcGIS.Client.Tasks
Imports System.Collections.Generic
Imports ESRI.ArcGIS.Client.Local
Imports System.Windows
Namespace ArcGISWPFSDK
Partial Public Class LocalEventBasedAsync
Inherits UserControl
Private _mapService As LocalMapService
Public Sub New ()
InitializeComponent()
CreateMapServiceAsync()
End Sub
Private Sub Button_Click_1(sender As Object , e As System.Windows.RoutedEventArgs)
UsingEventBased()
End Sub
Public Sub CreateMapServiceAsync()
LocalMapService.GetServiceAsync("..\Data\MPKS\USCitiesStates.mpk" , Sub (localMapService__1)
_mapService = localMapService__1
button1.IsEnabled = True
End Sub )
End Sub
Private Sub UsingEventBased()
ClearResults()
Dim t As New QueryTask(_mapService.UrlMapService & "/2" )
AddHandler t.ExecuteCompleted, AddressOf QueryTask_ExecuteCompleted
AddHandler t.Failed, AddressOf QueryTask_Failed
Dim query As New ESRI.ArcGIS.Client.Tasks.Query()
query.Where = "1=1"
query.ReturnGeometry = True
query.OutSpatialReference = MyMap.SpatialReference
t.ExecuteAsync(query, "initial" )
End Sub
Private Sub QueryTask_ExecuteCompleted(sender As Object , args As ESRI.ArcGIS.Client.Tasks.QueryEventArgs)
Dim featureSet As FeatureSet = args.FeatureSet
AddResultsToMap(featureSet.Features)
End Sub
Private Sub QueryTask_Failed(sender As Object , args As TaskFailedEventArgs)
MessageBox.Show("Query failed: " & Convert.ToString(args.[Error ]))
End Sub
Private Sub ClearResults()
Dim l = TryCast(MyMap.Layers("MyGraphics" ), GraphicsLayer)
l.Graphics.Clear()
End Sub
Private Sub AddResultsToMap(features As IList(Of Graphic))
Dim l = TryCast(MyMap.Layers("MyGraphics" ), GraphicsLayer)
l.Graphics.AddRange(features)
MyMap.ZoomTo(l.FullExtent)
End Sub
End Class
End Namespace
5/16/2014