<UserControlx:Class="ArcGISWPFSDK.LocalTaskBasedAsync"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:ArcGISLocalTiledLayerID="BaseMap"Path="..\Data\TPKs\Topographic.tpk"/><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"><Buttonx:Name="button1"Content="QueryTask using Task Based Async"Margin="5"Width="188"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 System.Threading.Tasks;
using ESRI.ArcGIS.Client.Local;
using System.Windows;
using System;
namespace ArcGISWPFSDK
{
publicpartialclass LocalTaskBasedAsync : UserControl
{
LocalMapService _localMapService;
public LocalTaskBasedAsync()
{
InitializeComponent();
try
{
_localMapService = new LocalMapService(@"..\Data\MPKS\USCitiesStates.mpk");
// Use Task.ContinueWith and enable the button in the continuation.
_localMapService.StartAsync().ContinueWith(localServiceTask =>
{
if (localServiceTask.IsFaulted)
MessageBox.Show(localServiceTask.Exception.Message);
else
button1.IsEnabled = true;
},TaskScheduler.FromCurrentSynchronizationContext());
}
catch (System.Exception)
{
throw;
}
}
privatevoid Button_Click_1(object sender, System.Windows.RoutedEventArgs e)
{
try
{
ClearResults();
QueryTask queryTask = new QueryTask(_localMapService.UrlMapService + "/2");
// 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(queryResult =>
{
if (queryResult.IsFaulted)
{
MessageBox.Show(queryResult.Exception.Message);
}
elseif (queryResult.Result != null)
{
AddResultsToMap(queryResult.Result.FeatureSet.Features);
}
}, TaskScheduler.FromCurrentSynchronizationContext());
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
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 ESRI.ArcGIS.Client
Imports ESRI.ArcGIS.Client.Tasks
Imports System.Collections.Generic
Imports System.Threading.Tasks
Imports ESRI.ArcGIS.Client.Local
Namespace ArcGISWPFSDK
PartialPublicClass LocalTaskBasedAsync
Inherits UserControl
Private _mapService As LocalMapService
PublicSubNew()
InitializeComponent()
CreateMapServiceAsync()
EndSubPrivateSub Button_Click_1(sender AsObject, e As System.Windows.RoutedEventArgs)
UsingTaskBased()
EndSubPublicSub CreateMapServiceAsync()
LocalMapService.GetServiceAsync("..\Data\MPKS\USCitiesStates.mpk", Sub(localMapService__1)
_mapService = localMapService__1
button1.IsEnabled = TrueEndSub)
EndSubPrivateFunction UsingTaskBased() As Task
ClearResults()
Dim t = New QueryTask(Convert.ToString(_mapService.UrlMapService) & "/2")
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