none
BingMaps and mvvm RRS feed

  • Question

  • Hi I'm trying to use MVVM with bing maps,

    I have a MainPage with a hubControl, in one of its sections I got my map control with this xaml:

    <bm:Map x:Name="myMap"Grid.Row="0" Grid.Column="1" Grid.RowSpan="2" Width="800"ZoomLevel="18"
             DataContext="{Binding VMpuntos}"         Credentials="xxxxxx" >
                                <bm:MapItemsControl ItemsSource="{Binding Locations}">
                                    <bm:MapLayer.Position>
                                    <bm:Location Latitude="{Binding Path=Latitude}" Longitude="{Binding Path=Longitude}"/>
                                    </bm:MapLayer.Position>
                                </bm:MapItemsControl>
                            </bm:Map>

    I got a ViewModel named "VMMain" wich is the VM for the mainPage.

    in The VMMain I got a property called VMPuntos  wich is another VM but for MapManage only. In that VM I got a constructor like this:

    public VMpuntos() {     Task.Run(async () =>     {         awaitthis.LoadPoints();     }); }

            public async Task CargaPuntos()
            {
                Puntos = await BLL.clsPunto.GetPuntos();
                foreach (CPunto p in Puntos)
                {
    #if WINDOWS_APP
                        Location l = new Location();
                        l.Latitude = p.Latitud; 
                        l.Longitude = p.Longitud ;
                        Locations.Add(l); 
    #elif WINDOWS_PHONE_APP
    #endif
                }
            }
    I am having an exception in the line Location l = new Location(); and the exception is:

    "The application called an interface that was marshalled for a different thread."

    I really don't have a clue, what is this ? something with the async programing I guess.

    Thnaks in advance

     

    asdas


    Salu2 Sergio T

    • Moved by Jamles Hez Wednesday, July 1, 2015 7:05 AM
    Tuesday, June 30, 2015 4:49 PM

Answers

  • The issue here is that you have an async method which generates a second thread to process the method that is not the UI thread. The location object is a dependency object which means it must be created on the UI thread. Make the method none async and it should get rid of this error. Alternatively you could use a dispatcher but this would likely only add more overhead in this scenario.

    http://rbrundritt.wordpress.com


    Friday, July 3, 2015 6:50 PM

All replies

  • Hi Sergio,

    I will move the question to BingMap forum for a better support.

    Thanks for your understanding.

    --James


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Wednesday, July 1, 2015 7:05 AM
  • The issue here is that you have an async method which generates a second thread to process the method that is not the UI thread. The location object is a dependency object which means it must be created on the UI thread. Make the method none async and it should get rid of this error. Alternatively you could use a dispatcher but this would likely only add more overhead in this scenario.

    http://rbrundritt.wordpress.com


    Friday, July 3, 2015 6:50 PM