C# 使用XAML C在Windows 8应用商店应用程序中设置Bing地图中心
我正在尝试使用XAML/C在Windows 8应用商店应用程序中的Bing Maps控件上设置中心位置。我遇到一些文章解释说,不能在XAML中为中心属性使用绑定,因此我正在尝试在C中设置它。我在studio中使用默认的Windows 8应用商店应用程序网格模板C# 使用XAML C在Windows 8应用商店应用程序中设置Bing地图中心,c#,xaml,windows-8,winrt-xaml,C#,Xaml,Windows 8,Winrt Xaml,我正在尝试使用XAML/C在Windows 8应用商店应用程序中的Bing Maps控件上设置中心位置。我遇到一些文章解释说,不能在XAML中为中心属性使用绑定,因此我正在尝试在C中设置它。我在studio中使用默认的Windows 8应用商店应用程序网格模板 <FlipView x:Name="flipView" AutomationProperties.AutomationId="ItemsFlipView" AutomationPro
<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助手尝试了codeplex中的WinRT XAML工具包,但没有成功
任何帮助都将不胜感激。Thx.如果您只想更改一次值,您可以注册地图的加载:
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);
}
我使用了dependency属性,因为我需要根据flipview使用的选定项对其进行多次更改。