Win8.1 offline cache for application development and win8.1 Offline Application Development

Source: Internet
Author: User

Win8.1 offline cache for application development and win8.1 Offline Application Development

When developing app Store applications, we need the app to have the cache function, so that it can still work in offline mode. The project we selected is Hub.

The policy adopted here is: In HubPage. xaml. cs (the reason why App is not selected. xaml. cs is used to allow users to download images on one side of the operation interface.) await asynchronous programming is used to avoid blocking the UI and read the JSON file containing the image path first, parse the JSON to get the URI of each image, and then download the Image Based on the URI. For text resources, download the JSON directly. Pay special attention to file operations-File permissions, and the selection of file streams-improper selection may result in occasional rigidity when downloading images.

In 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. 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; // "Center page" item template in http://go.microsoft.com/fwlink? LinkID = 321224 describes namespace demo02 {public sealed partial class HubPage: Page {// a container, contains the url object of each image // read the uri of the image saved in JSON to PicArray, and then download the image private static OfflinePicArray PicArray {get; set;} private NavigationHelper navigationHelper through PicArray; private ObservableDictionary defaultViewModel = new ObservableDictionary (); public HubPage () {this. initializeComponent (); this. navigationHelper = new Naviga TionHelper (this); this. navigationHelper. loadState + = navigationHelper_LoadState;} private async void navigationHelper_LoadState (object sender, LoadStateEventArgs e) {if (ConnectState. isConnected) // if the network is connected, perform cache download {if (OfflineFirstFlag. isFirstEnter) // Since the HubPage will be accessed multiple times later, we should only download {OfflineFirstFlag when entering the page for the first time. isFirstEnter = false; // obtain the image resource. First download the JSON containing the uri, parse the JSON, and then download the image using the parsed uri. // obtain the JSON containing the uri used for offline mode. Await HttpGetOfflinePICSJSON (); // obtain the JSON await HttpGetOfflineTeacherRegionJSON () with the url containing the instructor and region image; // obtain the await HttpGetOfflingPicByUrl () of the first part of the image (); // obtain await HttpGetOfflingPicByUrl02 (); // obtain await HttpGetOfflingPicByUrl03 (); // obtain await HttpGetOfflingPicByUrl04 (); // obtain the text resource // obtain the source JSON await HttpGetOfflineJSON () ;}}// request the server for the latest pics json private async static Task HttpGetOfflinePICSJ SON () {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; // get the text content in the specified file string textContent = await FileIO. readTextAsync (storageFile, Windows. storage. streams. unicodeEncoding. utf8); PicArray = JsonConvert. deserializeObject <OfflinePicArray> (textContent); foreach (OfflinePicUri onePic in PicArray. images) {foreach (string pi CName in onePic. ShowImages) {if (picName! = Null) {bigOverhead. uri = ServiceUrl. BASEURL_OFFLINE + onePic. imageFolder + picName; System. diagnostics. debug. writeLine (bigOverhead. uri + "kakakakakakakakak"); bigOverhead. filename = picName; await bigOverhead. getDataAsync () ;}}} catch (Exception ex) {}} private async static Task HttpGetOfflingPicByUrl02 () {try {StorageFolder storageFolder = Windows. applicationModel. package. current. instal LedLocation; StorageFolder folder = await storageFolder. GetFolderAsync (metadata. Name); StorageFile storageFile = await folder. GetFileAsync (ServiceUrl. PICSJSON_LOCALNAME); if (storageFile! = Null) {XDownload bigOverhead = new XDownload (); bigOverhead. library = XDownload. app; bigOverhead. myfolder = OfflineSubFolder. name; // get the text content in the specified file string textContent = await FileIO. readTextAsync (storageFile, Windows. storage. streams. unicodeEncoding. utf8); PicArray = JsonConvert. deserializeObject <OfflinePicArray> (textContent); foreach (OfflinePicUri onePic in PicArray. images) {if (onePic. tileIma GePath! = 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 storage Folder. 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; // get the text content in the specified file 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 (FlineSubFolder. Name); // download the 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; // get the text content in the specified file 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) {// starts to download the bigOverhead image. 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 {// The role of this class is to download the server-side file uri to a specified local directory through http, and name it 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 "events": 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 \ AppXbreak; 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); // select an appropriate stream to avoid freezing var fs = await outputFile during image download. 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) {// complete break;} // progress totalBytesRead + = buffer. length; System. diagnostics. debug. writeLine ("Bytes read: {0}", totalBytesRead); // write a file. 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 ;}}}



How to clear win81 application Cache

Clean up junk data with new drug overlord

These should be the cache of metro applications.


 
How can the win81 system quickly clear the metro application cache and safely delete old win8 applications?

Install 360 security guard to completely clean up cache and useless system Spam!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.