Skip to main content
 首页 » 编程设计

c#之无法使用必应 map 通过 mvvm 将 map 地理坐标中心绑定(bind)到 Windows Phone 上的 map

2024年01月02日34开发

我正在尝试根据给定实例在 map 上显示的各种路线动态设置 map 控件的中心属性。我在这里使用路线的起点作为地理坐标中心。为此,我尝试使用 bing map 通过 Windows Phone 应用程序上的 mvvm 将地理坐标中心绑定(bind)到 map 控件。我能够通过纬度和经度获取中心位置的数据,但无法将其绑定(bind)到 UI(xaml) 页面。

下面是我的RouteViewModel类

 private Location startPoint; 
    public Location StartPoint 
    { 
        get { return startPoint; } 
        set 
        { 
            startPoint = value; 
            Change("StartPoint"); 
        } 
    } 
 
   private System.Device.Location.GeoCoordinate centre; 
 
    public System.Device.Location.GeoCoordinate Centre 
    { 
        get { return centre; } 
        set { centre = value; } 
    } 
 
    private string getcenter; 
 
    public string Getcenter 
    { 
        get { return getcenter; } 
        set { getcenter = value; } 
    } 
 
    private Location endPoint; 
    public Location EndPoint 
    { 
        get { return endPoint; } 
        set 
        { 
            endPoint = value; 
            Change("EndPoint"); 
        } 
    } 
 
    private ObservableCollection<Location> routePoints; 
    public ObservableCollection<Location> RoutePoints 
    { 
        get { return routePoints; } 
        set 
        { 
            routePoints = value; 
            Change("RoutePoints"); 
        } 
    } 
 
    private ObservableCollection<ItineraryItem> itinerary; 
    public ObservableCollection<ItineraryItem> Itinerary 
    { 
        get 
        { 
            return itinerary; 
        } 
        set 
        { 
            itinerary = value; 
            Change("Itinerary"); 
        } 
    } 
 
 
   private void LocationLoaded() 
    { 
        if (fromLocation != null && toLocation != null) 
        { 
            var fromWaypoint = new Waypoint(); 
            fromWaypoint.Description = "From"; 
            fromWaypoint.Location = new Location(); 
            fromWaypoint.Location.Altitude = fromLocation.Altitude; 
            fromWaypoint.Location.Latitude = fromLocation.Latitude; 
            fromWaypoint.Location.Longitude = fromLocation.Longitude; 
 
            var toWaypoint = new Waypoint(); 
            toWaypoint.Description = "To"; 
            toWaypoint.Location = new Location(); 
            toWaypoint.Location.Altitude = toLocation.Altitude; 
            toWaypoint.Location.Latitude = toLocation.Latitude; 
            toWaypoint.Location.Longitude = toLocation.Longitude; 
 
            var routeRequest = new RouteRequest(); 
            routeRequest.Credentials = new Credentials(); 
            routeRequest.Credentials.ApplicationId = App.BingApiKey; 
            routeRequest.Waypoints = new System.Collections.ObjectModel.ObservableCollection<Waypoint>(); 
            routeRequest.Waypoints.Add(fromWaypoint); 
            routeRequest.Waypoints.Add(toWaypoint); 
            routeRequest.Options = new RouteOptions(); 
            routeRequest.Options.RoutePathType = RoutePathType.Points; 
            routeRequest.UserProfile = new Utils.WP7.Bing.BingRoute.UserProfile(); 
            routeRequest.UserProfile.DistanceUnit = Utils.WP7.Bing.BingRoute.DistanceUnit.Kilometer; 
 
            var routeClient = new RouteServiceClient("BasicHttpBinding_IRouteService"); 
            routeClient.CalculateRouteCompleted += new EventHandler<CalculateRouteCompletedEventArgs>(OnRouteComplete); 
            routeClient.CalculateRouteAsync(routeRequest); 
        } 
    } 
 
 
   private void OnRouteComplete(object sender, CalculateRouteCompletedEventArgs e) 
    { 
        if (e.Result != null && e.Result.Result != null && e.Result.Result.Legs != null & e.Result.Result.Legs.Any()) 
        { 
            var result = e.Result.Result; 
            var legs = result.Legs.FirstOrDefault(); 
 
            StartPoint = legs.ActualStart; 
            EndPoint = legs.ActualEnd; 
            RoutePoints = result.RoutePath.Points; 
            Itinerary = legs.Itinerary; 
 
     //Centre = StartPoint; 
             Getcenter = string.Format("{0},{1}", StartPoint.Latitude.ToString(), StartPoint.Longitude.ToString()); 
 
            RaiseRouteResolved(); 
        } 
     } 

以下是我的.xaml.cs页面中的代码,后面是我的xaml页面中的代码。

 (DataContext as RouteViewModel).ResolveRouteFromCurrent(); 
 
 
 
<maps:Map x:Name="RMaps" Center="{Binding Getcenter}" ZoomLevel="5" CredentialsProvider="{StaticResource MapCredentials}"> 

我也尝试绑定(bind) GeoCoordinate 类型的“Centre”(在上面的代码中注释),但这并没有解决我的问题。有人可以告诉我解决此问题的方法吗...在此先感谢。

请您参考如下方法:

您的类需要实现 INotifyPropertyChanged 并且您的 Getcenter 属性需要在值发生变化时引发 PropertyChangedEvent 以便数据绑定(bind)在值发生时起作用更新。