无法使用bing贴图通过mvvm将地图地理坐标中心绑定到Windows手机上的地图上

时间:2012-05-29 09:25:13

标签: c# xaml mvvm windows-phone-7.1 bing-maps

我正在尝试根据我在给定实例的地图上显示的各种路径动态设置地图控件的center属性。我在这里使用起点作为地理坐标中心。为了实现这一点,我尝试使用bing贴图通过windows phone应用程序上的mvvm将地理坐标中心绑定到地图控件上。我可以使用latitiude和经度获取中心位置的数据,但无法将其绑定到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}">

我也试过绑定GeoCoordinate类型的“Center”(在上面的代码中评论),但这并没有解决我的问题。有人可以让我知道我能够解决这个问题的方法......提前致谢。

1 个答案:

答案 0 :(得分:0)

您的类需要实现INotifyPropertyChanged,并且当值更改时,您的Getcenter属性需要引发PropertyChangedEvent,以便数据绑定在值更新时工作。