簡體   English   中英

如何將隔離存儲應用於Windows Phone中的值?

[英]How to apply Isolated Storage to the values in Windows Phone?

在以下程序中如何將隔離存儲應用於textBlocks。 通過在此鏈接中看到這些圖像,U可以清楚地理解我要執行的操作:“ 如何使用Sqlite在數據庫中保存值? ”我需要日期(今天的日期-代碼中未包含),時間(TimeLabel。文本),速度(paceLabel.Text),距離(distanceLabel.Text)要與以下程序隔離存儲,以將其發布到列表框中。 也可以接受和贊賞任何可以讓我很好地將值存儲在要在Listbox中發布的IsolatedStorage(簡短答案)中的鏈接。 預先謝謝你。

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

像這樣存儲集合的最好方法之一是將集合序列化為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);
    }
}

希望能有所幫助

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM