2013-07-02 101 views
1

如何将隔离存储应用于以下程序中的textBlock。你可以清楚地知道我想要做的是通过在链接“How to save values in database using Sqlite?”中看到这些图像,我需要日期(今天的日期 - 这不包括在代码中),时间(TimeLabel.Text),节奏(paceLabel.Text ),距离(distanceLabel.Text)从下面的程序放置在独立存储中以将它们发布到列表框中。任何可以给我一个好主意的方法是将值存储在IsolatedStorage(简要答案)中以便发布到Listbox中,这些链接也被接受和赞赏。提前感谢你。如何将独立存储应用于Windows Phone中的值?

//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; 
    } 
} 
+0

所以你要存储在IsoStorage或SQLite的值是多少? –

+0

@Shawn Kendrot可以帮我把这个值放在IsolatedStorage中吗? –

+0

你想存储整个列表或只是最后一个值 –

回答

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); 
    } 
} 

希望帮助

相关问题