【问题标题】:How should I handle click events on pushpins in my Bing Maps control for WP7我应该如何在我的 WP7 必应地图控件中处理图钉上的点击事件
【发布时间】:2010-09-19 05:05:37
【问题描述】:

我即将在我的地图上的每个图钉内添加模板化的<Button> 控件,以便与单击(呃,触摸)图钉的用户进行交互。这是使用图钉的正确方法吗?我不想处理 MouseDown 和 MouseUp 并重新发明一切(没有人应该这样做)。

我只需要确认。

【问题讨论】:

    标签: windows-phone-7 bing-maps


    【解决方案1】:

    MouseLeftBUttonUp ?我只有模拟器,它适用于我的自定义图钉:

    <Maps:MapItemsControl ItemsSource="{Binding Stores}">
                    <Maps:MapItemsControl.ItemTemplate>
                        <DataTemplate>
                            <Maps:Pushpin Location="{Binding Location}" MouseLeftButtonUp="Pushpin_MouseLeftButtonUp">
                                <Maps:Pushpin.Template>
                                    <ControlTemplate TargetType="Maps:Pushpin">
                                        <Border BorderBrush="Black" BorderThickness="1" Background="MintCream" Width="32" Height="32" HorizontalAlignment="Center" VerticalAlignment="Center">
                                            <TextBlock Text="{Binding Store.Address}" FontWeight="Bold" HorizontalAlignment="Center" VerticalAlignment="Center"/>
                                        </Border>
                                    </ControlTemplate>
                                </Maps:Pushpin.Template>
                            </Maps:Pushpin>
                        </DataTemplate>
                    </Maps:MapItemsControl.ItemTemplate>
                </Maps:MapItemsControl>
    

    编辑:在获得真实设备后,我测试了我的应用程序,我可以确认 MouseLeftBUttonUp 是一个坏主意(微软在Performance tips 中不推荐)

    相反,您应该使用操纵事件:

    <Maps:MapItemsControl ItemsSource="{Binding Stores}">
    <Maps:MapItemsControl.ItemTemplate>
        <DataTemplate>
            <Maps:Pushpin Location="{Binding Location}" ManipulationStarted="Pushpin_ManipulationStarted">
                <Maps:Pushpin.Template>
                    <ControlTemplate TargetType="Maps:Pushpin">
                        <Image Width="48" Height="48" Source="{Binding InventoryIcon}" />
                    </ControlTemplate>
                </Maps:Pushpin.Template>
            </Maps:Pushpin>
        </DataTemplate>
    </Maps:MapItemsControl.ItemTemplate>
    </Maps:MapItemsControl>
    

    【讨论】:

    • 我建议使用 Invisible Button 而不是 Manipulation/MouseDown 是因为 Button.Click 是非常标准的 UX,程序员很容易处理它。只有当我有一个 MouseDown + MouseUp 这是一个 Click 时,我才需要触发我的事件 :)
    【解决方案2】:

    为什么不在 MapItemsControl.ItemTemplate 中添加一个不可见的样式按钮并使用单击按钮。

    【讨论】:

    • 是的,我的问题中有“
    • 当你有真实事件时添加一个不可见的按钮似乎没有必要
    【解决方案3】:

    如果您只想点击/点击每个图钉,请将 MouseLeftButtonUp 事件添加到您创建的每个图钉。例如:

    Microsoft.Phone.Controls.Maps.Pushpin pp = null;
    System.Device.Location.GeoCoordinate loc = null;
    
    pp = new Microsoft.Phone.Controls.Maps.Pushpin();
    loc = new System.Device.Location.GeoCoordinate([Latitude], [Longitude]);
    
    pp.Location = loc;
    pp.Content = "Some Content";
    pp.MouseLeftButtonUp += new MouseButtonEventHandler(Pushpin_MouseLeftButtonUp);
    

    然后你添加

    void Pushpin_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
    {
         Microsoft.Phone.Controls.Maps.Pushpin tempPP = new Microsoft.Phone.Controls.Maps.Pushpin();
    
         tempPP = (Microsoft.Phone.Controls.Maps.Pushpin)sender;
    
         // you can check the tempPP.Content property
    
    } 
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-08-04
      相关资源
      最近更新 更多