Win8.1應用開發之離線緩存

版權聲明:本文爲博主原創文章,未經博主贊成不得轉載。

https://blog.csdn.net/BlueCloudMatrix/article/details/37889939 css

咱們在開發應用商店應用時,需要app具備緩存的功能。這樣在離線模式下,仍能工做。咱們選擇的project爲Hub。html

這裏採取的策略是:在HubPage.xaml.cs(之因此不選擇App.xaml.cs。是爲了能讓用戶一邊操做界面一邊進行下載)中。利用await異步編程,避免堵塞UI,先讀取存有圖片路徑的JSON,而後解析該JSON獲得每一張圖片的URI,再依據URI下載圖片,對於文字資源,直接下載JSON。web

這裏要特別注意文件操做——文件權限。同一時候更要注意文件流的選取——假設選擇不當會致使在下載圖片時偶然性僵死。編程

在HubPage.xaml.cs中:緩存

網絡

using Bing.Maps; using demo02.Common; using demo02.Data; using demo02.DataModel; using demo02.DataStructure; using demo02.Entity; using demo02.Helper; using Newtonsoft.Json; using System; using System.Collections.Generic; using System.Collections.ObjectModel; using System.IO; using System.Linq; using System.Net.Http; using System.Runtime.InteropServices.WindowsRuntime; using System.Threading.Tasks; using WhereWeGo.Helper; using Windows.ApplicationModel.Search; using Windows.Foundation; using Windows.Foundation.Collections; using Windows.Storage; using Windows.UI; using Windows.UI.Popups; using Windows.UI.Xaml; using Windows.UI.Xaml.Controls; using Windows.UI.Xaml.Controls.Primitives; using Windows.UI.Xaml.Data; using Windows.UI.Xaml.Input; using Windows.UI.Xaml.Media; using Windows.UI.Xaml.Media.Animation; using Windows.UI.Xaml.Navigation; //「中心頁」項模板在 http://go.microsoft.com/fwlink/?

LinkID=321224 上有介紹 namespace demo02 { public sealed partial class HubPage : Page { //一個容器,裏面存有每個圖片的url對象 //將JSON中存的圖片的uri讀取到PicArray。而後經過PicArray下載圖片 private static OfflinePicArray PicArray { get; set; } private NavigationHelper navigationHelper; private ObservableDictionary defaultViewModel = new ObservableDictionary(); public HubPage() { this.InitializeComponent(); this.navigationHelper = new NavigationHelper(this); this.navigationHelper.LoadState += navigationHelper_LoadState; } private async void navigationHelper_LoadState(object sender, LoadStateEventArgs e) { if (ConnectState.isConnected) //假設網絡是鏈接的。進行緩存下載 { if (OfflineFirstFlag.isFirstEnter) //由於以後會屢次進入HubPage頁面,咱們僅僅應當在第一次進入頁面時進行下載 { OfflineFirstFlag.isFirstEnter = false; //得到圖片資源。先下載存有uri的JSON。再解析JSON,再經過解析出的uri下載圖片 //獲取存實用於離線模式下圖片uri的JSON await HttpGetOfflinePICSJSON(); //獲取存有老師和地域圖片的url的JSON await HttpGetOfflineTeacherRegionJSON(); //得到第一部分圖片 await HttpGetOfflingPicByUrl(); //得到第二部分圖片 await HttpGetOfflingPicByUrl02(); //得到第三部分圖片 await HttpGetOfflingPicByUrl03(); //得到第四部分圖片 await HttpGetOfflingPicByUrl04(); //得到文字資源 //得到離線模式數據展現的來源JSON await HttpGetOfflineJSON(); } } } //向server請求最新的PICS JSON private async static Task HttpGetOfflinePICSJSON() { XDownload xmen = new XDownload(ServiceUrl.URL_OFFLINE_PICSJSON, XDownload.App, OfflineSubFolder.Name, ServiceUrl.PICSJSON_LOCALNAME); await xmen.GetDataAsync(); } private async static Task HttpGetOfflingPicByUrl() { try { StorageFolder storageFolder = Windows.ApplicationModel.Package.Current.InstalledLocation; StorageFolder folder = await storageFolder.GetFolderAsync(OfflineSubFolder.Name); StorageFile storageFile = await folder.GetFileAsync(ServiceUrl.PICSJSON_LOCALNAME); if (storageFile != null) { XDownload bigOverhead = new XDownload(); bigOverhead.Library = XDownload.App; bigOverhead.Myfolder = OfflineSubFolder.Name; // 獲取指定的文件裏的文本內容 string textContent = await FileIO.ReadTextAsync(storageFile, Windows.Storage.Streams.UnicodeEncoding.Utf8); PicArray = JsonConvert.DeserializeObject<OfflinePicArray>(textContent); foreach (OfflinePicUri onePic in PicArray.Images) { foreach (string picName in onePic.ShowImages) { if (picName != null) { bigOverhead.Uri = ServiceUrl.BASEURL_OFFLINE + onePic.ImageFolder + picName; System.Diagnostics.Debug.WriteLine(bigOverhead.Uri + "kakakakakakkakakak"); bigOverhead.Filename = picName; await bigOverhead.GetDataAsync(); } } } } catch(Exception ex) { } } private async static Task HttpGetOfflingPicByUrl02() { try { StorageFolder storageFolder = Windows.ApplicationModel.Package.Current.InstalledLocation; StorageFolder folder = await storageFolder.GetFolderAsync(OfflineSubFolder.Name); StorageFile storageFile = await folder.GetFileAsync(ServiceUrl.PICSJSON_LOCALNAME); if (storageFile != null) { XDownload bigOverhead = new XDownload(); bigOverhead.Library = XDownload.App; bigOverhead.Myfolder = OfflineSubFolder.Name; // 獲取指定的文件裏的文本內容 string textContent = await FileIO.ReadTextAsync(storageFile, Windows.Storage.Streams.UnicodeEncoding.Utf8); PicArray = JsonConvert.DeserializeObject<OfflinePicArray>(textContent); foreach (OfflinePicUri onePic in PicArray.Images) { if (onePic.TileImagePath != null) { bigOverhead.Uri = ServiceUrl.BASEURL_OFFLINE + onePic.ImageFolder + onePic.TileImagePath; bigOverhead.Filename = onePic.TileImagePath; await bigOverhead.GetDataAsync(); } } } } catch (Exception ex) { } } private async static Task HttpGetOfflingPicByUrl03() { try { StorageFolder storageFolder = Windows.ApplicationModel.Package.Current.InstalledLocation; StorageFolder folder = await storageFolder.GetFolderAsync(OfflineSubFolder.Name); StorageFile storageFile = await folder.GetFileAsync(ServiceUrl.PICSJSON_LOCALNAME); if (storageFile != null) { XDownload bigOverhead = new XDownload(); bigOverhead.Library = XDownload.App; bigOverhead.Myfolder = OfflineSubFolder.Name; // 獲取指定的文件裏的文本內容 string textContent = await FileIO.ReadTextAsync(storageFile, Windows.Storage.Streams.UnicodeEncoding.Utf8); PicArray = JsonConvert.DeserializeObject<OfflinePicArray>(textContent); foreach (OfflinePicUri onePic in PicArray.Images) { if (onePic.Badge != null) { bigOverhead.Uri = ServiceUrl.BASEURL_OFFLINE + onePic.ImageFolder + onePic.Badge; bigOverhead.Filename = onePic.Badge; await bigOverhead.GetDataAsync(); } } } } catch(Exception ex) { } } private async Task HttpGetOfflingPicByUrl04() { try { StorageFolder storageFolder = Windows.ApplicationModel.Package.Current.InstalledLocation; StorageFolder folder = await storageFolder.GetFolderAsync(OfflineSubFolder.Name); //接着下載老師和地域的圖片 StorageFile storageFile01 = await folder.GetFileAsync(ServiceUrl.T_R_JSON_LOCALNAME); if (storageFile01 != null) { XDownload bigOverhead = new XDownload(); bigOverhead.Library = XDownload.App; bigOverhead.Myfolder = OfflineSubFolder.Name; // 獲取指定的文件裏的文本內容 string textContent = await FileIO.ReadTextAsync(storageFile01, Windows.Storage.Streams.UnicodeEncoding.Utf8); TRPic tr = new TRPic(); tr = JsonConvert.DeserializeObject<TRPic>(textContent); foreach (string onePic in tr.TeacherImages) { //開始下載圖片 bigOverhead.Uri = ServiceUrl.BASEURL_OFFLINE + onePic; bigOverhead.Filename = TeaRegFilter.GetPicName(onePic); await bigOverhead.GetDataAsync(); } } }catch (Exception ex) { } } private async static Task HttpGetOfflineJSON() { XDownload xmen = new XDownload(ServiceUrl.URL_OFFLINE_JSON, XDownload.App, OfflineSubFolder.Name, ServiceUrl.JSON_LOCALNAME); await xmen.GetDataAsync(); } private async static Task HttpGetOfflineTeacherRegionJSON() { XDownload xmen = new XDownload(ServiceUrl.URL_OFFLINE_TEACHER_REGION_JSON, XDownload.App, OfflineSubFolder.Name, ServiceUrl.T_R_JSON_LOCALNAME); await xmen.GetDataAsync(); } } } using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Net.Http; using System.Text; using System.Threading.Tasks; using Windows.Networking.BackgroundTransfer; using Windows.Storage; using Windows.Storage.Streams; namespace demo02.Helper { //該類的做用是將server端的文件uri經過http下載到本地指定文件夾,並命名 public class XDownload { public static string Documents = "Documents"; public static string Pictures = "Pictures"; public static string Music = "Music"; public static string Videos = "Videos"; public static string App = "App"; public XDownload() { this.Uri = ""; this.Library = ""; this.Myfolder = ""; this.Filename = ""; } public XDownload(String uri, String library, String myfolder, String filename) { this.Uri = uri; this.Library = library; this.Myfolder = myfolder; this.Filename = filename; } //不帶返回值的Task public async Task GetDataAsync() { StorageFolder folder = null; try { switch (this.Library) { case "Documents": folder = KnownFolders.DocumentsLibrary; break; case "Pictures": folder = KnownFolders.PicturesLibrary; break; case "Music": folder = KnownFolders.MusicLibrary; break; case "Videos": folder = KnownFolders.VideosLibrary; break; case "App": folder = Windows.ApplicationModel.Package.Current.InstalledLocation; //C:\Users\tom\Documents\GitHub\wherewegov1\wherewego\demo02\bin\Debug\AppX break; default: System.Diagnostics.Debug.WriteLine("ERROR: Make sure you are using the four special library: Documents, Music, Videos, Pictures"); return; } if (this.Myfolder == "") { System.Diagnostics.Debug.WriteLine("ERROR: Make sure you input a valid subfolder"); return; } StorageFolder childFolder = await folder.CreateFolderAsync(this.Myfolder, CreationCollisionOption.OpenIfExists); if (this.Filename == "") { System.Diagnostics.Debug.WriteLine("ERROR: Make sure you input a valid filename"); return; } //Creates a new file in the current folder, and specifies what to do if //a file with the same name already exists in the current folder. StorageFile outputFile = await childFolder.CreateFileAsync(this.Filename, CreationCollisionOption.ReplaceExisting); //選擇恰當的流,避免下載圖片過程當中僵死 var fs = await outputFile.OpenAsync(FileAccessMode.ReadWrite); HttpClientHandler handler = new HttpClientHandler(); handler.ClientCertificateOptions = ClientCertificateOption.Automatic; HttpClient hc = new HttpClient(handler); HttpResponseMessage response = await hc.GetAsync(this.Uri, HttpCompletionOption.ResponseHeadersRead); Stream stream = await response.Content.ReadAsStreamAsync(); IInputStream inputStream = stream.AsInputStream(); ulong totalBytesRead = 0; while (true) { // Read from the web. IBuffer buffer = new Windows.Storage.Streams.Buffer(1024); buffer = await inputStream.ReadAsync( buffer, buffer.Capacity, InputStreamOptions.None); if (buffer.Length == 0) { // 完畢 break; } // 進度 totalBytesRead += buffer.Length; System.Diagnostics.Debug.WriteLine("Bytes read: {0}", totalBytesRead); // 寫文件. await fs.WriteAsync(buffer); } inputStream.Dispose(); fs.Dispose(); } catch (Exception) { System.Diagnostics.Debug.WriteLine("ERROR: file download failed, tai can le"); } } public string Uri { get; set; } public string Library { get; set; } public string Myfolder { get; set; } public string Filename { get; set; } } }
相關文章
相關標籤/搜索