1

如何将独立存储应用于以下程序中的文本块。通过在此链接中查看这些图像,您可以清楚地了解我想要做的是“如何使用 Sqlite 在数据库中保存值? ”我需要日期(今天的日期 - 不包含在代码中),时间(TimeLabel.文本),步速(paceLabel.Text),距离(distanceLabel.Text)要从下面的程序中隔离存储,以将它们发布到列表框中。任何可以让我很好地将值存储在隔离存储中的链接(简短答案)以发布在列表框中也被接受和赞赏。提前谢谢你。

//location tracker
public partial class Mog : PhoneApplicationPage
        {
         //   private Geo
          //  _watcher = new Geolocator(GeoPositionAccuracy.High);
          private GeoCoordinateWatcher _watcher = new GeoCoordinateWatcher(GeoPositionAccuracy.High);
            private MapPolyline _line;
            private DispatcherTimer _timer = new DispatcherTimer();
            private long _startTime;   
    public Mog()
            {
                InitializeComponent();

            _line = new MapPolyline();
            _line.StrokeColor = Colors.Red;
            _line.StrokeThickness = 5;
            Map.MapElements.Add(_line);

            _watcher.Start();

            _timer.Start();
            _startTime = System.Environment.TickCount;

            _watcher.PositionChanged += Watcher_PositionChanged;

            _timer.Interval = TimeSpan.FromSeconds(1);
            _timer.Tick += Timer_Tick;
        } 


private double _kilometres;
        private double calories;      
        private long _previousPositionChangeTick;

private void Watcher_PositionChanged(object sender, GeoPositionChangedEventArgs<GeoCoordinate> e)
        {
            //IsolatedStorageSettings milli = IsolatedStorageSettings.ApplicationSettings;
            var coord = new GeoCoordinate(e.Position.Location.Latitude, e.Position.Location.Longitude);

            if (_line.Path.Count > 0)
            {
                var calories = caloriesLabel.Text;

                var previousPoint = _line.Path.Last();
                var distance = coord.GetDistanceTo(previousPoint);
                var millisPerKilometer = (1000.0 / distance) * (System.Environment.TickCount - _previousPositionChangeTick);
                _kilometres += distance / 1000.0;               
                paceLabel.Text = TimeSpan.FromMilliseconds(millisPerKilometer).ToString(@"mm\:ss");
                distanceLabel.Text = string.Format("{0:f2} km", _kilometres);
                caloriesLabel.Text = string.Format("{0:f0}", _kilometres * 65);
                PositionHandler handler = new PositionHandler();
                var heading = handler.CalculateBearing(new Position(previousPoint), new Position(coord));
                Map.SetView(coord, Map.ZoomLevel, heading, MapAnimationKind.Parabolic);
            }
else
      {
        Map.Center = coord;
      }

      _line.Path.Add(coord);
      _previousPositionChangeTick = System.Environment.TickCount;
    }
}
4

1 回答 1

0

像这样存储集合的最佳方法之一是将集合序列化为 json 并保存文件。您可以为此使用 Json.Net。您需要创建一个对象集合来存储这些值。

public class RunMarker
{
    public double Kilometers { get; set; }
    public TimeSpan Pace { get; set; }
    public double Calories { get; set; }
}

当位置发生变化时,您将为此设置值并将其添加到集合中。

private void Watcher_PositionChanged(object sender, GeoPositionChangedEventArgs<GeoCoordinate> e)
{
    // add actual values
    _markers.Add(new RunMarker { Kilometers = 0, Pace = TimeSpan.FromSeconds(1), Calories = 50 });
}

然后,当您想保存它时,您将其序列化为 Json 并将文件保存在 IsolatedStorage

const string MarkerFileName = "RunMarker.json";

var storage = IsolatedStorageFile.GetUserStoreForApplication();
if (storage.FileExists(MarkerFileName)) storage.DeleteFile(MarkerFileName);

using (var fileStream = storage.CreateFile(MarkerFileName))
{
    //Write the data
    using (var isoFileWriter = new StreamWriter(fileStream))
    {
        var json = JsonConvert.SerializeObject(_markers);
        isoFileWriter.WriteLine(json);
    }
}

希望有帮助

于 2013-07-17T15:49:38.390 回答