在的ObservableCollection绑定图钉到Bing地图控制WP7

编程入门 行业动态 更新时间:2024-10-03 19:20:50
本文介绍了在的ObservableCollection绑定图钉到Bing地图控制WP7的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我工作的一款Windows Phone 7项目(芒果),我试图用一个ObservableCollection绑定图钉我的Silverlight的Bing地图控件,但它不会工作。我一直坐在这里在过去的5个小时,淘计算器上的互联网和其他问题,并已经采取了一些答案,但无法找到其中一个工作:(

我会很感激的任何想法,为什么它不工作。我是pretty的确定它与我的XAML作为我的ObservableCollection正确填入有效的位置(在运行时使用断点检查)。眼下我的ObservableCollection只填充了两个位置,但是我会找我的时候开始使用必应RouteService增加这个数量。

下面是code:

公共部分类的MapView:PhoneApplicationPage {     私人只读CredentialsProvider bingMapsCredentials =新ApplicationIdCredentialsProvider(App.BingMapsKey);     私人GeoCoordinate geoDestination;     私人GeoCoordinate geoCurrentLocation;     公众的ObservableCollection< PushpinModel> PushpinCollection {获得;组; }     公共图形页面()     {         的InitializeComponent();         geoDestination =新GeoCoordinate(54.975556,-1.621667);         geoCurrentLocation =新GeoCoordinate(53.463056,-2.291389);         CreatePushpins();     }     私人无效CreatePushpins()     {         PushpinModel currentLocationModel =新PushpinModel();         PushpinModel destinationLocationModel =新PushpinModel();         currentLocationModel.Location = geoCurrentLocation;         destinationLocationModel.Location = geoDestination;         PushpinCollection =新的ObservableCollection< PushpinModel>();         PushpinCollection.Add(currentLocationModel);         PushpinCollection.Add(destinationLocationModel);     }

下面是PushpinModel类:

使用System.Device.Location; 命名空间NavigationApp {     公共类PushpinModel     {        公共GeoCoordinate位置{获得;组; }     } }

和下面是有问题的XAML(我想!):

<电话:PhoneApplicationPage     X:类=NavigationApp.MapView     的xmlns =htt​​p://schemas.microsoft/winfx/2006/xaml/$p$psentation     的xmlns:X =htt​​p://schemas.microsoft/winfx/2006/xaml     XMLNS:手机=CLR的命名空间:Microsoft.P​​hone.Controls;装配= Microsoft.P​​hone     的xmlns:SHELL =CLR的命名空间:Microsoft.P​​hone.Shell;装配= Microsoft.P​​hone     的xmlns:D =htt​​p://schemas.microsoft/ex$p$pssion/blend/2008     的xmlns:MC =htt​​p://schemas.openxmlformats/markup-compatibility/2006     XMLNS:地方=CLR的命名空间:NavigationApp     的FontFamily ={的StaticResource PhoneFontFamilyNormal}     字号={的StaticResource PhoneFontSizeNormal}     前景={的StaticResource PhoneForegroundBrush}     SupportedOrientations =纵向方向=纵向     MC:可忽略=DD:DesignHeight =768D:DesignWidth =480     外壳:SystemTray.IsVisible =真     的xmlns:我=CLR的命名空间:Microsoft.P​​hone.Controls.Maps;装配= Microsoft.P​​hone.Controls.Maps>     <电话:PhoneApplicationPage.Resources>         <地方:PushpinModel X:关键=PushpinModel/>         < D​​ataTemplate中X:关键=LogoTemplate>             <我:图钉位置={结合位置}背景=#FFB6DE2E/>         < / DataTemplate中>     < /电话:PhoneApplicationPage.Resources>     <! - LayoutRoot是根格,所有页面内容被置于 - >     <电网X:名称=LayoutRoot背景=透明>         < Grid.RowDefinitions>             &所述; RowDefinition高度=0/>             < RowDefinition高度=768 */>         < /Grid.RowDefinitions>         <! - TitlePanel包含的应用程序和页面标题的名字 - >         &所述; StackPanel中X:名称=TitlePanelGrid.Row =0保证金=12,17,0,28>&所述; / StackPanel的>         <! - 的ContentPanel - 地方更多的内容在这里 - >         &所述;网格X:名称=的ContentPanelGrid.Row =1>             <我:地图高度=520的Horizo​​ntalAlignment =左保证金=6,6,0,0NAME =Map1的VerticalAlignment =热门WIDTH =468ZoomBarVisibility =坍塌缩放级别=1 CredentialsProvider ={结合bingMapsCredentials}>                 <我:MapItemsControl名称=Pushpinsss的ItemTemplate ={的StaticResource LogoTemplate}的ItemsSource ={结合PushpinCollection}>                  < /我:MapItemsControl>             < /我:地图>         < /网格>     < /网格> < /电话:PhoneApplicationPage>

如果您需要再code /信息,然后才让我知道:) 谢谢 瑞安

解决方案的

改的ObservableCollection为:

私人的ObservableCollection< PushpinModel> PushpinCollection;     公众的ObservableCollection< PushpinModel> pushpinCollection     {         得到         {             返回PushpinCollection;         }     }

和XAML现在是:

<我:MapItemsControl名称=Pushpinsss的ItemTemplate ={的StaticResource LogoTemplate}的ItemsSource ={结合pushpinCollection}>

解决方案

从你的code它接缝你忘了设置的 DataContext的。你可以这样做有:

公共图形页面() {     的InitializeComponent();     geoDestination =新GeoCoordinate(54.975556,-1.621667);     geoCurrentLocation =新GeoCoordinate(53.463056,-2.291389);     CreatePushpins();     的DataContext =这一点; }

到了,为什么,你只能绑定属性。所以这是行不通的:

私人只读CredentialsProvider bingMapsCredentials =     新ApplicationIdCredentialsProvider(App.BingMapsKey);

XAML:

<我:地图... CredentialsProvider ={结合bingMapsCredentials}... />

使用的封装属性格式,而不是:

私人只读CredentialsProvider bingMapsCredentials =     新ApplicationIdCredentialsProvider(App.BingMapsKey); 公共CredentialsProvider BingMapsCredentials {      {返回bingMapsCredentials; } }

XAML:

<我:地图... CredentialsProvider ={结合BingMapsCredentials}... />

有一个关于 rel="nofollow">数据绑定(它是关于WPF,但在大多数情况下也适用于WP7)

I'm working on a Windows Phone 7 Project (Mango) and am trying to bind Pushpins to my silverlight Bing Maps control using an ObservableCollection, but it wont work. I've been sat here for the last 5 hours, scouring the internet and other questions on stackoverflow and have implemented some answers but can't find one which works :(

I would be very grateful for any ideas as to why its not working. I'm pretty sure its to do with my XAML as my ObservableCollection is populated correctly (checked at runtime using breakpoints) with valid Locations. For the moment my ObservableCollection is only populated with two locations, however I will be looking to increase this number when I start using the Bing RouteService.

Here is the code:

public partial class MapView : PhoneApplicationPage { private readonly CredentialsProvider bingMapsCredentials = new ApplicationIdCredentialsProvider(App.BingMapsKey); private GeoCoordinate geoDestination; private GeoCoordinate geoCurrentLocation; public ObservableCollection<PushpinModel> PushpinCollection { get; set; } public MapView() { InitializeComponent(); geoDestination = new GeoCoordinate(54.975556, -1.621667); geoCurrentLocation = new GeoCoordinate(53.463056, -2.291389); CreatePushpins(); } private void CreatePushpins() { PushpinModel currentLocationModel = new PushpinModel(); PushpinModel destinationLocationModel = new PushpinModel(); currentLocationModel.Location = geoCurrentLocation; destinationLocationModel.Location = geoDestination; PushpinCollection = new ObservableCollection<PushpinModel>(); PushpinCollection.Add(currentLocationModel); PushpinCollection.Add(destinationLocationModel); }

Below is the PushpinModel Class:

using System.Device.Location; namespace NavigationApp { public class PushpinModel { public GeoCoordinate Location { get; set; } } }

And below is the offending XAML (I think!):

<phone:PhoneApplicationPage x:Class="NavigationApp.MapView" xmlns="schemas.microsoft/winfx/2006/xaml/presentation" xmlns:x="schemas.microsoft/winfx/2006/xaml" xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone" xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone" xmlns:d="schemas.microsoft/expression/blend/2008" xmlns:mc="schemas.openxmlformats/markup-compatibility/2006" xmlns:local="clr-namespace:NavigationApp" FontFamily="{StaticResource PhoneFontFamilyNormal}" FontSize="{StaticResource PhoneFontSizeNormal}" Foreground="{StaticResource PhoneForegroundBrush}" SupportedOrientations="Portrait" Orientation="Portrait" mc:Ignorable="d" d:DesignHeight="768" d:DesignWidth="480" shell:SystemTray.IsVisible="True" xmlns:my="clr-namespace:Microsoft.Phone.Controls.Maps;assembly=Microsoft.Phone.Controls.Maps"> <phone:PhoneApplicationPage.Resources> <local:PushpinModel x:Key="PushpinModel" /> <DataTemplate x:Key="LogoTemplate"> <my:Pushpin Location="{Binding Location}" Background="#FFB6DE2E" /> </DataTemplate> </phone:PhoneApplicationPage.Resources> <!--LayoutRoot is the root grid where all page content is placed--> <Grid x:Name="LayoutRoot" Background="Transparent"> <Grid.RowDefinitions> <RowDefinition Height="0"/> <RowDefinition Height="768*"/> </Grid.RowDefinitions> <!--TitlePanel contains the name of the application and page title--> <StackPanel x:Name="TitlePanel" Grid.Row="0" Margin="12,17,0,28"></StackPanel> <!--ContentPanel - place additional content here--> <Grid x:Name="ContentPanel" Grid.Row="1"> <my:Map Height="520" HorizontalAlignment="Left" Margin="6,6,0,0" Name="map1" VerticalAlignment="Top" Width="468" ZoomBarVisibility="Collapsed" ZoomLevel="1" CredentialsProvider="{Binding bingMapsCredentials}" > <my:MapItemsControl Name="Pushpinsss" ItemTemplate="{StaticResource LogoTemplate}" ItemsSource="{Binding PushpinCollection}" > </my:MapItemsControl> </my:Map> </Grid> </Grid> </phone:PhoneApplicationPage>

If you need anymore Code/Info then just let me know :) Thanks Ryan

Solution

Changed ObservableCollection to:

private ObservableCollection<PushpinModel> PushpinCollection; public ObservableCollection<PushpinModel> pushpinCollection { get { return PushpinCollection; } }

And XAML is now:

<my:MapItemsControl Name="Pushpinsss" ItemTemplate="{StaticResource LogoTemplate}" ItemsSource="{Binding pushpinCollection}" >

解决方案

From your code it seams you forgot to set the DataContext. You can do this with:

public MapView() { InitializeComponent(); geoDestination = new GeoCoordinate(54.975556, -1.621667); geoCurrentLocation = new GeoCoordinate(53.463056, -2.291389); CreatePushpins(); DataContext = this; }

By the why, you can only bind to properties. So this won't work:

private readonly CredentialsProvider bingMapsCredentials = new ApplicationIdCredentialsProvider(App.BingMapsKey);

XAML:

<my:Map ... CredentialsProvider="{Binding bingMapsCredentials}" ... />

Use a wrapper propery instead:

private readonly CredentialsProvider bingMapsCredentials = new ApplicationIdCredentialsProvider(App.BingMapsKey); public CredentialsProvider BingMapsCredentials { get { return bingMapsCredentials; } }

XAML:

<my:Map ... CredentialsProvider="{Binding BingMapsCredentials}" ... />

There is a good overview about DataBinding on MSDN (it's about WPF but the most part also applies for WP7)

更多推荐

在的ObservableCollection绑定图钉到Bing地图控制WP7

本文发布于:2023-07-09 10:15:12,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1085577.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:图钉   绑定   地图   ObservableCollection   Bing

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!