1

为一个遛狗的朋友制作了一个快速应用程序,他想知道他走了多远,速度以及高精度的东西。无论如何,我用计时器让应用程序只跟踪距离和位置。一切正常,但是在更新屏幕上的统计信息(每 8 秒执行一次)时,似乎存在性能问题,因为时间会暂停然后跳 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 回答