This sample demonstrates using the new awaitable TAP operation using async and wait keywords availiable in .NET Framework 4.5 for a QueryTask on local data.
Download Sample Application
< UserControl x:Class = " ArcGISWPFSDK.LocalAsyncAwait "
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,143,0 " Padding = " 5 " Background = " White "
BorderBrush = " Black " BorderThickness = " 1 " Width = " 219 " >
< 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 Async Await " Margin = " 5 " Width = " 188 " Click = " Button_Click_1 " IsEnabled = " False " />
</ StackPanel >
</ Border >
</ Grid >
</ UserControl >
using System.Windows.Controls;
using System.Windows;
using ESRI.ArcGIS.Client;
using ESRI.ArcGIS.Client.Local;
using ESRI.ArcGIS.Client.Tasks;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
namespace ArcGISWPFSDK
{
public partial class LocalAsyncAwait : UserControl
{
LocalMapService _localMapService;
public LocalAsyncAwait()
{
InitializeComponent();
/*
* Note:- If using .NET 4.5 / 4.5.1 (or .NET 4.0 with the Async Targeting Pack for Visual Studio 11) can use "async" and "await" keywords.
* However - this sample application is compiled as .NET 4.0 and therefore uses Task.ContinueWith as an alternative.
*/
//try
//{
// _mapService = await LocalMapService.StartAsync(@"..\Data\MPKS\USCitiesStates.mpk");
//}
//catch (Exception ex)
//{
// var aex = ex as AggregateException;
// MessageBox.Show(aex.Message);
// var innerExceptions = aex.Flatten().InnerExceptions;
//}
try
{
_localMapService = new LocalMapService(@"..\Data\MPKS\USCitiesStates.mpk" );
_localMapService.StartAsync().ContinueWith(localServiceTask =>
{
if (localServiceTask.IsFaulted)
MessageBox.Show(localServiceTask.Exception.Message);
else
button1.IsEnabled = true ;
}, TaskScheduler.FromCurrentSynchronizationContext());
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
private void Button_Click_1(object sender, System.Windows.RoutedEventArgs e)
{
ClearResults();
QueryTask queryTask = new QueryTask(_localMapService.UrlMapService + "/2" );
/*
* 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.
*/
//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((queryResultTask) =>
{
if (queryResultTask.IsFaulted)
{
MessageBox.Show(queryResultTask.Exception.Message);
}
else if (queryResultTask.Result != null )
{
AddResultsToMap(queryResultTask.Result.FeatureSet.Features);
}
}, TaskScheduler.FromCurrentSynchronizationContext());
}
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 System.Windows
Imports ESRI.ArcGIS.Client
Imports ESRI.ArcGIS.Client.Local
Imports ESRI.ArcGIS.Client.Tasks
Imports System.Collections.Generic
Imports System.Threading.Tasks
Namespace ArcGISWPFSDK
Partial Public Class LocalAsyncAwait
Inherits UserControl
Private _mapService As LocalMapService
Public Sub New ()
InitializeComponent()
CreateMapServiceAsyncAwait()
End Sub
Private Sub Button_Click_1(sender As Object , e As System.Windows.RoutedEventArgs)
UsingAsyncAwait()
End Sub
Public Async Sub CreateMapServiceAsyncAwait()
Try
_mapService = Await LocalMapService.GetServiceAsync("..\Data\MPKS\USCitiesStates.mpk" )
Catch ex As Exception
Dim aex = TryCast(ex, AggregateException)
MessageBox.Show(aex.Message)
Dim innerExceptions = aex.Flatten().InnerExceptions
End Try
button1.IsEnabled = True
End Sub
Private Async Function UsingAsyncAwait() As Task
ClearResults()
Dim t = New QueryTask(Convert.ToString(_mapService.UrlMapService) & "/2" )
Dim b As QueryResult = Nothing
Try
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 IsNot Nothing AndAlso innermostExceptions.Count > 0 Then
MessageBox.Show(innermostExceptions(0).Message)
Else
MessageBox.Show(ex.Message)
End If
End Try
End Function
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