将 ObservableCollection 中的图钉绑定到 Bing 地图控件 WP7

发布于 2024-12-23 10:42:34 字数 4706 浏览 3 评论 0原文

我正在开发 Windows Phone 7 项目 (Mango),并尝试使用 ObservableCollection 将图钉绑定到我的 silverlight Bing Maps 控件,但它不起作用。在过去的 5 个小时里,我一直坐在这里,在 stackoverflow 上搜索互联网和其他问题,并实现了一些答案,但找不到一个有效的答案:(

如果有任何关于为什么它不起作用的想法,我将非常感激。我很确定它与我的 XAML 有关,因为我的 ObservableCollection 已使用有效位置正确填充(在运行时使用断点进行检查),目前我的 ObservableCollection 仅填充了两个位置,但我会这样做。当我开始使用 Bing RouteService 时,希望增加这个数字。

以下是代码:

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

下面是 PushpinModel 类:

using System.Device.Location;

namespace NavigationApp
{
    public class PushpinModel
    {
       public GeoCoordinate Location { get; set; }
    }
}

下面是有问题的 XAML(我认为!):

<phone:PhoneApplicationPage 
    x:Class="NavigationApp.MapView"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/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="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/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>

如果您需要更多代码/信息,请告诉我:) 谢谢 Ryan

解决方案

将 ObservableCollection 更改为:

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

XAML 现在是:

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

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="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/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="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/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}" >

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

番薯 2024-12-30 10:42:34

从您的代码看来,您忘记设置 DataContext 。您可以通过以下方式执行此操作:

public MapView()
{
    InitializeComponent();

    geoDestination = new GeoCoordinate(54.975556, -1.621667);
    geoCurrentLocation = new GeoCoordinate(53.463056, -2.291389);

    CreatePushpins();
    DataContext = this;
}

由于原因,您只能绑定到属性。所以这不起作用:

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

XAML:

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

使用包装器属性:

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

public CredentialsProvider BingMapsCredentials 
{ 
     get { return bingMapsCredentials; } 
}

XAML:

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

有一个关于 MSDN 上的 DataBinding (这是关于 WPF 的,但大部分也适用于 WP7)

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)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文