【问题标题】:wp8 : how to insert map into PanoramaItem HeaderTemplatewp8:如何将地图插入 PanoramaItem HeaderTemplate
【发布时间】:2014-03-25 10:03:15
【问题描述】:

我正在尝试在我的应用程序中将地图控件插入到 PanoramaItem Header 中:

<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 类设置,适用于其他页面)。 并且 'center' 选项绑定不起作用。

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

【问题讨论】:

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


    【解决方案1】:

    在标题模板中传递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");
    

    【讨论】:

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