2016-07-23 57 views
1

解決地圖控件Template10

我解決了這個問題的事實,我必須添加一個MapItemsControl.ItemTemplate。沒有這個,它不會呈現任何超過控件名稱的東西。

因此,只要添加以下代碼:

<Maps:MapItemsControl x:Name="mainMapItems" ItemsSource="{Binding MapItems}"> 
<Maps:MapItemsControl.ItemTemplate> 
    <DataTemplate> 
    <StackPanel Background="Transparent"> 
    <TextBlock Maps:MapControl.Location="{Binding Location}" Text="{Binding Title}" Maps:MapControl.NormalizedAnchorPoint="0.5,0.5" FontSize="20" Margin="5"/> 
    </StackPanel> 
    </DataTemplate> 
</Maps:MapItemsControl.ItemTemplate> 
</Maps:MapItemsControl> 

它並不完美,因爲這不會給你在地圖上的圖標,而是隻是一個文本。但可以通過在集合中添加Image =「」輕鬆解決。


我想在Template10佈局中使用MapControl。

我使用的代碼是

MainPage.xaml中

  <Maps:MapControl x:Name="MapControl1" 
      ZoomInteractionMode="GestureAndControl" 
      TiltInteractionMode="GestureAndControl" 
      MapServiceToken="<ApiCodeHere>" 
      Loaded="{x:Bind ViewModel.Map_Loaded}"/> 

MainPageViewModel.cs

MapControl _Map; 
    public MapControl Map { get { return _Map; } set { Set(ref _Map, value); RaisePropertyChanged(); } } 

    public async void Map_Loaded(object sender, Windows.UI.Xaml.RoutedEventArgs e) 
    { 

     MapControl newMap = new MapControl(); 

     Geoposition userLocation = await GetUserLocation(); 

     BasicGeoposition GeoPos_UserLocation = new BasicGeoposition() { Latitude = userLocation.Coordinate.Point.Position.Latitude, Longitude = userLocation.Coordinate.Point.Position.Longitude }; 
     BasicGeoposition GeoPos_NorthWest = new BasicGeoposition() { Latitude = userLocation.Coordinate.Point.Position.Latitude + 0.05, Longitude = userLocation.Coordinate.Point.Position.Latitude + 0.1 }; 
     BasicGeoposition GeoPos_SouthEast = new BasicGeoposition() { Latitude = userLocation.Coordinate.Point.Position.Latitude - 0.05, Longitude = userLocation.Coordinate.Point.Position.Latitude - 0.1 }; 

     GeoboundingBox mapBox = new GeoboundingBox(GeoPos_NorthWest, GeoPos_SouthEast); 

     // Add Point for User 
     MapIcon Icon_UserLocation = new MapIcon() { Location = new Geopoint(GeoPos_UserLocation) }; 
     newMap.MapElements.Add(Icon_UserLocation); 
     newMap.Center = new Geopoint(mapBox.Center); 

     Map = newMap; 

     await Task.CompletedTask; 
    } 

爲exepcted的Map_Loaded功能被激活。我遇到的麻煩是,如果這是一個正常的項目,我會直接將數據設置爲MapControl1.Center和MapControl1.MapElements.Add。但是由於這是一個MVVM項目,這不是如何完成的,我對如何繼續處理有點困惑。

我想要做一些像Views.MainPage.MapControl1.Center = new Geopoint(),但這顯然不起作用。


其他更新

事實證明,這是我想的那麼簡單。這只是一個按正確順序思考的問題。

MapControl具有縮放和居中等控件。因此,對於MVVM代碼這個作品

Center="{Binding MapCenter,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" 
Zoom="{Binding MapZoom,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" 

我有過但與問題的文件我源中描述的設置MapItems。

爲了讓你需要添加MapItemsControl在地圖上的項目,它應該像這樣...

<Maps:MapItemsControl x:Name="mainMapItems" ItemsSource="{Binding MapItems,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}"></Maps:MapItemsControl> 

但我在MainPageViewModel.xaml代碼不能與這方面的工作。這些項目不會更新。

IList<MapElement> _MapItems; 
public IList<MapElement> MapItems { get { return _MapItems; } set { Set(ref _MapItems, value); RaisePropertyChanged(); } } 

IList<MapElement> MapItems = new List<MapElement>() { 
    new MapIcon() { 
     Location = new Geopoint(GeoPos_UserLocation), 
     Title = "You Are Here!" 
    } 
}; 

來源:Windows 10 SDK Bing Maps Control

回答

1

我解決了這個問題的事實,我必須添加一個MapItemsControl.ItemTemplate。沒有這個,它不會呈現任何超過控件名稱的東西。

因此,只要添加以下代碼:

<Maps:MapItemsControl x:Name="mainMapItems" ItemsSource="{Binding MapItems}"> 
<Maps:MapItemsControl.ItemTemplate> 
    <DataTemplate> 
    <StackPanel Background="Transparent"> 
    <TextBlock Maps:MapControl.Location="{Binding Location}" Text="{Binding Title}" Maps:MapControl.NormalizedAnchorPoint="0.5,0.5" FontSize="20" Margin="5"/> 
    </StackPanel> 
    </DataTemplate> 
</Maps:MapItemsControl.ItemTemplate> 
</Maps:MapItemsControl> 

它並不完美,因爲這不會給你在地圖上的圖標,而是隻是一個文本。但可以通過在集合中添加Image =「」輕鬆解決。

1

嘗試使用

ObservableCollection ObserverableCollection<MapElement> MapItems = new ObservableCollection<MapElement>();

既然你期待的項目是「可更新在運行時」或反應幕後變化的ObservableCollection實現INPC

+0

這並沒有做任何的不同。這些物品還沒有顯示在地圖上。 – Startail