当我尝试放大(Button_Click_1 事件)时,我在尝试将图像/图层添加回地图时遇到错误。
我应该注意:我已经大大简化了代码,以便更容易找出错误(我似乎无法弄清楚)。每个缩放级别都有一组不同的图像,以防您想知道为什么我需要不断清除图层/图像。
每个图像/图层/叠加层都是全局定义的(这样我就可以在几种方法中使用它们
 Image img1 = new Image();
     Image img2 = new Image();
     MapLayer lyr1 = new MapLayer();
     MapLayer lyr2 = new MapLayer();
     MapOverlay ovrly1 = new MapOverlay();
     MapOverlay ovrly2 = new MapOverlay();
当页面以单独的方法加载时,这些中的每一个都被初始化:
     private void initializeImages()
     {
         ovrly1.GeoCoordinate = new GeoCoordinate(49.33783000, -0.45215600);
         img1.Source = new BitmapImage(new Uri("images/push-pin.png", UriKind.Relative));
         ovrly1.Content = img1;
         ovrly1.PositionOrigin = new Point(0.0, 0.0);
         img1.Opacity = 0.8;
         img1.Height = 30;
         img1.Width = 30;
         img1.Tap += img1_Tap;
         ovrly2.GeoCoordinate = new GeoCoordinate(49.35783000, -0.45425600);
         img2.Source = new BitmapImage(new Uri("images/push-pin.png", UriKind.Relative));
         ovrly2.Content = img2;
         ovrly2.PositionOrigin = new Point(0.0, 0.0);
         img2.Opacity = 0.8;
         img2.Height = 30;
         img2.Width = 30;
         img2.Tap += img2_Tap;
     }
当我第一次尝试放大 Button_Click 时,它工作正常。但是任何其他时候我尝试缩放时都会出现错误:
     private void Button_Click_1(object sender, RoutedEventArgs e)
     {
         map1.ZoomLevel += 1;
         map1.Layers.Clear();
         lyr1.Add(ovrly1);  // ERROR OCCURS HERE
         map1.Layers.Add(lyr1);
         lyr2.Add(ovrly2);
         map1.Layers.Add(lyr2);
     }
当我在 Button_Click 事件中“本地”声明所有图像/叠加层/图层时,此错误消失。但我不能这样做,否则我将无法访问该方法之外的图像。
任何帮助将不胜感激。