2012-05-09 43 views
0

就有关使用下面的代码片段的一个简单的问题:WP7 Bing地图缩放级别基于图钉集合地点

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

在这个答案建议: Zoom to show all locations in bing maps

我检索使用ObservableCollection将地理坐标列表绑定到Bing地图;使用在所得到的数据复制到主UI线程:

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

我的问题是,我不能引用分派器内的地图控制(或I可以??),所以我怎样才能应用新的图钉位置使用地图:

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

感谢, S.

回答

0

因为Map最终派生从DependencyObject它实际上有自己的Dispatcher。你可以这样做;

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

此外,值得一提的,你只需要调用BeginInvoke()如果CheckAccess()返回false。 (CheckAccess标有EditorBrowsable(EditorBrowsableState.Never)属性,所以它不会显示在智能感知中,您必须手动输入)。常见模式是;

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

感谢您的回应;我的问题,虽然,是我使用MVVM。视图模型正在检索位置列表并将它们绑定到正在更新地图的Observable集合。地图本身无法从视图模型访问,那么我可以使用视图模型中的map.Dispatcher.BeginInvoke吗? – StevieB

+1

您正在使用哪个MVVM框架? – MrMDavidson

+0

@StevieB是否有一个原因,你的视图模型不能也继承自'DependencyObject'? –