如何在Windows Phone上显示DataBound图钉 [英] How to display a DataBound Pushpin on Windows Phone
问题描述
我正在尝试在Windows Phone上创建一个图钉,该图钉显示多个位置,包括名称,评分等.我已经在MSDN,Stack Overflow和其他站点上搜索了此问题的解决方案,但只找到了部分答案.
I am trying to create a pushpin on Windows Phone that shows a number of locations, including there name, rating and more. I have searched MSDN, Stack Overflow and other sites for a solution to this and found only partial answers.
这是我目前的尝试.它根本不起作用.它确实会创建一个图钉,但不会更改为显示网格或任何其他元素.相反,它在地图的左上方显示了一个空白图钉.
This is my current attempt. It doesn't work at all. It does create a pushpin, but it is not changed to show a grid or any of the other elements. Instead it shows a blank pushpin in the top left of the map.
<mi:Map x:Name="map" Margin="-12,0" d:LayoutOverrides="Width" CredentialsProvider="Something else goes here :)">
<mi:MapItemsControl ItemsSource="{Binding Pushpins}">
<mi:MapItemsControl.ItemTemplate>
<DataTemplate>
<mi:Pushpin Location="{Binding Location}">
<Grid Background="{Binding Background}">
<TextBlock Text="{Binding Name}" Margin="6,0"/>
<ProgressBar Maximum="5" Value="{Binding Rating}"/>
</Grid>
</mi:Pushpin>
</DataTemplate>
</mi:MapItemsControl.ItemTemplate>
</mi:MapItemsControl>
</mi:Map>
如何获取上面的代码或类似的代码?
How can I get the above code or something like it to work?
我以前的解决方案涉及使用代码创建元素,但问题是,它超出了几个别针而变得凌乱,并且无法轻松地用于获取其他信息(例如商店URL).
My previous solution involved creating the elements with code, the problem with that is that it got messy beyond a few pins and could not easily be used to get other information (such as a Stores URL).
有关一切背后代码的一些额外信息.
Some extra information on the code behind everything.
public struct PushpinModel
{
public GeoCoordinate Location { get; set; }
public string Name { get; set; }
public string Url { get; set; }
public float Rating { get; set; }
public Brush Background { get; set; }
}
这些图钉模型通过AddPin方法添加到图钉的可观察集合中:
These pushpin models are added to the Pushpin's observable collection with the AddPin method:
public void AddPin(GeoCoordinate coord, string Title, string url, float rating, Brush background)
{
Pushpins.Add(new PushpinModel { Location = coord, Name = Title, Rating = rating, Url = url, Background = background} );
}
当位置更改时,我的GeoCoordinateWatcher会调用watcher_PositionChanged事件.这样会在用户位置添加图钉.
My GeoCoordinateWatcher calls the watcher_PositionChanged event when it's position is changed. This adds a pushpin at the users location.
当前正在调用ZoomToUser()函数,但未显示图钉.
Currently the ZoomToUser() function is being called, but the pushpin is not appearing.
推荐答案
这看起来是错误的
<mi:MapItemsControl ItemsSource="Pushpins">
如果它是数据上下文的属性,则应为
if it is a property of the datacontext it should be
<mi:MapItemsControl ItemsSource="{Binding Pushpins}">
这篇关于如何在Windows Phone上显示DataBound图钉的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!