2016-07-23 97 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