<UserControlx:Class="ArcGISWPFSDK.TaskBasedAsync"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:SimpleFillSymbolx:Key="RedFillSymbol"Fill="#66FF0000"BorderBrush="Red"BorderThickness="2"/><esri:SimpleRendererx:Key="FillRenderer"Symbol="{StaticResource RedFillSymbol}"/></Grid.Resources><esri:Mapx:Name="MyMap"UseAcceleratedDisplay="True"WrapAround="True"Background="#FFE3E3E3"><esri:ArcGISTiledMapServiceLayerID="MyLayer"Url="http://services.arcgisonline.com/ArcGIS/rest/services/World_Street_Map/MapServer"/><esri:GraphicsLayerID="MyGraphics"Renderer="{StaticResource FillRenderer}"/></esri:Map><BorderHorizontalAlignment="Right"VerticalAlignment="Top"Margin="0,10,143,0"Padding="5"Background="White"BorderBrush="Black"BorderThickness="1"Width="219"><Border.Effect><DropShadowEffect/></Border.Effect><StackPanelOrientation="Horizontal"VerticalAlignment="Top"HorizontalAlignment="Left"Width="330"Height="50"Margin="5"><ButtonContent="QueryTask using Task Based Async"Margin="5"Width="188"Click="Button_Click_1"/></StackPanel></Border></Grid></UserControl>
using ESRI.ArcGIS.Client;
using ESRI.ArcGIS.Client.Tasks;
using System.Collections.Generic;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
namespace ArcGISWPFSDK
{
publicpartialclass TaskBasedAsync : UserControl
{
public TaskBasedAsync()
{
InitializeComponent();
}
privatevoid Button_Click_1(object sender, System.Windows.RoutedEventArgs e)
{
ClearResults();
QueryTask queryTask = new QueryTask("http://sampleserver6.arcgisonline.com/arcgis/rest/services/Census/MapServer/3");
// Call ExecuteTaskAsync and handle add the results to the map in the Continuation.
Task task = queryTask.ExecuteTaskAsync(new Query()
{
Where = "1=1",
ReturnGeometry = true,
OutSpatialReference = MyMap.SpatialReference
}).ContinueWith((queryResultTask) =>
{
if (queryResultTask.IsFaulted)
{
MessageBox.Show(queryResultTask.Exception.Message);
}
elseif (queryResultTask.Result != null)
{
AddResultsToMap(queryResultTask.Result.FeatureSet.Features);
}
}, TaskScheduler.FromCurrentSynchronizationContext());
}
privatevoid ClearResults()
{
GraphicsLayer graphicsLayer = MyMap.Layers["MyGraphics"] as GraphicsLayer;
graphicsLayer.Graphics.Clear();
}
privatevoid AddResultsToMap(IList<Graphic> features)
{
GraphicsLayer graphicsLayer = MyMap.Layers["MyGraphics"] as GraphicsLayer;
graphicsLayer.Graphics.AddRange(features);
MyMap.ZoomTo(graphicsLayer.FullExtent);
}
}
}
Imports ESRI.ArcGIS.Client
Imports ESRI.ArcGIS.Client.Tasks
Imports System.Collections.Generic
Imports System.Threading.Tasks
Imports System.Windows.Controls
Namespace ArcGISWPFSDK
PartialPublicClass TaskBasedAsync
Inherits UserControl
PublicSubNew()
InitializeComponent()
EndSubPrivateSub Button_Click_1(sender AsObject, e As System.Windows.RoutedEventArgs)
UsingTaskBased()
EndSubPrivateFunction UsingTaskBased() As Task
ClearResults()
Dim t = New QueryTask("http://sampleserver6.arcgisonline.com/arcgis/rest/services/Census/MapServer/3")
Dim task As Task(Of QueryResult) = t.ExecuteTaskAsync(New Query() With { _
.Where = "1=1", _
.ReturnGeometry = True, _
.OutSpatialReference = MyMap.SpatialReference _
})
task.ContinueWith(Function(r)
If r.IsFaulted ThenDim ex = r.Exception
ElseIf r.Result IsNotNothingThen
AddResultsToMap(r.Result.FeatureSet.Features)
EndIfEndFunction, TaskScheduler.FromCurrentSynchronizationContext())
EndFunctionPrivateSub ClearResults()
Dim l = TryCast(MyMap.Layers("MyGraphics"), GraphicsLayer)
l.Graphics.Clear()
EndSubPrivateSub AddResultsToMap(features As IList(Of Graphic))
Dim l = TryCast(MyMap.Layers("MyGraphics"), GraphicsLayer)
l.Graphics.AddRange(features)
MyMap.ZoomTo(l.FullExtent)
EndSubEndClassEndNamespace