【问题标题】:Set Bing Maps Center in Windows 8 Store App using XAML C#使用 XAML C# 在 Windows 8 Store App 中设置 Bing 地图中心
【发布时间】:2013-10-06 20:14:42
【问题描述】:

我正在尝试使用 XAML/C# 在 Windows 8 商店应用程序中的 Bing 地图控件上设置中心位置。我遇到了一些文章,解释说您不能在 XAML 中为 center 属性使用绑定,所以我试图在 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 控件内的 Bing 地图控件 (bm:Map),因此我可以设置中心。我已经使用 VisualTree Helpers 从 codeplex 尝试了 WinRT XAML Toolkit,但没有成功。

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

【问题讨论】:

    标签: c# xaml windows-8 winrt-xaml


    【解决方案1】:

    如果你只打算改变一次值,你可以注册加载地图:

       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);
        }
    

    【讨论】:

    • 我使用了依赖属性,因为我需要根据 Flipview 使用的所选项目多次更改它。
    猜你喜欢
    • 2013-03-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-11-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多