Windows 8 Store Apps學習(24) Application Data和Package中的檔案操作

來源:互聯網
上載者:User

檔案系統: Application Data 中的檔案操作, Package 中的檔案操作

介紹

重新想象 Windows 8 Store Apps 之 檔案系統

Application Data(應用程式資料儲存) 中的檔案操作

Application Data(應用程式資料儲存) 中的“設定”操作

通過 uri 引用 Application Data(應用程式資料儲存) 中的媒體(圖片、視頻或音頻)

訪問 Package 中的檔案

訪問抽取式存放裝置

樣本

1、示範如何在 Application Data(應用程式資料儲存)中對檔案進行操作

FileSystem/AppData/FileDemo.xaml.cs

/*
* 示範如何在 Application Data(應用程式資料儲存)中對檔案進行操作
*
* ApplicationData - 操作應用程式資料儲存的類
* Current - 返回當前的 ApplicationData 對象
* LocalFolder - 返回 StorageFolder 對象。本機存放區,永久儲存
* 儲存路徑:%USERPROFILE%\AppData\Local\Packages\{PackageId}\LocalState
* RoamingFolder - 返回 StorageFolder 對象。漫遊儲存,同一微軟賬戶同一應用程式在不同裝置間會自動同步
* 儲存路徑:%USERPROFILE%\AppData\Local\Packages\{PackageId}\RoamingState
* TemporaryFolder - 返回 StorageFolder 對象。臨時儲存,系統在需要的時候可以將其刪除
* 儲存路徑:%USERPROFILE%\AppData\Local\Packages\{PackageId}\TempState
* RoamingStorageQuota - 從漫遊儲存同步到雲端的資料的最大大小,唯讀
* ClearAsync() - 刪除 Application Data 中的資料* ClearAsync(ApplicationDataLocality locality) - 刪除指定儲存區的資料據
* ApplicationDataLocality.Local, ApplicationDataLocality.Roaming, ApplicationDataLocality.Temporary
*
* DataChanged - 從服務端向 app 同步漫遊資料時所觸發的事件
* SignalDataChanged() - 強行觸發 DataChanged 事件
*/
using System;
using Windows.Storage;
using Windows.UI.Core;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Navigation;
namespace XamlDemo.FileSystem.AppData
{
public sealed partial class FileDemo : Page
{
StorageFolder _roamingFolder = ApplicationData.Current.RoamingFolder;
public FileDemo()
{
this.InitializeComponent();
}
protected override void OnNavigatedTo(NavigationEventArgs e)
{
ApplicationData.Current.DataChanged += Current_DataChanged;
}
protected override void OnNavigatedFrom(NavigationEventArgs e)
{
ApplicationData.Current.DataChanged -= Current_DataChanged;
}
async void Current_DataChanged(ApplicationData sender, object args)
{
await Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
{
lblMsg.Text += Environment.NewLine;
lblMsg.Text += "DataChanged 事件被觸發";
});
}
private async void btnReadWrite_Click_1(object sender, RoutedEventArgs e)
{
// 寫
StorageFile fileWrite = await _roamingFolder.CreateFileAsync(@"webabcdTest\readWriteDemo.txt", CreationCollisionOption.ReplaceExisting);
await FileIO.WriteTextAsync(fileWrite, "I am webabcd: " + DateTime.Now.ToString());
// 讀
// StorageFile fileRead = await _roamingFolder.GetFileAsync(@"webabcdTest\readWriteDemo.txt");
// ms-appdata:///local/, ms-appdata:///roaming/, ms-appdata:///temp/
StorageFile fileRead = await StorageFile.GetFileFromApplicationUriAsync(new Uri("ms-appdata:///roaming/webabcdTest/readWriteDemo.txt", UriKind.Absolute));
string textContent = await FileIO.ReadTextAsync(fileRead);
lblMsg.Text = textContent;
// 強行觸發 DataChanged 事件(僅為示範用,實際項目中不需要)
ApplicationData.Current.SignalDataChanged();
}
}
}

2、示範如何在 Application Data(應用程式資料儲存)中對“設定”進行操作

FileSystem/AppData/SettingsDemo.xaml

<Page    x:Class="XamlDemo.FileSystem.AppData.SettingsDemo"    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"    xmlns:local="using:XamlDemo.FileSystem.AppData"    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"    mc:Ignorable="d">        <Grid Background="Transparent">        <StackPanel Margin="120 0 0 0">                <TextBlock Name="lblMsg" FontSize="14.667" />                <Button Name="btnReadWrite" Content="讀寫 Settings" Click="btnReadWrite_Click_1" Margin="0 10 0 0" />                <Button Name="btnReadWriteWithContainer" Content="分組 Settings" Click="btnReadWriteWithContainer_Click_1" Margin="0 10 0 0" />                <Button Name="btnReadWriteWithComposite" Content="父子 Settings" Click="btnReadWriteWithComposite_Click_1" Margin="0 10 0 0" />                <Button Name="btnSetVersion0" Content="將 Application Data 的版本號碼設定為 0" Click="btnSetVersion0_Click_1" Margin="0 10 0 0" />                <Button Name="btnSetVersion1" Content="將 Application Data 的版本號碼設定為 1" Click="btnSetVersion1_Click_1" Margin="0 10 0 0" />            </StackPanel>    </Grid></Page>

FileSystem/AppData/SettingsDemo.xaml.cs

/*
* 示範如何在 Application Data(應用程式資料儲存)中對“設定”進行操作
*
* ApplicationData - 操作應用程式資料儲存的類
* Current - 返回當前的 ApplicationData 對象
* LocalSettings - 返回 ApplicationDataContainer 對象。本機存放區,永久儲存
* 儲存路徑:%USERPROFILE%\AppData\Local\Packages\{PackageId}\Settings
* RoamingSettings - 返回 ApplicationDataContainer 對象。漫遊儲存,同一微軟賬戶同一應用程式在不同裝置間會自動同步
* 儲存路徑:%USERPROFILE%\AppData\Local\Packages\{PackageId}\Settings
* Version - 擷取當前 Application Data 的版本號碼,預設值為 0,唯讀(用於本地“設定”資料的版本控制)
* SetVersionAsync() - 指定當前 Application Data 的版本號碼(用於本地“設定”資料的版本控制)
* * ApplicationDataContainer - 操作“設定”資料的類
* Name - 容器的名稱,預設為空白 * CreateContainer(string name, ApplicationDataCreateDisposition disposition) - 啟用一個用於儲存“設定”資料的容器,即分組“設定”資料
* name - 容器的名稱
* disposition - 容器的啟用方式:Always - 始終啟用;Existing - 容器存在才啟用
* Containers - 容器集合
* DeleteContainer() - 刪除指定的容器
* Values - 儲存“設定”資料,一個字典表
* 其資料可以是一個 ApplicationDataCompositeValue 類型的資料,ApplicationDataCompositeValue 也是一個字典表,這樣可將多個“設定”資料放到一個 key 裡
* *
* 備忘:
* 當 key 為 HighPriority 時,系統會以最快的速度在多個裝置間同步 HighPriority 所對應的資料(支援 ApplicationDataCompositeValue 資料)
* 樣本如下:
* ApplicationDataContainer.Values["HighPriority"] = "此處的值將會以系統最快的速度在多個裝置間同步";
*/
using System;
using Windows.Storage;
using Windows.UI.Core;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
namespace XamlDemo.FileSystem.AppData
{
public sealed partial class SettingsDemo : Page
{
public SettingsDemo()
{
this.InitializeComponent();
}
// 操作“設定”資料的 demo
private void btnReadWrite_Click_1(object sender, RoutedEventArgs e)
{
ApplicationDataContainer localSettings = ApplicationData.Current.LocalSettings;
// 儲存“設定”資料
localSettings.Values["key"] = "I am webabcd";
// 如果 key 為 HighPriority,則系統將會以最高優先順序的速度在多個裝置間同步 HighPriority 的值
// localSettings.Values["HighPriority"] = "I am webabcd";
// 刪除指定的“設定”資料
// localSettings.Values.Remove("key");
// 擷取“設定”資料
lblMsg.Text = (string)localSettings.Values["key"];
}
// 分組“設定”資料,即在不同的容器中儲存不同的資料
private void btnReadWriteWithContainer_Click_1(object sender, RoutedEventArgs e)
{
// 在 LocalSettings 中啟用名為 groupName 的容器
ApplicationDataContainer container = ApplicationData.Current.LocalSettings;
ApplicationDataContainer localSettings = container.CreateContainer("groupName", ApplicationDataCreateDisposition.Always);
// 刪除指定的容器
// container.DeleteContainer("groupName");
// 在容器內儲存“設定”資料
if (container.Containers.ContainsKey("groupName"))
{
container.Containers["groupName"].Values["key"] = "I am webabcd";
}
// 從指定的容器內擷取“設定”資料
lblMsg.Text = (string)container.Containers["groupName"].Values["key"];
lblMsg.Text += Environment.NewLine;
// 從指定的容器內擷取“設定”資料
lblMsg.Text += (string)localSettings.Values["key"];
}
// 父子“設定”資料,即 key 中的資料是一個 ApplicationDataCompositeValue 對象,而 ApplicationDataCompositeValue 也是一個字典表
private void btnReadWriteWithComposite_Click_1(object sender, RoutedEventArgs e)
{
ApplicationDataContainer localSettings = ApplicationData.Current.LocalSettings;
// 父子“設定”資料的儲存
ApplicationDataCompositeValue parent1 = new ApplicationDataCompositeValue();
parent1["child1"] = "abc";
parent1["child2"] = "xyz";
localSettings.Values["parent1"] = parent1;
// 父子“設定”資料的擷取
lblMsg.Text = (string)((ApplicationDataCompositeValue)localSettings.Values["parent1"])["child1"];
lblMsg.Text += Environment.NewLine;
lblMsg.Text += (string)((ApplicationDataCompositeValue)localSettings.Values["parent1"])["child2"];
}
private async void btnSetVersion0_Click_1(object sender, RoutedEventArgs e)
{
// 將 Application Data 的版本號碼設定為 0,並執行指定的方法
await ApplicationData.Current.SetVersionAsync(0, new ApplicationDataSetVersionHandler(SetVersionHandler));
}
private async void btnSetVersion1_Click_1(object sender, RoutedEventArgs e)
{
// 將 Application Data 的版本號碼設定為 1,並執行指定的方法
await ApplicationData.Current.SetVersionAsync(1, new ApplicationDataSetVersionHandler(SetVersionHandler));
}
// 根據目前的版本號和將要設定成的版本號碼,將“設定”資料升級到最新版本
async void SetVersionHandler(SetVersionRequest request)
{
// 非同步作業
SetVersionDeferral deferral = request.GetDeferral();
await Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
{
lblMsg.Text = "CurrentVersion: " + request.CurrentVersion; // 目前的版本號
lblMsg.Text += Environment.NewLine;
lblMsg.Text += "DesiredVersion: " + request.DesiredVersion; // 將要設定成的版本號碼
lblMsg.Text += Environment.NewLine;
lblMsg.Text += "ApplicationData.Current.Version: " + ApplicationData.Current.Version; // 目前的版本號
});
// 完成非同步作業
deferral.Complete();
}
}
}

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.