wp8:如何将地图插入到PanoramaItem HeaderTemplate中 [英] wp8 : how to insert map into PanoramaItem HeaderTemplate

查看:168
本文介绍了wp8:如何将地图插入到PanoramaItem HeaderTemplate中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 < phone:PanoramaItem Orientation =水平Width =480> 

< phone:PanoramaItem.HeaderTemplate>
< DataTemplate>
< StackPanel Height =155Width =478Margin = - 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
标志符号Enabled =True/>
< / StackPanel>
< / DataTemplate>
< / phone:PanoramaItem.HeaderTemplate>

<! - 车站列表 - >
< phone:LongListSelector x:Name =ListNearbyItems
ItemsSource ={Binding StationItems}Margin =0,-38,0,0Height =480>
...

结果很好,我的地图显示得很好。
但是在后面的代码中,我有以下错误:

  name'StationsMapOverview'不存在于当前上下文

Datacontext ViewModelLocator 类,并为其他页面工作正常)。
和'center'选项绑定不起作用。



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

解决方案

通过 DataContext 内部标题模板add:

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

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

  private T FindElementInVisualTree&T;(DependencyObject parentElement,string name)其中T:DependencyObject 
{
var count = VisualTreeHelper.GetChildrenCount(parentElement);
if(count == 0)
返回null; (int i = 0; i< count; i ++)
{
var child = VisualTreeHelper.GetChild(parentElement,i);



if(child!= null&& child是FrameworkElement&&(child as FrameworkElement).Name.Equals(name))
{
return )儿童;
}
else
{
var result = FindElementInVisualTree< T>(child,name);
if(result!= null)
返回结果;

}
}
返回null;
}

然后拨打:

  Map map = FindElementInVisualTree< Map>(全景图,StationsMapOverview); 


I'm trying to insert map control into PanoramaItem Header in my App :

<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">
...

The result is good and my map appears well. But in the behind code, I have the following error:

name 'StationsMapOverview' does not exist in the current context

(Datacontext is set by ViewModelLocator class and work fine for others pages). And 'center' option binding is not working.

So my question is, is that someone has tried to integrate a map into PanoramaItem Header ?

解决方案

To pass DataContext inside header template add:

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

You can't access an object in template by name. Use code below to find element by name:

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

And then call:

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

这篇关于wp8:如何将地图插入到PanoramaItem HeaderTemplate中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆