简体   繁体   English

打开bing地图以绑定坐标

[英]Open bing maps to binded coordinates

So, I set the data context when I navigate to the page where the bing maps should show the right location like this: 因此,当我导航到bing映射应显示正确位置的页面时,将设置数据上下文,如下所示:

        protected override void OnNavigatedTo(NavigationEventArgs e)
    {
        var k = (app.MainPage.Country)PhoneApplicationService.Current.State["country"];
        DataContext = k;
    }

And I set the location in the xaml: 然后在xaml中设置位置:

        <Grid x:Name="ContentPanel" Grid.Row="1" Margin="0,0,0,0">
        <maps:Map x:Name="MyMap" Center="{Binding BingCoordinates}" ZoomLevel="10"/>
    </Grid>

BingCoordinates are like this: 41.333333,19.800000 必应坐标是这样的:41.333333,19.800000

But it isn't working. 但这不起作用。 It opens a map and points to congo, lol. 它打开地图,指向刚果,哈哈。 If I change the code to this: 如果我将代码更改为此:

        <Grid x:Name="ContentPanel" Grid.Row="1" Margin="0,0,0,0">
        <maps:Map x:Name="MyMap" Center="41.333333,19.800000" ZoomLevel="10"/>
    </Grid>

It works, why doesn't it work if I bind the coordinates ? 它有效,如果我绑定坐标,为什么不起作用? I could do this with C#, but I don't know how to bind the coordinates like this: 我可以使用C#来做到这一点,但是我不知道如何绑定这样的坐标:

mapsTask.Center = new GeoCoordinate(41.333333,19.800000);

Thanks for the help:) 谢谢您的帮助:)

As already stated in some comments, the reason for your binding to fail is that the assigned type is not the expected one (GeoCoordinate). 如某些注释中所述,绑定失败的原因是分配的类型不是预期的类型(GeoCoordinate)。

To solve this, I can think of two ways. 为了解决这个问题,我可以想到两种方法。 Which one actually is more fitting depends on thoughts about seperation of concern. 哪一个实际上更合适取决于对关注分离的想法。

  1. Changing the data source to provide a object that you can bind to. 更改数据源以提供可以绑定到的对象。 In your viewmodel (data context) you have a property called "BingCoordinates". 在视图模型(数据上下文)中,您具有一个名为“ BingCoordinates”的属性。 Right now that Property seems to be of type string (from what I understood). 现在,该属性似乎是字符串类型(据我了解)。 Changing its type to GeoCoordinate would solve your problem. 将其类型更改为GeoCoordinate可以解决您的问题。 All you have to do is parsing the string into a GeoCoordinate. 您所要做的就是将字符串解析为GeoCoordinate。 This might do the trick (untested): 这可能会达到目的(未测试):

     var r = (string)query.Element("bingCoordinates").Split(','); BingCoordinates = new GeoCoordinate(r[0], r[1]); 

    Hints: GeoCoordinate constructors first arg is the latitude followed by the longitude. 提示:GeoCoordinate构造函数的第一个arg是纬度,然后是经度。 Also note that a null check for the split operation should be used if you want to avoid possible exceptions. 另请注意,如果要避免可能的异常,应对分割操作使用null检查。

  2. Using an ValueConverter to convert the source object into an element your view "understands". 使用ValueConverter将源对象转换为视图“理解”的元素。 If you for some reason decide that the representation of your model is accurate and changing it there doesn't make to much sense you can always convert the value to a value of your needs on demand. 如果您出于某种原因决定模型的表示形式是准确的并且对其进行了更改,那么从某种意义上讲,您始终可以将值转换为按需需求的值。 To do this a ValueConverter can be used. 为此,可以使用ValueConverter。 A ValueConverter is a class that implements the IValueConverter interface and can be used in bindings to convert a value into the desired type (most common example: converting a boolean (true/false) into a Visibility type). ValueConverter是一个实现IValueConverter接口的类,可用于在绑定中将值转换为所需的类型(最常见的示例:将布尔值(true / false)转换为Visibility类型)。 Learn more about value converters . 了解有关价值转换器的更多信息

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM