单击图钉并在Windows Phone 8中显示该项目的更多详细信息

时间:2014-03-15 07:39:16

标签: windows-phone-8 pushpin

我正在为Windows Phone 8开发一个简单的地图应用程序。我使用windows phone toolkit设置了多个图钉。我想在点击图钉项目时显示更多详细信息。 这是我的代码。

    <Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
        <maps:Map Name="MyMap"
                  CartographicMode="Road" ColorMode="Light"
                  LandmarksEnabled="True" PedestrianFeaturesEnabled="True">

            <toolkit:MapExtensions.Children>                    
                <toolkit:MapItemsControl Name="allDatas">
                    <toolkit:MapItemsControl.ItemTemplate>
                        <DataTemplate>
                            <toolkit:Pushpin GeoCoordinate="{Binding Coordinate}" 
                                             Content="{Binding Name}"            
                                             Background="Green"
                                             Foreground="Black"
                                             Tap="Pushpin_Tap"/>
                        </DataTemplate>
                    </toolkit:MapItemsControl.ItemTemplate>
                </toolkit:MapItemsControl>            
            </toolkit:MapExtensions.Children>

        </maps:Map>
    </Grid> 

    void MainPage_Loaded(object sender, RoutedEventArgs e)
    {
        ObservableCollection<Data> datas = new ObservableCollection<Data>() 
        {
            new Data() { Coordinate = new GeoCoordinate(22.832991,89.539921), Name = "H", Details = "Hospital", Address = "Address of Hospital" },
            new Data() { Coordinate = new GeoCoordinate(22.845489,89.539406), Name = "P", Details = "Fire Station", Address = "Address of Fire"},
            new Data() { Coordinate = new GeoCoordinate(22.818019,89.54563), Name = "F", Details = "Police Station", Address = "Address of Police"}                       
        };

        ObservableCollection<DependencyObject> children = MapExtensions.GetChildren(MyMap);        
        var obj = children.FirstOrDefault(x => x.GetType() == typeof(MapItemsControl)) as MapItemsControl;  
        obj.ItemsSource = datas;    
    }

    private void Pushpin_Tap(object sender, System.Windows.Input.GestureEventArgs e)
    {
        Pushpin pushpin = sender as Pushpin;

        if (pushpin.Content != null)
        {   //Here i want to show details
            MessageBox.Show(pushpin.Content.ToString());
        }
    }

2 个答案:

答案 0 :(得分:0)

你可以这样做:

pushpin.Tap += delegate
{
     if (AppSettings["PushpinOpen"] == true)
     {
           pushpin.Content = attraction.Content;
           AppSettings["PushpinOpen"] = attraction.Title;
           AppSettings.Save();
      }
      else
      {
           pushpin.Content = attraction.Title;
           AppSettings.Remove("PushpinOpen");
       }

  };

您不必使用IsolatedStorageSettings(在这种情况下为AppSettings)。您只需要检查图钉是否已经打开,如果没有,您可以将图钉的内容更改为更详细的信息。

答案 1 :(得分:0)

private void Pushpin_Tap(object sender, System.Windows.Input.GestureEventArgs e)
{
var element = (FrameworkElement)sender;
Data data = (Data)element.DataContext;
MessageBox.Show(data.Address);
}

可能有点迟了,但我试图达到同样的目的。显示有关图钉Tap的更多数据。 我找到了一个有效的答案here,这是对一个不同问题的答案。