0
votes

Just a quick question regarding the use of the following code snippet:

var locations = CurrentItems.Select(model => model.Location);
map.SetView(LocationRect.CreateLocationRect(locations));

as suggested in this answer: Zoom to show all locations in bing maps

I am retrieving a list of geocoordinate asynchrounsly and binding these to a Bing Map using an ObservableCollection; copying the resultant data over to the main UI thread using:

Deployment.Current.Dispatcher.BeginInvoke( ()=> {...} )

My problem is that, I can't reference the map control within the Dispatcher (or can I??), so how can I apply the new Pushpin locations to the map using:

map.SetView(LocationRect.CreateLocationRect(locations));

Thanks, S.

2

2 Answers

0
votes

Because Map ultimately derives from DependencyObject it actually has its own Dispatcher. As such you can do;

map.Dispatcher.BeginInvoke(() => map.SetView(LocationRect.CreateLocationRect(locations)));

Also, it's worth noting you only need to call BeginInvoke() if the CheckAccess() returns false. (CheckAccess is tagged with an EditorBrowsable(EditorBrowsableState.Never) attribute so it won't show up in intellisense, you'll have to type it manually). The common pattern is;

if (map.Dispatcher.CheckAccess() == false) {
  map.Dispatcher.BeginInvoke(() => map.setView(LocationRect.CreateLocationRect(locations)));
} else {
  map.SetView(LocationRect.CreateLocationRect(locations));
}
0
votes

I perhaps you will find this post useful. To bind the view of the map and the ViewModel, the method described use a DependecyPropety : http://sveiberg.wordpress.com/2012/06/24/5/.