0

地図上にルートを表示しようとしていますが、この例外が発生します。理由がわかりません。ここに私のコードビハインドがあります:

public partial class map_new : PhoneApplicationPage
{
    public GeoCoordinate destination;
    public GeoCoordinate myPosition;
    public Geolocator myGeolocator;
    List<GeoCoordinate> waypoints = new List<GeoCoordinate>();
    public RouteQuery routeQuery;


    public map_new()
    {
        InitializeComponent();
        destination = new GeoCoordinate(41.909859, 12.461792);
        ShowDestinationLocationOnTheMap();

        myGeolocator = new Geolocator();
        myGeolocator.DesiredAccuracy = PositionAccuracy.High;
        myGeolocator.MovementThreshold = 20; // The units are meters.
        myGeolocator.StatusChanged+=geolocator_StatusChanged;


    }

    private async Task update_position() 
    {
        Geoposition myGeoposition = await myGeolocator.GetGeopositionAsync();
        Geocoordinate myGeocoordinate = myGeoposition.Coordinate;
        myPosition = CoordinateConverter.ConvertGeocoordinate(myGeocoordinate);
        Dispatcher.BeginInvoke(() =>
        {
            this.myMap.Center = myPosition;
            this.myMap.ZoomLevel = 16;
        });
        //update_route();
    }

    private async void update_route()
    {
        await update_position();
        RouteQuery routeQuery = new RouteQuery();
        waypoints.Add(myPosition);
        waypoints.Add(destination);
        routeQuery.Waypoints = waypoints;
        routeQuery.QueryCompleted += routeQuery_QueryCompleted;
        routeQuery.QueryAsync();
    }

    void routeQuery_QueryCompleted(object sender, QueryCompletedEventArgs<Route> e)
    {
        if (e.Error == null)
        {
            Route MyRoute = e.Result;
            MapRoute MyMapRoute = new MapRoute(MyRoute);
            myMap.AddRoute(MyMapRoute);
            routeQuery.Dispose();
        }
    }

    void geolocator_StatusChanged(Geolocator sender, StatusChangedEventArgs args)
    {
        string status = "";

        switch (args.Status)
        {

            case PositionStatus.Ready:
                // the location service is generating geopositions as specified by the tracking parameters
                status = "ready";
                update_route();
                break;

    }

エラーは update_route() が update_position() が完了するのを待たないことだと思いますが、 update_route() を待つように設定する方法がわかりません。何か案が?

編集:KooKiz と Stephen ソリューションを適用した後、エラーは次の行にあります。

RouteQuery routeQuery = new RouteQuery();
4

2 に答える 2

3

通常、バックグラウンド スレッドから UI を更新しようとすると、「無効なクロススレッド アクセス」が発生します。あなたの場合、myMapコントロールを更新しようとしたときだと思います。

Dispatcher.BeginInvokeUI スレッドからコントロールを更新するには、次のメソッドを使用できます。

private async void update_position() 
{
    Geoposition myGeoposition = await myGeolocator.GetGeopositionAsync();
    Geocoordinate myGeocoordinate = myGeoposition.Coordinate;
    myPosition = CoordinateConverter.ConvertGeocoordinate(myGeocoordinate);
    Dispatcher.BeginInvoke(() =>
    {
            this.myMap.Center = myPosition;
            this.myMap.ZoomLevel = 16;
    });
    //update_route();
}

編集:どうやら、RouteQuery オブジェクトも UI スレッドでインスタンス化する必要があります。update_route次に、UI でメソッド全体を呼び出すことをお勧めします。

        case PositionStatus.Ready:
            // the location service is generating geopositions as specified by the tracking parameters
            status = "ready";
            Dispatcher.BeginInvoke(() => update_route());
            break;

(そしてDispatcher.BeginInvoke、メソッドからを削除しupdate_positionます)

于 2013-09-06T11:14:05.727 に答える
0

避けるべきasync voidです。代わりに、返されたasync Taskを使用できる を使用します。イベント ハンドラにのみ使用してください。awaitTaskasync void

ブログに紹介がありますので参考になさってくださいasync

于 2013-09-06T11:25:25.097 に答える