2012-07-06 29 views
0

這是我的XAML代碼:更改地圖模式在Bing WPF控件

<s:SurfaceWindow x:Class="map_test.SurfaceWindow1" 
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
xmlns:s="http://schemas.microsoft.com/surface/2008" 
xmlns:m="clr-namespace:Microsoft.Maps.MapControl.WPF;assembly=Microsoft.Maps.MapControl.WPF" mc:Ignorable="d" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" Height="1080" Width="1920" WindowStartupLocation="CenterScreen"> 
<Grid> 
    <m:Map x:Name="NotreDameMap" CredentialsProvider="AmNh-jzPEmrdpc2T2wBxEjyB0fvSimavosz4B4_IPEAY4RDE-r7L2rHa439uEzlz" Mode="AerialWithLabels" Center="41.702380,-86.234168" ZoomLevel="16" > 
     <Canvas Width="175.198" Height="85" 
      m:MapLayer.Position="41.702380,-86.234168" 
      m:MapLayer.PositionOrigin="BottomCenter" Opacity="0.75"> 
      <!-- Layer 1/<Group>/<Path> --> 
      <Path StrokeThickness="0.2" Stroke="#ffec008b" StrokeMiterLimit="1.0" Data="F1 M 122.944,144.000"/> 

      <!-- Layer 1/<Group>/<Path> --> 
      <Path StrokeThickness="0.2" Stroke="#ffec008b" StrokeMiterLimit="1.0" Data="F1 M 65.081,47.477"/> 


      <!-- Layer 1/<Group>/<Group>/<Compound Path> --> 
      <Path Fill="#ff0e223e" Data="F1 M 159.484,130.991 L 158.570,130.991 L 158.570,128.309 C 158.791,128.277 159.073,128.247 159.484,128.247 C 160.901,128.247 161.408,128.941 161.408,129.636 C 161.408,130.609 160.523,130.991 159.484,130.991 Z M 161.187,131.494 L 161.187,131.432 C 162.036,131.180 162.732,130.547 162.732,129.505 C 162.732,128.750 162.479,128.213 162.006,127.866 C 161.532,127.521 160.744,127.269 159.544,127.269 C 158.600,127.269 157.968,127.364 157.306,127.457 L 157.306,135.373 L 158.570,135.373 L 158.570,131.965 L 159.453,131.965 C 160.459,131.965 160.964,132.347 161.155,133.355 C 161.312,134.396 161.502,135.122 161.693,135.373 L 163.019,135.373 C 162.890,135.122 162.700,134.646 162.511,133.420 C 162.354,132.251 161.879,131.623 161.187,131.494 Z"/> 

      <!-- Layer 1/<Group>/<Group>/<Compound Path> --> 
      <Path Fill="#ff0e223e" Data="F1 M 154.433,89.842 L 149.441,95.128 L 144.158,95.128 L 144.158,47.566 L 149.441,47.566 L 154.433,52.852 L 154.433,89.842 Z M 123.018,95.128 L 120.080,95.128 L 91.603,47.566 L 123.018,47.566 L 123.018,95.128 Z M 62.242,95.128 L 62.232,95.128 L 62.232,47.566 L 65.170,47.566 L 93.646,95.128 L 62.242,95.128 Z M 41.101,95.128 L 31.693,95.128 L 31.693,47.566 L 41.101,47.566 L 41.101,95.128 Z M 157.372,26.423 L 144.158,26.423 L 144.158,21.138 L 157.372,21.138 L 157.372,0.000 L 109.807,0.000 L 109.807,21.138 L 123.018,21.138 L 123.018,26.423 L 78.677,26.423 L 62.242,0.000 L 27.892,0.000 L 27.892,21.138 L 41.101,21.138 L 41.101,26.423 L 0.000,26.421 L 0.000,47.737 L 10.571,47.737 L 10.571,95.299 L 0.000,95.299 L 0.000,116.276 L 40.808,116.265 L 40.808,121.550 L 27.609,121.550 L 27.609,142.688 L 75.453,142.688 L 75.453,121.550 L 62.242,121.550 L 62.242,116.265 L 106.345,116.265 L 122.165,142.688 L 144.158,142.688 L 144.158,116.265 L 157.372,116.265 L 175.598,97.774 L 175.598,44.921 L 157.372,26.423 Z"/> 

      <!-- Layer 1/<Group>/<Group>/<Compound Path> --> 
      <Path Fill="#ff0e223e" Data="F1 M 159.925,137.267 C 156.771,137.267 154.215,134.617 154.215,131.241 C 154.215,127.928 156.771,125.251 159.895,125.251 L 159.925,125.251 C 163.079,125.251 165.602,127.928 165.602,131.272 C 165.602,134.617 163.079,137.299 159.925,137.267 Z M 159.925,124.146 L 159.895,124.146 C 156.014,124.146 152.859,127.300 152.859,131.241 C 152.859,135.248 156.014,138.373 159.895,138.373 C 163.802,138.373 166.896,135.248 166.896,131.241 C 166.896,127.300 163.802,124.146 159.925,124.146 Z"/> 

      <TextBlock FontSize="9" Foreground="White" Margin="65" TextAlignment="Center"> 
        Hesburgh<LineBreak /> 
        Library <LineBreak /> 
      </TextBlock> 
     </Canvas> 

    </m:Map> 
    <StackPanel> 
     <ToggleButton x:Name="toggleButton" Content="Change Map Mode"/> 
     <TextBlock Text="Maps" /> 

     </StackPanel> 
</Grid> 

很簡單我想創建一個按鈕,將地圖的模式改變,從AerialWithLabels到路模式。

任何幫助將不勝感激。

PS對不起,如果它是一個很基本的問題,我是新來的。如果你正在使用C#與應用程序,然後一個方式做編碼

回答

3

這將是:

  1. 創建一個按鈕XAML,並給它一個名字:

    <Button Name="TestButton"> 
          <TextBlock FontSize="26">Change Map Mode</TextBlock> 
         </Button> 
    
  2. 指定的事件處理程序的按鈕的的MouseUp或觸事件:

    <Button Name="TestButton" TouchUp="Toggle_Map"> 
          <TextBlock FontSize="26">Change Map Mode</TextBlock> 
         </Button> 
    
  3. 在你的C#代碼隱藏,創建事件處理程序像這樣,用正確的C#代碼,切換地圖模式:

    using Microsoft.Maps.MapControl.WPF; 
    
    private void Toggle_Map(object sender, TouchEventArgs e) 
    { 
        if (myMap.Mode.ToString() == "Microsoft.Maps.MapControl.WPF.RoadMode") 
        { 
         //Set the map mode to Aerial with labels 
         myMap.Mode = new AerialMode(true); 
        } 
    
    
        if (myMap.Mode.ToString() == "Microsoft.Maps.MapControl.WPF.AerialMode") 
        { 
         //Set the map mode to RoadMode 
         myMap.Mode = new RoadMode(); 
        } 
    } 
    

MyMap中需要符合地圖控件的名稱,在XAML中使用Name =「myMap」。

應該讓你開始那......

+0

第二個IF語句應該是ELSE IF,否則你將總是以Road模式結束 – user2136076 2015-03-19 10:49:23

1

我不能對以前的答案添加評論。因此,我將修改

1)再回答在C#中的XAML

<Button Content="Toggle Map" HorizontalAlignment="Right" VerticalAlignment="Top" Width="81" Click="btnToggleMap_Click"/> 

2)創建一個按鈕,如果和else事件處理程序應該有一個。該代碼固定的,它是:

using Microsoft.Maps.MapControl.WPF; 

private void btnToggleMap_Click(object sender, TouchEventArgs e) 
{ 
    if (NotreDameMap.Mode.ToString() == "Microsoft.Maps.MapControl.WPF.RoadMode") 
    { 
     //Set the map mode to Aerial with labels 
     NotreDameMap.Mode = new AerialMode(true); 
    } else if (NotreDameMap.Mode.ToString() == "Microsoft.Maps.MapControl.WPF.AerialMode") 
    { 
     //Set the map mode to RoadMode 
     NotreDameMap.Mode = new RoadMode(); 
    } 
} 

注:NotreDameMap是地圖的名稱,它必須是一樣的地圖上XAML

您可以複製和粘貼代碼,並應工作。