Image 如何从Bing Maps异步调用填充多个Silverlight图像?

Image 如何从Bing Maps异步调用填充多个Silverlight图像?,image,silverlight,asynchronous,bing-maps,Image,Silverlight,Asynchronous,Bing Maps,我有下面的代码,如果您只想用Bing地图的响应填充一个图像,那么它就可以正常工作。但是如果我尝试做两个,那么变量_currentImage总是以“image1”结束,因为调用是异步的。如何将图像变量传递给ImageryServiceGetMapUriCompleted方法 using System; using System.Windows.Controls; using System.Windows.Media.Imaging; using BasicBingMapsImagerySvc.Im

我有下面的代码,如果您只想用Bing地图的响应填充一个图像,那么它就可以正常工作。但是如果我尝试做两个,那么变量_currentImage总是以“image1”结束,因为调用是异步的。如何将图像变量传递给ImageryServiceGetMapUriCompleted方法

using System;
using System.Windows.Controls;
using System.Windows.Media.Imaging;
using BasicBingMapsImagerySvc.ImageryService;

namespace BasicBingMapsImagerySvc
{
    public partial class MainPage : UserControl
    {
        private const string BingMapsKey = "my key";

        private Image _currentImage;

        public MainPage()
        {
            InitializeComponent();

            GetMap(42.573377, -101.032251, image0, MapStyle.AerialWithLabels);

            GetMap(42.573377, -101.032251, image1, MapStyle.Road_v1);
        }


        private void GetMap(double lat, double lon, Image image, MapStyle mapStyle)
        {
            var mapUriRequest = new MapUriRequest();

            // Set credentials using a valid Bing Maps key
            mapUriRequest.Credentials = new Credentials();
            mapUriRequest.Credentials.ApplicationId = BingMapsKey;

            // Set the location of the requested image
            mapUriRequest.Center = new Location();
            mapUriRequest.Center.Latitude = lat;
            mapUriRequest.Center.Longitude = lon;

            // Set the map style and zoom level
            var mapUriOptions = new MapUriOptions();
            mapUriOptions.Style = mapStyle;
            mapUriOptions.ZoomLevel = 13;

            // Set the size of the requested image to match the size of the image control
            mapUriOptions.ImageSize = new SizeOfint();
            mapUriOptions.ImageSize.Height = 256;
            mapUriOptions.ImageSize.Width = 256;

            mapUriRequest.Options = mapUriOptions;

            var imageryService = new ImageryServiceClient("BasicHttpBinding_IImageryService");
            imageryService.GetMapUriCompleted += ImageryServiceGetMapUriCompleted;

            _currentImage = image;

            imageryService.GetMapUriAsync(mapUriRequest);


        }

        private void ImageryServiceGetMapUriCompleted(object sender, GetMapUriCompletedEventArgs e)
        {
            // The result is an MapUriResponse Object
            MapUriResponse mapUriResponse = e.Result;
            var bmpImg = new BitmapImage(new Uri(mapUriResponse.Uri));

            _currentImage.Source = bmpImg;
        }
    }
}

您可以为事件处理程序使用lambda表达式/委托,它允许您“捕获”对映像的引用:

  var imageryService = new ImageryServiceClient("BasicHttpBinding_IImageryService");
  imageryService.GetMapUriCompleted += (s,e) =>
     {
         // The result is an MapUriResponse Object
         MapUriResponse mapUriResponse = e.Result;
         var bmpImg = new BitmapImage(new Uri(mapUriResponse.Uri));

         // set the image source
         image.Source = bmpImg;
    };

科林,你太棒了!谢谢我试图自己去那里,但是弄不懂lambda语法。