<UserControlx:Class="ArcGISWPFSDK.AsyncAwait"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 Async Await"Margin="5"Width="188"Click="Button_Click_1"/></StackPanel></Border></Grid></UserControl>
using System.Windows.Controls;
using System.Windows;
using ESRI.ArcGIS.Client;
using ESRI.ArcGIS.Client.Tasks;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
namespace ArcGISWPFSDK
{
publicpartialclass AsyncAwait : UserControl
{
public AsyncAwait()
{
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");
/*
* If using .NET 4.5 / 4.5.1 (or .NET 4.0 with the Async Targeting Pack for Visual Studio 11) can simply "await" methods which return a Task.
* However - this sample application is compiled as .NET 4.0 and therefore uses ContinueWith as an alternative.
*///QueryResult queryResult = null;//try//{// queryResult = await queryTask.ExecuteTaskAsync(new Query()// {// Where = "1=1",// ReturnGeometry = true,// OutSpatialReference = MyMap.SpatialReference// });// AddResultsToMap(queryResult.FeatureSet.Features);//}//catch (AggregateException ex)//{// var innermostExceptions = ex.Flatten().InnerExceptions;// if (innermostExceptions != null && innermostExceptions.Count > 0)// MessageBox.Show(innermostExceptions[0].Message);// else// MessageBox.Show(ex.Message);//}
Task task = queryTask.ExecuteTaskAsync(new Query()
{
Where = "1=1",
ReturnGeometry = true,
OutSpatialReference = MyMap.SpatialReference
}).ContinueWith((r) =>
{
if (r.IsFaulted)
{
var ex = r.Exception;
}
elseif (r.Result != null)
{
AddResultsToMap(r.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 System.Windows.Controls
Imports System.Windows
Imports ESRI.ArcGIS.Client
Imports ESRI.ArcGIS.Client.Tasks
Imports System.Collections.Generic
Imports System.Threading.Tasks
Namespace ArcGISWPFSDK
PartialPublicClass AsyncAwait
Inherits UserControl
PublicSubNew()
InitializeComponent()
EndSubPrivateSub Button_Click_1(sender AsObject, e As System.Windows.RoutedEventArgs)
UsingAsyncAwait()
EndSubPrivate Async Function UsingAsyncAwait() As Task
ClearResults()
Dim t = New QueryTask("http://sampleserver6.arcgisonline.com/arcgis/rest/services/Census/MapServer/3")
Dim b As QueryResult = NothingTry
b = Await t.ExecuteTaskAsync(New Query() With { _
.Where = "1=1", _
.ReturnGeometry = True, _
.OutSpatialReference = MyMap.SpatialReference _
})
AddResultsToMap(b.FeatureSet.Features)
Catch ex As AggregateException
Dim innermostExceptions = ex.Flatten().InnerExceptions
If innermostExceptions IsNotNothingAndAlso innermostExceptions.Count > 0 Then
MessageBox.Show(innermostExceptions(0).Message)
Else
MessageBox.Show(ex.Message)
EndIfEndTryEndFunctionPrivateSub 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