예제 #1
0
        public async Task <T> LoadLocalData(string filePath)
        {
            if (string.IsNullOrEmpty(filePath))
            {
                return(null);
            }

            T result = null;

            //load cache
            try
            {
                var json = await IsolatedStorageHelper.ReadFileAsync(filePath);

                if (!string.IsNullOrEmpty(json))
                {
                    result = JsonSerializer.Deserialize <T>(json);
                }
            }
            catch (Exception ex)
            {
            }

            return(result);
        }
예제 #2
0
        public async Task <T> LoadLocalData(string module, string file)
        {
            if ((string.IsNullOrEmpty(module) || string.IsNullOrEmpty(file)))
            {
                return(null);
            }

            T result = null;

            //load cache
            try
            {
                //ensure file existence
                //await IsolatedStorageHelper.GetFileAsync(moduleName, fileName, Windows.Storage.CreationCollisionOption.OpenIfExists);
                var json = await IsolatedStorageHelper.ReadFileAsync(module, file);

                if (!string.IsNullOrEmpty(json))
                {
                    result = JsonSerializer.Deserialize <T>(json);
                }
            }
            catch (Exception ex)
            {
            }

            return(result);
        }
예제 #3
0
        public async void Load(string dataURL, bool cacheData, string module, string file, Action <T> callback)
        {
            if (cacheData && (string.IsNullOrEmpty(module) || string.IsNullOrEmpty(file)))
            {
                return;
            }

            //for callback
            onCallback  = callback;
            toCacheData = cacheData;
            moduleName  = module;
            fileName    = file;

            if (!NetworkHelper.CheckInternet())
            {
                //load cache
                if (cacheData)
                {
                    try
                    {
                        var cachedJson = await IsolatedStorageHelper.ReadFileAsync(moduleName, fileName);

                        T obj = JsonSerializer.Deserialize <T>(cachedJson);
                        if (obj != null)
                        {
                            App.CurrentInstance.RunAsync(() =>
                            {
                                onCallback(obj);
                            });
                        }
                    }
                    catch (Exception ex)
                    {
                    }
                }
                return;
            }

            //download new
            try
            {
                HttpWebRequest request = HttpWebRequest.CreateHttp(new Uri(dataURL));
                request.Method = "GET";
                request.BeginGetResponse(GetData_Callback, request);

                Loaded = false;
                Busy   = true;
            }
            catch (WebException e)
            {
            }
            catch (Exception e)
            {
            }
        }
예제 #4
0
        public static async Task <T> Deserialize <T>(string file)
        {
            T val;

            string content = await IsolatedStorageHelper.ReadFileAsync(file);

            using (StringReader sr = new StringReader(content))
            {
                XmlSerializer xs = new XmlSerializer(typeof(T));
                val = (T)xs.Deserialize(sr);
            }
            return(val);
        }
예제 #5
0
        public static async Task FastIterate(string file, Action <string, string> actionOnEachNode)
        {
            string content = await IsolatedStorageHelper.ReadFileAsync(file);

            if (content.Contains("?>"))
            {
                int from = content.IndexOf("?>");
                content = content.Substring(from + 2);
            }

            string           nodeName  = string.Empty;
            string           nodeValue = string.Empty;
            int              maxIndex  = content.Length - 1;
            int              i         = 0;
            XmlParsingStatus status    = XmlParsingStatus.NodeEnded;

            while (i < maxIndex)
            {
                switch (status)
                {
                case XmlParsingStatus.BeginningOrEnd:
                    if (content[i] == '/')
                    {
                        status = XmlParsingStatus.EndOfNodeDetected;
                    }
                    else
                    {
                        status = XmlParsingStatus.BeginningOfNodeDetected;
                    }
                    break;

                case XmlParsingStatus.BeginningOfNodeDetected:
                    if (content[i] == '>')
                    {
                        status    = XmlParsingStatus.ValueCollecting;
                        nodeValue = string.Empty;
                    }
                    break;

                case XmlParsingStatus.ValueCollecting:
                    if (content[i] == '<')
                    {
                        status = XmlParsingStatus.BeginningOrEnd;
                    }
                    else
                    {
                        nodeValue += content[i];
                    }
                    break;

                case XmlParsingStatus.EndOfNodeDetected:
                    if (content[i] == '>')
                    {
                        status = XmlParsingStatus.NodeEnded;
                        actionOnEachNode(nodeName, nodeValue);
                        nodeName  = string.Empty;
                        nodeValue = string.Empty;
                    }
                    else
                    {
                        nodeName += content[i];
                    }
                    break;

                case XmlParsingStatus.NodeEnded:
                    if (content[i] == '<')
                    {
                        status = XmlParsingStatus.BeginningOrEnd;
                    }
                    break;

                default:
                    break;
                }

                i++;
            }
        }