Beispiel #1
0
        /// <summary>
        /// 將物件資料寫入到檔案中
        /// </summary>
        public virtual async Task WriteToFileAsync()
        {
            string data = "";

            if (IsCollectionType == false)
            {
                data = JsonConvert.SerializeObject(this.SingleItem);
            }
            else
            {
                data = JsonConvert.SerializeObject(this.Items);
            }
            await StorageUtility.WriteToDataFileAsync(this.CurrentFolderName, this.DataFileName, data);
        }
Beispiel #2
0
        /// <summary>
        /// 將物件資料從檔案中讀取出來
        /// </summary>
        public virtual async Task ReadFromFileAsync()
        {
            #region 先將建立該資料模型的物件,避免檔案讀取不出來之後, Items / SingleItem 的物件值為 null
            if (IsCollectionType == false)
            {
                SingleItem = (T)Activator.CreateInstance(typeof(T));
            }
            else
            {
                Items = (List <T>)Activator.CreateInstance(typeof(List <T>));
            }
            #endregion

            string data = await StorageUtility.ReadFromDataFileAsync(this.CurrentFolderName, this.DataFileName);

            if (string.IsNullOrEmpty(data) == true)
            {
            }
            else
            {
                try
                {
                    if (IsCollectionType == false)
                    {
                        this.SingleItem = JsonConvert.DeserializeObject <T>(data, new JsonSerializerSettings {
                            MetadataPropertyHandling = MetadataPropertyHandling.Ignore
                        });
                    }
                    else
                    {
                        this.Items = JsonConvert.DeserializeObject <List <T> >(data, new JsonSerializerSettings {
                            MetadataPropertyHandling = MetadataPropertyHandling.Ignore
                        });
                    }
                }
                catch (Exception ex)
                {
                    Debug.WriteLine(ex.Message);
                }
            }
        }