使用 XAML C# 在 Windows 8 应用商店应用中设置必应地图中心

本文关键字:应用 设置 地图 XAML Windows 使用 | 更新日期: 2023-09-27 18:34:12

我正在尝试使用 XAML/C# 在 Windows 8 应用商店应用程序中的 Bing 地图控件上设置中心位置。 我遇到了一些文章,解释说您不能在 XAML 中对中心属性使用绑定,因此我尝试在 C# 中设置它。 我在工作室中使用默认的 Windows 8 应用商店应用程序网格模板。

 <FlipView
        x:Name="flipView"
        AutomationProperties.AutomationId="ItemsFlipView"
        AutomationProperties.Name="Item Details"
        TabIndex="1"
        Grid.RowSpan="2"
        ItemsSource="{Binding Source={StaticResource itemsViewSource}}">
        <FlipView.ItemContainerStyle>
            <Style TargetType="FlipViewItem">
                <Setter Property="Margin" Value="0,137,0,0"/>
            </Style>
        </FlipView.ItemContainerStyle>
        <FlipView.ItemTemplate>
            <DataTemplate>
                <!--
                    UserControl chosen as the templated item because it supports visual state management
                    Loaded/unloaded events explicitly subscribe to view state updates from the page
                -->
                <UserControl Loaded="StartLayoutUpdates" Unloaded="StopLayoutUpdates">
                    <ScrollViewer x:Name="scrollViewer" Style="{StaticResource HorizontalScrollViewerStyle}" Grid.Row="1">
                        <!-- Content is allowed to flow across as many columns as needed -->
                        <common:RichTextColumns x:Name="richTextColumns" Margin="117,0,117,47">
                            <RichTextBlock x:Name="richTextBlock" Width="560" Style="{StaticResource ItemRichTextStyle}" IsTextSelectionEnabled="False">
                                <Paragraph>
                                    <Run FontSize="26.667" FontWeight="Medium" Text="{Binding Title}"/>
                                    <LineBreak/>
                                    <Run FontSize="16.667" FontWeight="Light" Text="{Binding EventStartDate,Converter={StaticResource StringConverter},ConverterParameter='{}{0:D}'}"/>
                                </Paragraph>
                                <Paragraph LineStackingStrategy="MaxHeight">
                                    <InlineUIContainer>
                                        <!--<Image x:Name="image" MaxHeight="480" Margin="0,20,0,10" Stretch="Uniform" Source="{Binding Image}" AutomationProperties.Name="{Binding Title}"/>-->
                                        <bm:Map Credentials="{StaticResource BingMapsApiKey}" Height="500" Width="560" Margin="0,20,0,10" 
                                                ZoomLevel="10" x:Name="myMap">
                                            <bm:Map.Children>
                                                <bm:Pushpin Background="Red">
                                                    <bm:MapLayer.Position>
                                                        <bm:Location Latitude="{Binding Place.latitude}" Longitude="{Binding Place.longitude}" />
                                                    </bm:MapLayer.Position>
                                                </bm:Pushpin>
                                            </bm:Map.Children>
                                        </bm:Map>
                                    </InlineUIContainer>
                                </Paragraph>
                                <Paragraph>
                                    <Run FontSize="18.667" FontWeight="Normal" Text="{Binding Place.placeName}"/>
                                    <LineBreak/>
                                    <Run FontSize="16.667" FontWeight="Light" Text="{Binding Place.addressLine1Txt}"/>
                                    <LineBreak/>
                                    <Run FontSize="16.667" FontWeight="Light" Text="{Binding Place.cityName}"/>
                                    <Run FontSize="16.667" FontWeight="Light" Text="{Binding Place.stateProvinceCode}"/>
                                    <Run FontSize="16.667" FontWeight="Light" Text="{Binding Place.postalCode}"/>
                                    <LineBreak/>
                                    <LineBreak/>
                                    <InlineUIContainer>
                                        <HyperlinkButton Margin="-15,0,0,0"  NavigateUri="{Binding HomePageUrl}" Content="{Binding HomePageUrl}" />
                                    </InlineUIContainer>
                                    <LineBreak/>
                                    <Run FontWeight="SemiLight" Text="{Binding Content}"/>
                                </Paragraph>
                            </RichTextBlock>

我无法找到 FlipView 控件内的必应地图控件 (bm:Map),以便我可以设置中心。 我已经使用可视化树助手从代码复合体尝试了WinRT XAML工具包,但没有奏效。

任何帮助将不胜感激。 谢谢。

使用 XAML C# 在 Windows 8 应用商店应用中设置必应地图中心

如果您只打算更改一次值,则可以注册加载 Map :

   private void Map_OnLoaded(object sender, RoutedEventArgs e)
    {
        Map map = sender as Map;
        Item item=map.DataContext as Item;
        map.Center = item.Center;
    }

如果您需要多次更改它,您可以使用附加属性,如下所示的内容应该有效:

public static readonly DependencyProperty MapCenterProperty =
        DependencyProperty.RegisterAttached("MapCenter", typeof (Location), typeof (MyAttached), 
        new PropertyMetadata(default(Location),MapCenterChanged));
    private static void MapCenterChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        Map map = d as Map;
        Location location = e.NewValue as Location;
        if (map != null &&location!=null)
        {
            map.Center = location;
        }
    }

    public static void SetMapCenter(UIElement element, Location value)
    {
        element.SetValue(MapCenterProperty, value);
    }
    public static Location GetMapCenter(UIElement element)
    {
        return (Location) element.GetValue(MapCenterProperty);
    }