1

犬を散歩させている友人のために簡単なアプリを作成しました。彼は、犬がどこまで歩いているかを知りたいと思っています。とにかく、タイマーで距離と場所だけをアプリに追跡させました。すべて正常に動作しますが、画面上の統計の更新(8秒ごとに実行)に関しては、時間が一時停止してから2秒ジャンプするため、更新部分に1秒かかると言うかのようにパフォーマンスの問題があるようです。 2.2。

これがコードです

List<GeoCoordinate> Locations;
Geolocator Locator = new Geolocator();
Locator.DesiredAccuracy = PositionAccuracy.High;
Locator.MovementThreshold = 1;
Locator.PositionChanged += Locator_PositionChanged;

DispatcherTimer _timer = new DispatcherTimer();
_timer.Interval = TimeSpan.FromSeconds(1);
_timer.Tick += Timer_Tick;
_timer.Start();
long _startTime = System.Enviroment.TickCount;

private void Locator_PositionChanged(Geolocator sender, PostionChangedEventArgs args)
{
    CurrentLocation = args.Position;

    if(GetPositionTime >= 8) // Checks to see if 8 seconds has passed
    {           
       Deployment.Current.Dispatcher.BeginInvoke(new Action(() =>
       {
            GeoCoordinate cord = new GeoCoordinate(CurrentLocation.Coordinate.Latitude,
                                    CurrentLocation.Coordinate.Longitude);
         if(Locations.Count > 0)
         {
            GeoCoordinate PreviousLocation = Locations.Last();

            // This part will update the stats on the screen as a textbox is bound to
            // DistanceMoved
            DistanceMoved = cord.GetDistanceTo(PreviousLocation);             
         }

         Locations.Add(cord);
       }));
    }
}

private void Timer_Tick(object sender, EventArgs e)
{
    GetPositionTime++;
    TimeSpan time = TimeSpan.FromMilliseconds(System.Enviroment.TickCount - _startTime);

    // Update the timer on the screen
    Duration = time.ToString(@"hh\:mm\:ss");
}
4

1 に答える 1

0

UI コントロールは UI スレッドで更新する必要があります - Dispatcher.BeginInvoke を Duration にラップすると、動作するはずです。

Deployment.Current.Dispatcher.BeginInvoke(new Action(() =>
{
        Duration = time.ToString(@"hh\:mm\:ss"); 
}
于 2013-02-17T06:54:11.880 に答える