<UserControlx:Class="ArcGISWPFSDK.LocalEventBasedAsyncLambda"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,133,0"Padding="5"Background="White"BorderBrush="Black"BorderThickness="1"Width="229"><Border.Effect><DropShadowEffect/></Border.Effect><StackPanelOrientation="Horizontal"VerticalAlignment="Top"HorizontalAlignment="Left"Width="330"Height="50"Margin="5"><Buttonx:Name="button1"Content="QueryTask using Event Based Async"Margin="5"Width="199"Click="Button_Click_1"IsEnabled="False"/></StackPanel></Border></Grid></UserControl>
using ESRI.ArcGIS.Client;
using ESRI.ArcGIS.Client.Local;
using ESRI.ArcGIS.Client.Tasks;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Controls;
namespace ArcGISWPFSDK
{
publicpartialclass LocalEventBasedAsyncLambda : UserControl
{
LocalMapService _localMapService;
public LocalEventBasedAsyncLambda()
{
InitializeComponent();
_localMapService = new LocalMapService(@"..\Data\MPKS\USCitiesStates.mpk");
// Use in-line Lambda expression to handle the async delegate callback.
_localMapService.StartAsync((localMapService) =>
{
button1.IsEnabled = true;
});
}
privatevoid Button_Click_1(object sender, System.Windows.RoutedEventArgs e)
{
ClearResults();
QueryTask queryTask = new QueryTask(_localMapService.UrlMapService + "/2");
// Use in-line Lambda expression to handle the ExecuteCompleted event.
queryTask.ExecuteCompleted += (a, b) =>
{
AddResultsToMap(b.FeatureSet.Features);
};
// Use in-line Lambda expression to handle the Failed event.
queryTask.Failed += (a, b) =>
{
if (b.Error != null)
MessageBox.Show(b.Error.Message);
};
// Call Execute Async after the in-line handlers have been declared.
queryTask.ExecuteAsync(new Query()
{
Where = "1=1",
ReturnGeometry = true,
OutSpatialReference = MyMap.SpatialReference
});
}
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.Local
Imports ESRI.ArcGIS.Client.Tasks
Imports System.Collections.Generic
Imports System.Windows
Imports System.Windows.Controls
Namespace ArcGISWPFSDK
PartialPublicClass LocalEventBasedAsyncLambda
Inherits UserControl
Private _mapService As LocalMapService
PublicSubNew()
InitializeComponent()
CreateMapServiceAsync()
EndSubPrivateSub Button_Click_1(sender AsObject, e As System.Windows.RoutedEventArgs)
UsingEventBasedLambda()
EndSubPublicSub CreateMapServiceAsync()
LocalMapService.GetServiceAsync("..\Data\MPKS\USCitiesStates.mpk", Function(localMapService__1)
_mapService = localMapService__1
button1.IsEnabled = TrueEndFunction)
EndSubPrivateSub UsingEventBasedLambda()
ClearResults()
Dim t = New QueryTask(Convert.ToString(_mapService.UrlMapService) & "/2")
t.ExecuteCompleted += Function(a, b)
AddResultsToMap(b.FeatureSet.Features)
EndFunction
t.Failed += Function(a, b)
If b.[Error] IsNotNothingThen
MessageBox.Show(b.[Error].Message)
EndIfEndFunction
t.ExecuteAsync(New Query() With { _
.Where = "1=1", _
.ReturnGeometry = True, _
.OutSpatialReference = MyMap.SpatialReference _
})
EndSubPrivateSub 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