简体   繁体   中英

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:

        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:

        <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

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:

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).

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". Right now that Property seems to be of type string (from what I understood). Changing its type to GeoCoordinate would solve your problem. All you have to do is parsing the string into a 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. Also note that a null check for the split operation should be used if you want to avoid possible exceptions.

  2. Using an ValueConverter to convert the source object into an element your view "understands". 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. 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). Learn more about value converters .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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