【问题标题】:WP7 Bing Maps Zoom level based on Push Pin collection locationsWP7 Bing Maps 基于图钉集合位置的缩放级别
【发布时间】:2012-05-09 11:42:58
【问题描述】:

关于使用以下代码 sn-p 的简单问题:

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( ()=> {...} )

我的问题是,我无法在 Dispatcher 中引用地图控件(或者我可以吗??),那么如何使用以下方法将新的 Pushpin 位置应用到地图:

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

谢谢, S.

【问题讨论】:

    标签: windows-phone-7 bing-maps


    【解决方案1】:

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

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

    另外,值得注意的是,如果 CheckAccess() 返回 false,您只需要调用 BeginInvoke()。 (CheckAccess 被标记为 EditorBrowsable(EditorBrowsableState.Never) 属性,因此它不会出现在智能感知中,您必须手动输入)。常见的模式是;

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

    【讨论】:

    • 感谢您的回复;不过,我的问题是我正在使用 MVVM。 View Model 正在检索位置列表并将它们绑定到 Observable Collection,后者正在更新地图。地图本身无法从视图模型访问,所以我可以从视图模型使用 map.Dispatcher.BeginInvoke 吗?
    • @StevieB 您的视图模型是否也不能从 DependencyObject 继承?
    【解决方案2】:

    我也许你会发现这篇文章很有用。要将地图的视图与 ViewModel 绑定,所描述的方法使用 DependecyPropety :http://sveiberg.wordpress.com/2012/06/24/5/

    【讨论】:

    • 你答案的第二行没有意义。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-02-21
    • 1970-01-01
    • 2021-04-23
    • 2021-04-18
    • 1970-01-01
    相关资源
    最近更新 更多