Windows 8 Store Apps學習(28) 選取器: CachedFileUpdater

來源:互聯網
上載者:User

選取器: CachedFileUpdater(快取檔案更新程式)

介紹

重新想象 Windows 8 Store Apps 之 選取器

CachedFileUpdater - 快取檔案更新程式

樣本

一、首先建立一個 Windows 市集項目,使其作為快取檔案更新程式

1、 開啟一個檔案,並關 聯到 CachedFileUpdater

CachedFileUpdaterProvider/MyOpenPicker.xaml

<Page    x:Class="CachedFileUpdaterProvider.MyOpenPicker"    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"    xmlns:local="using:CachedFileUpdaterProvider"    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="btnPickCachedFileLocal" Content="提供一個由 Local 更新的 CachedFile(由 CachedFileUpdater 更新 CachedFile)" Click="btnPickCachedFileLocal_Click_1" Margin="0 10 0 0" />                <Button Name="btnPickCachedFileRemote" Content="提供一個由 Remote 更新的 CachedFile(由 app 更新 CachedFile)" Click="btnPickCachedFileRemote_Click_1" Margin="0 10 0 0" />            </StackPanel>    </Grid></Page>

CachedFileUpdaterProvider/MyOpenPicker.xaml.cs

/* * 開啟一個檔案,並關聯到 CachedFileUpdater *  * 1、在 Package.appxmanifest 中新增一個“檔案開啟選取器”聲明,並做相關配置 * 2、在 App.xaml.cs 中 override void OnFileOpenPickerActivated(FileOpenPickerActivatedEventArgs args),如果 app 是由檔案開啟選取器啟用的,則可以在此擷取其相關資訊 */    using System;using Windows.ApplicationModel.Activation;using Windows.Storage;using Windows.Storage.Pickers.Provider;using Windows.Storage.Provider;using Windows.UI.Xaml.Controls;using Windows.UI.Xaml.Navigation;    namespace CachedFileUpdaterProvider{    public sealed partial class MyOpenPicker : Page    {        private FileOpenPickerUI _fileOpenPickerUI;            public MyOpenPicker()        {            this.InitializeComponent();        }            protected override void OnNavigatedTo(NavigationEventArgs e)        {            var args = (FileOpenPickerActivatedEventArgs)e.Parameter;            _fileOpenPickerUI = args.FileOpenPickerUI;                _fileOpenPickerUI.Title = "自訂檔案開啟選取器";        }            // 本 CachedFile 用於從 Local 更新        private async void btnPickCachedFileLocal_Click_1(object sender, Windows.UI.Xaml.RoutedEventArgs e)        {            StorageFile file = await ApplicationData.Current.LocalFolder.CreateFileAsync(@"webabcdCachedFileUpdaterLocal.txt", CreationCollisionOption.ReplaceExisting);            string textContent = "I am webabcd";                await FileIO.WriteTextAsync(file, textContent);                /*             * 設定 CachedFile,即將檔案關聯到 CachedFileUpdater             * SetUpdateInformation(IStorageFile file, string contentId, ReadActivationMode readMode, WriteActivationMode writeMode, CachedFileOptions options);             *     file - 與 CachedFileUpdater 關聯的檔案             *     contentId - 與 CachedFileUpdater 關聯的檔案標識             */            CachedFileUpdater.SetUpdateInformation(file, "cachedFileLocal", ReadActivationMode.BeforeAccess, WriteActivationMode.NotNeeded, CachedFileOptions.RequireUpdateOnAccess);                lblMsg.Text = "選擇的檔案: " + file.Name;            AddFileResult result = _fileOpenPickerUI.AddFile("myFile", file);        }            // 本 CachedFile 用於從 Remote 更新        private async void btnPickCachedFileRemote_Click_1(object sender, Windows.UI.Xaml.RoutedEventArgs e)        {            StorageFile file = await ApplicationData.Current.LocalFolder.CreateFileAsync(@"webabcdCachedFileUpdaterRemote.txt", CreationCollisionOption.ReplaceExisting);            string textContent = "I am webabcd";                await FileIO.WriteTextAsync(file, textContent);                /*             * 設定 CachedFile,即將檔案關聯到 CachedFileUpdater             * SetUpdateInformation(IStorageFile file, string contentId, ReadActivationMode readMode, WriteActivationMode writeMode, CachedFileOptions options);             *     file - 與 CachedFileUpdater 關聯的檔案             *     contentId - 與 CachedFileUpdater 關聯的檔案標識             */            CachedFileUpdater.SetUpdateInformation(file, "cachedFileRemote", ReadActivationMode.NotNeeded, WriteActivationMode.AfterWrite, CachedFileOptions.RequireUpdateOnAccess);                lblMsg.Text = "選擇的檔案: " + file.Name;            AddFileResult result = _fileOpenPickerUI.AddFile("myFile", file);        }    }}

2、 開發自訂快取檔案更新程式

CachedFileUpdaterProvider/MyCachedFileUpdater.xaml

<Page    x:Class="CachedFileUpdaterProvider.MyCachedFileUpdater"    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"    xmlns:local="using:CachedFileUpdaterProvider"    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="btnUpdate" Content="更新 CachedFile(由 CachedFileUpdater 更新 CachedFile)" Click="btnUpdate_Click_1" Margin="0 10 0 0" />            </StackPanel>    </Grid></Page>

CachedFileUpdaterProvider/MyCachedFileUpdater.xaml.cs

/* * 示範如何開發自訂快取檔案更新程式 *  * 1、在 Package.appxmanifest 中新增一個“快取檔案更新程式”聲明,並做相關配置 * 2、在 App.xaml.cs 中 override void OnCachedFileUpdaterActivated(CachedFileUpdaterActivatedEventArgs args),如果 app 是由檔案開啟選取器啟用的,則可以在此擷取其相關資訊 *  * CachedFileUpdaterActivatedEventArgs - 通過“快取檔案更新程式”啟用應用程式時的事件參數 *     CachedFileUpdaterUI - 擷取 CachedFileUpdaterUI 對象 *     PreviousExecutionState, Kind, SplashScreen - 各種啟用 app 的方式的事件參數基 *  * CachedFileUpdaterUI - 快取檔案更新程式的協助類 *     Title - 將在“快取檔案更新程式”上顯示的標題 *     UIStatus - “快取檔案更新程式”的 UI 狀態(Unavailable, Hidden, Visible, Complete) *     UpdateTarget - Local 代表由 CachedFileUpdater 更新; Remote 代表由 app 更新 *     UIRequested - 需要顯示“快取檔案更新程式”的 UI 時觸發的事件 *     FileUpdateRequested - 當 app 啟用快取檔案更新程式時,會觸發 FileUpdateRequested 事件(事件參數:CachedFileUpdaterActivatedEventArgs) *      * CachedFileUpdaterActivatedEventArgs *     Request - 返回 FileUpdateRequest 類型的對象 *      * FileUpdateRequest *     File - 關聯的檔案 *     ContentId - 關聯的檔案標識 *     Status - 檔案的更新狀態(FileUpdateStatus 枚舉。Incomplete, Complete, UserInputNeeded, CurrentlyUnavailable, Failed, CompleteAndRenamed) *     GetDeferral() - 擷取非同步作業對象,同時開始非同步作業,之後通過 Complete() 通知完成非同步作業 */    using System;using Windows.ApplicationModel.Activation;using Windows.Storage;using Windows.Storage.Provider;using Windows.UI.Core;using Windows.UI.Xaml;using Windows.UI.Xaml.Controls;using Windows.UI.Xaml.Navigation;    namespace CachedFileUpdaterProvider{    public sealed partial class MyCachedFileUpdater : Page    {        private CachedFileUpdaterUI _cachedFileUpdaterUI;        private FileUpdateRequest _fileUpdateRequest;        private CoreDispatcher _dispatcher = Windows.UI.Xaml.Window.Current.Dispatcher;            public MyCachedFileUpdater()        {            this.InitializeComponent();        }            protected override void OnNavigatedTo(NavigationEventArgs e)        {            // 擷取 CachedFileUpdaterUI 對象            var args = (CachedFileUpdaterActivatedEventArgs)e.Parameter;            _cachedFileUpdaterUI = args.CachedFileUpdaterUI;                _cachedFileUpdaterUI.Title = "快取檔案更新程式";                _cachedFileUpdaterUI.FileUpdateRequested += _cachedFileUpdaterUI_FileUpdateRequested;            _cachedFileUpdaterUI.UIRequested += _cachedFileUpdaterUI_UIRequested;        }            // 需要顯示 CachedFileUpdater 的 UI 時(即當 FileUpdateRequest.Status 等於 UserInputNeeded 時),會調用此事件處理器        async void _cachedFileUpdaterUI_UIRequested(CachedFileUpdaterUI sender, object args)        {            await _dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>            {                Windows.UI.Xaml.Window.Current.Content = this;                lblMsg.Text = "FileUpdateStatus: " + _fileUpdateRequest.Status.ToString();            });        }            void _cachedFileUpdaterUI_FileUpdateRequested(CachedFileUpdaterUI sender, FileUpdateRequestedEventArgs args)        {            _fileUpdateRequest = args.Request;            FileUpdateRequestDeferral fileUpdateRequestDeferral = _fileUpdateRequest.GetDeferral();                if (_cachedFileUpdaterUI.UpdateTarget == CachedFileTarget.Local) // 由 CachedFileUpdater 更新 CachedFile(CachedFileTarget.Local 方式)            {                // 顯示 CachedFileUpdater 的 UI                if (_cachedFileUpdaterUI.UIStatus == UIStatus.Hidden)                {                    _fileUpdateRequest.Status = FileUpdateStatus.UserInputNeeded;                    fileUpdateRequestDeferral.Complete();                }            }            else if (_cachedFileUpdaterUI.UpdateTarget == CachedFileTarget.Remote) // 由 app 更新 CachedFile(CachedFileTarget.Remote 方式)            {                // CachedFileUpdater 返回給 app 一個 FileUpdateStatus 狀態                _fileUpdateRequest.Status = FileUpdateStatus.Complete;                fileUpdateRequestDeferral.Complete();            }        }            private async void btnUpdate_Click_1(object sender, RoutedEventArgs e)        {            FileUpdateRequestDeferral fileUpdateRequestDeferral = _fileUpdateRequest.GetDeferral();                // 由 CachedFileUpdater 更新 CachedFile,然後返回給 app 一個 FileUpdateStatus 狀態            await FileIO.AppendTextAsync(_fileUpdateRequest.File, Environment.NewLine + "由 CachedFileUpdater 更新:" + DateTime.Now.ToString());                string fileContent = await FileIO.ReadTextAsync(_fileUpdateRequest.File);                lblMsg.Text = "檔案名稱: " + _fileUpdateRequest.File.Name;            lblMsg.Text += Environment.NewLine;            lblMsg.Text += "檔案內容: " + fileContent;                _fileUpdateRequest.Status = FileUpdateStatus.Complete;            fileUpdateRequestDeferral.Complete();                btnUpdate.IsEnabled = false;        }    }}

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.