问题描述
我在WP7应用程序中要求从多个数据源在bing映射上显示图钉,但我不确定做到这一点的最佳方法.
I have the requirement in my WP7 application to display pushpins on a bing map from multiple data sources and I am not sure of the best way to do this.
例如,可能是这样,我从Web服务接收到人员列表,建筑物列表,POI列表等.我需要在他们自己的视图中分别显示它们,但还要显示它们在每种类型具有不同图像的地图上.
So for example it would be something like this, I receive from a web service a list of people, a list of buildings, a list of POIs etc. I would need to display these individually in their own views but also display them on a map with different images for each type.
我正在尝试使用MVVM方法,因此有一个用于Person的类,一个用于建筑物的类,依此类推,每个类都有一个位置.然后,我为每种类型都有一个ObservableCollection,因此使用数据绑定很容易为每种类型进行View.
I am trying to use a MVVM approach so have a class for a Person, class for a building and so on, each one of these has a location. I then have an ObservableCollection for each of these types and so using data binding it is easy enough to do a View for each of these.
目前,我只有一个ViewModel,但我首先想到的是,我认为每个类型实际上应该有一个ViewModel.那么PersonViewModel,BuildingViewModel在这里?但是,地图视图随后需要从每个视图中获取信息,而且我不确定如何将一个视图绑定到这样的多个ViewModel上,即使这是明智的.
At the moment I only have one ViewModel but my first thought is that I think I should really have one ViewModel per type. So PersonViewModel, BuildingViewModel here? However a Map View would then need to take information from each of these views and I am no sure how you bind a view to multiple ViewModels like this or even if that is sensible.
MapItemsControl似乎也只能绑定到一件事,所以我如何将其绑定到多个不同的数据源呢?我以为我可以创建一个简单的图钉类,但是那意味着要复制每种类型的数据,我最终希望能够单击图钉以显示图钉的详细信息,因此希望将类型分开
MapItemsControl also only seems to be able to bind to one thing so how can I bind it to multiple different data sources like this? I thought I could create a simple pushpin class but that would then mean having the data for each type duplicated and I eventually want to be able to click on the pushpins to display the details for the pin so wanted to keep the types separate
对前进道路上的任何指针都表示赞赏
Any pointers on a way forward much appreciated
推荐答案
使用MapItemsControl
是您所说的方式.这很容易.正如您所说的,您想要的是一个ViewModel,其中每个类型都有一个ObservableCollection.
Using a MapItemsControl
is as you say, the way to do it. And it's very easy. What you want is a ViewModel with a ObservableCollection for each of your types, as you say you already have.
所以我想您的问题是您不了解如何使用样式和ItemTemplates.
So I guess your problem is that you don't understand how to use Styles and ItemTemplates.
如果要对每种不同的类型使用相同的样式,则只需定义一个适用于每个MapItemsControl.ItemTemplate
的公共ItemTemplate
.同样,如果需要的话,您可以定义一种自定义样式,将其全局应用到每个图钉.
If what you want, is to have the same style for each of the different types, then simply define a common ItemTemplate
that you apply to each MapItemsControl.ItemTemplate
. Likewise you can define a custom style that you apply to each Pushpin globally, if what's what you want.
<Grid.Resources>
<DataTemplate x:Name="PushpinItemTemplate">
<maps:Pushpin Location="{Binding Location}" Tap="Pushpin_Tap" Style="{StaticResource PushpinStyle}" />
</DataTemplate>
</Grid.Resources>
...
<maps:Map>
<maps:MapLayer>
<maps:MapItemsControl ItemsSource="{Binding People}" ItemTemplate="{StaticResource PushpinItemTemplate}" />
<maps:MapItemsControl ItemsSource="{Binding Buildings}" ItemTemplate="{StaticResource PushpinItemTemplate}" />
</maps:MapLayer>
</maps:Map>
这篇关于如何绑定来自多个模型的Bing图钉?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!