wp8:如何将地图插入到PanoramaItem HeaderTemplate中

时间:2014-03-25 10:03:15

标签: xaml data-binding windows-phone-8 map panorama-control

我试图将地图控件插入到我的应用程序中的PanoramaItem标题中:

<phone:PanoramaItem Orientation="Horizontal" Width="480">

            <phone:PanoramaItem.HeaderTemplate>
                <DataTemplate>
                    <StackPanel Height="155" Width="478" Margin="-23,0,0,0">
                        <!-- Map -->
                        <maps:Map x:Name="StationsMapOverview"
                                  Visibility="{Binding IsDataLoaded, Converter={StaticResource BooleanToVisibilityConverter}}"
                                  Height="115" 
                                  Margin="0,-34,0,0"
                                  ZoomLevel="10"
                                  Center="{Binding UserGeoCoordinate, Mode=TwoWay}"
                                  CartographicMode="Road"
                                  ColorMode="Light"
                                  PedestrianFeaturesEnabled="True"
                                  LandmarksEnabled="True"/>
                    </StackPanel>
                </DataTemplate>
            </phone:PanoramaItem.HeaderTemplate>

            <!-- Stations list -->
            <phone:LongListSelector x:Name="ListNearbyItems" 
                                        ItemsSource="{Binding StationItems}" Margin="0,-38,0,0" Height="480">
...

结果很好,我的地图看起来很好。 但在后面的代码中,我有以下错误:

name 'StationsMapOverview' does not exist in the current context

DatacontextViewModelLocator类设置,适用于其他页面)。 而且&#39; center&#39;选项绑定无效。

所以我的问题是,有人试图将地图集成到PanoramaItem Header吗?

1 个答案:

答案 0 :(得分:0)

要在标题模板内添加DataContext添加:

<phone:PanoramaItem Header={Binding} x:Name="panorama"

您无法按名称访问模板中的对象。使用下面的代码按名称查找元素:

private T FindElementInVisualTree<T>(DependencyObject parentElement, string name) where T : DependencyObject
{
    var count = VisualTreeHelper.GetChildrenCount(parentElement);
    if (count == 0)
        return null;

    for (int i = 0; i < count; i++)
    {
        var child = VisualTreeHelper.GetChild(parentElement, i);

        if (child != null && child is FrameworkElement && (child as FrameworkElement).Name.Equals(name))
        {
            return (T)child;
        }
        else
        {
            var result = FindElementInVisualTree<T>(child, name);
            if (result != null)
                return result;

        }
    }
    return null;
}

然后致电:

Map map = FindElementInVisualTree<Map>(panorama, "StationsMapOverview");