【问题标题】:Windows Phone 8.1 WinRT set Button location on MapControlWindows Phone 8.1 WinRT 在 MapControl 上设置按钮位置
【发布时间】:2015-11-09 19:27:21
【问题描述】:

我想在我的 WP 8.1 应用程序中设置 MapControl 上的按钮。问题是按钮不在元素的位置,仅在地图的左上角并且它正在移动。绑定中的位置是 Geopoint。这是我的代码:

<Maps:MapControl x:Name="MapEvent" Grid.Row="1">

        <Maps:MapItemsControl ItemsSource="{Binding}">
            <Maps:MapItemsControl.ItemTemplate>
                <DataTemplate>
                    <StackPanel>
                        <Button Maps:MapControl.Location="{Binding Location}" 
                                Maps:MapControl.NormalizedAnchorPoint="0.5,0.5" 
    Content="{Binding Name}"/>
                    </StackPanel>
                </DataTemplate>
            </Maps:MapItemsControl.ItemTemplate>
        </Maps:MapItemsControl>

    </Maps:MapControl>

【问题讨论】:

  • 1) 你有一个按钮,还是多个按钮 = 每个 DataItem 1 个按钮?您有 WinRT WP 8.1 应用程序还是 Silverlight WP8.1 应用程序 - 换句话说,您使用的是 Windows.UI.Xaml.Maps.MapControl 还是 Microsoft.Phone.Maps.Controls.Map ?
  • @EmmanuelDURIN 我在 Observablecollection 中有很多按钮。它的作品和按钮出现,但它们不在地图上的位置。我使用 Windows.UI.Xaml.Controls.Maps.MapControl 和 WinRT。

标签: c# xaml windows-phone-8.1 bing-maps


【解决方案1】:

这里是一些对我有用的代码的摘录:

public class PhotoInfo
{
    public String Label { get; set; }
    public String FileName { get; set; }
    public Geocoordinate Coordinate { get; set; }
    public Point NormalizedAnchorPoint { get { return new Point(0.5, 1); } }
}

我只是提醒大家不要绑定Windows.Devices.Geolocation.Geocoordinate,而是绑定Windows.Devices.Geolocation.Geopoint

这就是m:MapControl.Location 的绑定在Coordinate.Point 上的原因

<m:MapControl ZoomLevel="{Binding ZoomLevel}" Center="{Binding Center}"  MapServiceToken="xxx">
    <m:MapItemsControl ItemsSource="{Binding PhotoInfos}">
        <m:MapItemsControl.ItemTemplate>
            <DataTemplate>
                <Image m:MapControl.Location="{Binding Coordinate.Point}" 
                                       Source="ms-appx:///Assets/pushpin.png" 
                               m:MapControl.NormalizedAnchorPoint="{Binding NormalizedAnchorPoint}" Width="20" Height="45" Tapped="Image_Tapped"/> 
            </DataTemplate>
        </m:MapItemsControl.ItemTemplate>
    </m:MapItemsControl>
</m:MapControl>

【讨论】:

  • 对不起,我解释得不好。此按钮表示地图上 Observablecollection 中的对象。每个对象都有一个在地图上设置的位置。此代码的结果是按钮在地图上。他们有内容名称,但不在他们的位置上,他们“挂”在地图上。位置不为空等。
  • 我需要确定 ObservableCollection 是 MapControl 的 DataContext。但是,如果 Collection 是 ViewModel 中对象的属性,那就更好了。例如,该属性将称为“位置”,您将编写
  • 所以在我的视图模型中我有 ObservableCollection。我在 App.xaml.cs 中创建了 vm 的静态对象。在 onNavigatedTo 方法的视图中,我将数据从 json 加载到 Observablecollection 并且我有: this.DataContext = "my_OC";
  • @Quiet,所以在我看来,你的绑定看起来不错。我只是完全改变了我的答案,因为我对可能是你的问题有了另一个想法
  • 我添加了 Maps:MapControl.Location="{Binding Location.Point } 但仍然是一样的。我看到你有 Geocoordinate 类型。应该是那个?我不知道是什么创建了这个类型.
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-04-03
  • 1970-01-01
  • 2014-08-20
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多