<UserControlx:Class="ArcGISWPFSDK.EventBasedAsyncLambda"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,131,0"Padding="5"Background="White"BorderBrush="Black"BorderThickness="1"Width="231"><Border.Effect><DropShadowEffect/></Border.Effect><StackPanelOrientation="Horizontal"VerticalAlignment="Top"HorizontalAlignment="Left"Width="330"Height="50"Margin="5"><ButtonContent="QueryTask using Event Based Async"Margin="5"Width="196"Click="Button_Click_1"/></StackPanel></Border></Grid></UserControl>
using System.Windows.Controls;
using ESRI.ArcGIS.Client;
using ESRI.ArcGIS.Client.Tasks;
using System.Collections.Generic;
using System.Windows;
namespace ArcGISWPFSDK
{
publicpartialclass EventBasedAsyncLambda : UserControl
{
public EventBasedAsyncLambda()
{
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");
// 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 Task Failed event.
queryTask.Failed += (a, b) =>
{
if (b.Error != null)
MessageBox.Show(b.Error.Message);
};
// Call ExecuteAsync after in-line event handler declaration.
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 System.Windows.Controls
Imports ESRI.ArcGIS.Client
Imports ESRI.ArcGIS.Client.Tasks
Imports System.Collections.Generic
Imports System.Windows
Namespace ArcGISWPFSDK
PartialPublicClass EventBasedAsyncLambda
Inherits UserControl
PublicSubNew()
InitializeComponent()
EndSubPrivateSub Button_Click_1(sender AsObject, e As System.Windows.RoutedEventArgs)
UsingEventBasedLambda()
EndSubPrivateSub UsingEventBasedLambda()
ClearResults()
Dim t = New QueryTask("http://sampleserver6.arcgisonline.com/arcgis/rest/services/Census/MapServer/3")
t.ExecuteCompleted += Sub(a, b)
AddResultsToMap(b.FeatureSet.Features)
EndSub
t.Failed += Sub(a, b)
If b.[Error] IsNotNothingThen
MessageBox.Show(b.[Error].Message)
EndIfEndSub
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