[英]MapControl 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>
这并不完美,因为这不会在地图上为您提供图标,而只会显示文本。 但是可以通过在Collection中添加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;
}
Map_Loaded函数作为exepcted触发。 我遇到的麻烦是,如果这是一个普通项目,则将数据直接设置为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!"
}
};
我必须添加一个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>
这并不完美,因为这不会在地图上为您提供图标,而只会显示文本。 但是可以通过在Collection中添加Image =“”来轻松解决。
尝试使用
ObservableCollection ObserverableCollection<MapElement> MapItems = new ObservableCollection<MapElement>();
由于您希望该项目“在运行时可更新”或对幕后的ObservableCollection更改做出反应,因此实现了INPC
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.