示例#1
0
        public async Task <Settings> GetSettingsAsync()
        {
            var      formatter = new DataContractSerializer(typeof(Settings));
            var      filePath  = Path.Combine(ApplicationData.Current.LocalFolder.Path, SettingsName);
            Settings settings  = new Settings();

            if (!File.Exists(filePath))
            {
                var defaultSettings = Create();
                return(await WriteSettingsAsync(defaultSettings));
            }

            var file = await ApplicationData.Current.LocalFolder.GetFileAsync(SettingsName);

            var stream = await file.OpenAsync(FileAccessMode.Read);

            ulong size = stream.Size;

            using (var inputStream = stream.GetInputStreamAt(0))
            {
                using (var dataReader = new Windows.Storage.Streams.DataReader(inputStream))
                {
                    uint numBytesLoaded = await dataReader.LoadAsync((uint)size);

                    settings.FileSystemDirectory = dataReader.ReadString(numBytesLoaded); // BUG
                    settings.IsFileSystemEnabled = dataReader.ReadBoolean();
                }
            }

            //var lines = await FileIO.ReadTextAsync(file);
            //settings.FileSystemDirectory = lines;
            //var stream = new FileStream(settingsFileName, FileMode.Open, FileAccess.Read, FileShare.None);
            //settings = (Settings)formatter.ReadObject(stream);


            Debug.WriteLine("Read Settings");
            return(settings);
        }
		/// <summary>
		/// Creates and adds a time track objects into the Items collection.
		/// </summary>
		public async void LoadData() {

			var fileList = await ApplicationData.Current.LocalFolder.GetFilesAsync();
			var file = fileList.FirstOrDefault(f => f.Name == localFileName);
			if (file != null) {
				using (var stream = await file.OpenReadAsync()) {
					using (var dr = new Windows.Storage.Streams.DataReader(stream)) {
						try {
							await dr.LoadAsync(unchecked((uint)stream.Size));
							var numberOfItems = dr.ReadInt32();
							ObservableCollection<TrackItem> loadingItems = new ObservableCollection<TrackItem>();
							for (int x = 0; x < numberOfItems; x++) {
								TrackItem item = new TrackItem();
								item.Start = dr.ReadDateTime();
								item.End = dr.ReadDateTime();
								int topicLength = dr.ReadInt32();
								item.Topic = dr.ReadString((uint)topicLength);
								loadingItems.Add(item);
							}
							bool currentItemExists = dr.ReadBoolean();
							TrackItem loadingCurrentItem = null;
							if (currentItemExists) {
								loadingCurrentItem = new TrackItem();
								loadingCurrentItem.Start = dr.ReadDateTime();
								int topicLength = dr.ReadInt32();
								loadingCurrentItem.Topic = dr.ReadString((uint)topicLength);
							}
							dr.DetachStream();

							Items = loadingItems;
							OnPropertyChanged(nameof(Items));
							CurrentItem = loadingCurrentItem;
							IsDataLoaded = true;
						} catch {
							try {
								await file.DeleteAsync();
							} catch { }
							CurrentItem = null;
							IsDataLoaded = true;
						}
					}
				}
			} else {
				// Sample data; replace with real data
				Items.Add(new TrackItem() {
					Topic = "First Tracked Item",
					Start = new DateTimeOffset(new DateTime(2015, 11, 5, 9, 0, 0), new TimeSpan(-6, 0, 0)),
					End = new DateTimeOffset(new DateTime(2015, 11, 5, 11, 30, 0), new TimeSpan(-6, 0, 0)),
				});
				Items.Add(new TrackItem() {
					Topic = "Second Tracked Item",
					Start = new DateTimeOffset(new DateTime(2015, 11, 5, 12, 30, 0), new TimeSpan(-6, 0, 0)),
					End = new DateTimeOffset(new DateTime(2015, 11, 5, 17, 0, 0), new TimeSpan(-6, 0, 0)),
				});
				CurrentItem = null;
				this.IsDataLoaded = true;
			}
		}