问题
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.
回答1:
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));
}
回答2:
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/.
来源:https://stackoverflow.com/questions/10515367/wp7-bing-maps-zoom-level-based-on-push-pin-collection-locations