Windows Phone 8.1 Data processing

Source: Internet
Author: User



Data storage locations for Windows Phone 8.1 apps include:


    1. Installation Folder
    2. ApplicationData
    3. Credential Locker
    4. Known Folders
    5. SD Card
    6. File System
    7. Networks


Attached Picture:









(1) Installation Folder



Installation Folder is a developer's own files added to the project and can only be read .



For example, the project structure is:






To use a picture in the Images folder, you can write this in XAML:


<Image x: Name = "image"
        Source = "/ Images / screen (157) .png"
        Stretch = "Uniform" /> 


To use in C # code, add ms-appx:///before the file path


 
protected override void OnNavigatedTo(NavigationEventArgs e)
{ BitmapImage img = new BitmapImage(new Uri("ms-appx:///Images/屏幕(157).png")); image.Source = img;
}


Note: The build action for the file is content









(2) ApplicationData



ApplicationData is the application of independent storage, can be freely read and write operations, divided into the following three areas:



1) Local (folder,settings)



Saved on the phone, there is no storage limit and will be retained when the app is updated, including Folder and Settings.



The way to use it is simple:


StorageFolder folder = ApplicationData.Current.LocalFolder;
ApplicationDataContainer settings = ApplicationData.Current.LocalSettings;


Folder operation is also very simple, of course, can also be manipulated by the Stream, as follows:


StorageFolder folder = ApplicationData.Current.LocalFolder;
await folder.CreateFileAsync("some.txt");
await folder.CreateFolderAsync("some");
await folder.GetFileAsync("some.txt");
await folder.GetFolderAsync("some");
await folder.OpenStreamForReadAsync("some.txt");
await folder.OpenStreamForWriteAsync("some.txt", CreationCollisionOption.FailIfExists);


The Settings is a key-value array that holds simple data, type <string, Object>, so the values are disassembled when the data is read:


ApplicationDataContainer settings = ApplicationData.Current.LocalSettings;

settings.Values["some"] = "some";

if( settings.Values.ContainsKey("some") )
{
    string some = (string)settings.Values["some"];
}





2) Roaming (folder,settings)



Saved in the cloud OneDrive, can be used across devices, but with 100Kb restrictions, also includes Folder and Settings.



The operation of the Roaming data is consistent with the local operation, and it is important to note that the 100kb limit, as well as the event datachanged triggered when the roaming data changes, such as the ability to update local data in this event:


 
 
public MainPage()
{
    this.InitializeComponent();

    ApplicationData.Current.DataChanged += Current_DataChanged;
}

private void Current_DataChanged(ApplicationData sender, object args)
{
    if( localSettings.Values.ContainsKey("test") && roamingSettings.Values.ContainsKey("test") )
    {
        localSettings.Values["test"] = roamingSettings.Values["test"];
    }
}


Note: The app must obtain the store's certification to use data roaming.






3) Temp (Folder)



Temporary data saved on the phone is not guaranteed when it is deleted (i.e. it is deleted automatically when there is not enough storage space), only Folder.



The method of use is consistent with Local and Roaming, and different things it is able to use Folder.






In addition to the above methods of accessing ApplicationData, the same Uri can be used to access the above Folder, simply add ms-appdata:///before the path:


 
 
var localFile = StorageFile.GetFileFromApplicationUriAsync(new Uri("ms-appdata:///local/some.txt"));
var roamingFile = StorageFile.GetFileFromApplicationUriAsync(new Uri("ms-appdata:///roaming/some.txt"));
var tempFile = StorageFile.GetFileFromApplicationUriAsync(new Uri("ms-appdata:///temp/some.txt"));





(3) Credential Locker



Credential Locak is a private space for storing user accounts and password pairs, is not accessed by other apps, and also supports cross-device roaming with the app.



How to use:


 
private void saveButton_Click(object sender, RoutedEventArgs e)
{
    string username = idTextBox.Text.Trim();
    string password = passwordBox.Password.Trim();

    PasswordCredential cred = new PasswordCredential("weibo", username, password);

    PasswordVault value = new PasswordVault();
    value.Add(cred);
}

private void loadButton_Click(object sender, RoutedEventArgs e)
{
    PasswordVault value = new PasswordVault();
    var creds = value.FindAllByResource("weibo");

    if( creds.Count != 0 )
    {
        foreach( var cred in creds )
        {
            cred.RetrievePassword();
            messageTextBlock.Text += string.Format("{0}\n{1}\n{2}\n", cred.Resource, cred.UserName, cred.Password);
        }
    }
}


Note: You must first call the Retrievepassword method when you get the password, otherwise the password you get is an empty string.






(4) Known Folders



Known Folders is the special folder on the phone, such as music video files.



The Get method is:


StorageFolder DocumentsLibrary = KnownFolders.DocumentsLibrary;
StorageFolder MusicLibrary = KnownFolders.MusicLibrary;
StorageFolder VideosLibrary = KnownFolders.VideosLibrary;
StorageFolder PicturesLibrary = KnownFolders.PicturesLibrary;
StorageFolder CameraRoll = KnownFolders.CameraRoll;
StorageFolder SavedPictures = KnownFolders.SavedPictures;
StorageFolder RemovableDevices = KnownFolders.RemovableDevices;

StorageFolder HomeGroup = KnownFolders.HomeGroup;
StorageFolder MediaServerDevices = KnownFolders.MediaServerDevices;


The above is all available known Folders, where HomeGroup and mediaserverdevices folders are temporarily not open.



Note: Some folders need to be read in appxmanifest to get permission:









(5) SD Card



The SD Card file read also needs to get permissions in Appxmanifest.



The Get method is:


 
var devices = KnownFolders.RemovableDevices;
var sdCards = await devices.GetFoldersAsync();

if( sdCards.Count == 0 )
    return;

StorageFolder firstCard = sdCards[0];


Note that the file read for SD Card requires a file type association to be added to the appxmanifest in advance to determine the type of file that needs to be read:









(6) Remark





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.