/// <summary>
        /// Loads data from a file
        /// </summary>
        /// <param name="fileName">Name of the file to read.</param>
        /// <returns>Data object</returns>
        public static async Task <T> ReadFromFileAsync(string directoryName, string fileName)
        {
            //T loadedFile = default(T);
            T      loadedFile = (T)Activator.CreateInstance(typeof(T));
            string tempStr    = "";

            try
            {
                tempStr = await StorageUtility.ReadFromDataFileAsync(directoryName, fileName);

                loadedFile = JsonConvert.DeserializeObject <T>(tempStr);
            }
            catch
            {
                //ApplicationState.ErrorLog.Add(new ErrorLog("LoadFromFile", e.Message));
            }

            return(loadedFile);
        }
示例#2
0
        /// <summary>
        /// 將物件資料從檔案中讀取出來
        /// </summary>
        public virtual async Task ReadFromFileAsync()
        {
            if (PersistentStorage == PersistentStorage.Single)
            {
                SingleItem = (T)Activator.CreateInstance(typeof(T));
            }
            else
            {
                Items = (List <T>)Activator.CreateInstance(typeof(List <T>));
            }

            string data = await StorageUtility.ReadFromDataFileAsync(this.現在資料夾名稱, this.資料檔案名稱);

            if (string.IsNullOrEmpty(data) == true)
            {
            }
            else
            {
                try
                {
                    if (PersistentStorage == PersistentStorage.Single)
                    {
                        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)
                {
                    Console.WriteLine(ex.Message);
                }
            }
        }