4
votes

Hello Stackoverflow community! I've started recently building a flight dashboard, including Bing Maps WPF Control in MVVM patters (at least as much, as possible) for Windows PC devices.

After seeking the web for a while I was able to deliver CredentialsProvider based on the key in app.config and moved to implement automatic centering of the Bing's control map based on the current device's position.

The XAML:

<m:Map ZoomLevel="16" Mode="Aerial" CredentialsProvider="{Binding BingMapsCredentials}" Grid.Row="1" Grid.ColumnSpan="2" Center="{Binding DevPosition}"/>

The ViewModel:

    private readonly CredentialsProvider bingMapsCredentials = new ApplicationIdCredentialsProvider(ConfigurationSettings.AppSettings.Get("BingMapsKey"));
    private readonly double nDefaultLatitude = double.Parse(ConfigurationSettings.AppSettings.Get("DefaultLongitude"), System.Globalization.CultureInfo.InvariantCulture);
    private readonly double nDefaultLongitude = double.Parse(ConfigurationSettings.AppSettings.Get("DefaultLatitude"), System.Globalization.CultureInfo.InvariantCulture);
    private GeoCoordinate nDevicePosition;

    public CredentialsProvider BingMapsCredentials
    {
        get { return bingMapsCredentials; }
    }
    public Location DevPosition
    {
        get { return new Location(nDeviceLat, nDeviceLon); }
    }
    public double nDeviceLon
    {
        get
        {
            if (nDevicePosition.IsUnknown)
                return nDefaultLongitude;
            else
                return nDevicePosition.Longitude; }
        set { nDevicePosition.Longitude = value; }
    }
    public double nDeviceLat
    {
        get
        {
            if (nDevicePosition.IsUnknown)
                return nDefaultLatitude;
            else
                return nDevicePosition.Latitude;
        }
        set { nDevicePosition.Latitude = value; }
    }

While binding the CredentialsProvider works fine, setting the Center location does not at all. The map is displayed correctly but somewhere in the middle of nowhere. The debugger shows that there is no call on the Get property on location. There are also no WPF warning/error traces in the Output Window.

Am I missing something here?

Any help is appreciated.

P.

2
Does your VM implements INotfiyPropertyChanged ? - XAMlMAX
I'm with @XAMIMAX. If you haven't implemented the 'INotifyPropertyChanged' interface, and changed the property's set to fire off a property changed notification, then the View will never see the property change. Ref: msdn.microsoft.com/en-us/library/vstudio/… Please let us know whether you've done this or not as it'll help the community help you. - River-Claire Williamson

2 Answers

0
votes

You haven't bound nDeviceLat and nDeviceLon to the DevPosition. As such this will only pull in the correct values when the get method is called. If the position is moved there is nothing to trigger the UI to update. Try doing something like this:

public Location DevPosition { get; set; }

public double nDeviceLon
{
    get
    {
        if (nDevicePosition.IsUnknown)
            return nDefaultLongitude;
        else
            return nDevicePosition.Longitude; }
    set 
{ 
    nDevicePosition.Longitude = value; 
    DevPosition = new Location(nDeviceLat, nDeviceLon);
}
}
public double nDeviceLat
{
    get
    {
        if (nDevicePosition.IsUnknown)
            return nDefaultLatitude;
        else
            return nDevicePosition.Latitude;
    }
    set { 
    nDevicePosition.Latitude = value; 
    DevPosition = new Location(nDeviceLat, nDeviceLon);
}
}
0
votes

This seems to be a common problem. There's a couple issues here, but basically you want to use setView() rather than setting to Center.

Here's two excellent articles on working with Bing maps in WPF, which also explains the center property more:
http://www.mobilemotion.eu/?p=1077&lang=en http://visualstudiomagazine.com/Articles/2012/04/01/Map-Your-Apps.aspx?Page=2

Relates to/Possible Duplicate of: Center and Zoom on Bing Maps WPF

Hope this helps!