首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> .NET 網頁程式設計 >> 關於.NET

Windows 8 Store Apps 進階功能實作:檔案壓縮、商店操作、App 與 Web 連結、Core API 應用與生命週期管理

     編輯:關於.NET

一、檔案壓縮與解壓縮

Windows.Storage.Compression 命名空間提供四種演算法:Xpress、XpressHuff、Mszip、Lzms。核心類別 Compressor、Decompressor 像資料流一樣操作。

完整範例

private async void CompressionDemo(CompressAlgorithm algorithm)
{
    // 1. 選取檔案
    var picker = new FileOpenPicker(); picker.FileTypeFilter.Add("*");
    var originalFile = await picker.PickSingleFileAsync(); if (originalFile == null) return;

    // 2. 建立壓縮檔 (需在 Package.appxmanifest 宣告 .compressed 檔案關聯)
    var compressedFile = await KnownFolders.DocumentsLibrary.CreateFileAsync(originalFile.Name + ".compressed", CreationCollisionOption.GenerateUniqueName);

    // 3. 壓縮
    using (var input = await originalFile.OpenReadAsync())
    using (var output = await compressedFile.OpenAsync(FileAccessMode.ReadWrite))
    using (var compressor = new Compressor(output.GetOutputStreamAt(0), algorithm, 0))
    {
        var bytesCompressed = await RandomAccessStream.CopyAsync(input, compressor);
        await compressor.FinishAsync();
        // 顯示壓縮前後大小
    }

    // 4. 解壓縮
    var decompressedFile = await KnownFolders.DocumentsLibrary.CreateFileAsync(originalFile.Name + ".decompressed", CreationCollisionOption.GenerateUniqueName);
    using (var input = await compressedFile.OpenSequentialReadAsync())
    using (var output = await decompressedFile.OpenAsync(FileAccessMode.ReadWrite))
    using (var decompressor = new Decompressor(input))
    {
        var bytesDecompressed = await RandomAccessStream.CopyAsync(decompressor, output);
        // 顯示解壓縮大小
    }
}

二、Windows 應用商店相關操作

// 開啟指定 App 的商店頁面
await Launcher.LaunchUriAsync(new Uri("ms-windows-store:PDP?PFN=" + Package.Current.Id.FamilyName));
// 開啟評論頁
await Launcher.LaunchUriAsync(new Uri("ms-windows-store:REVIEW?PFN=" + Package.Current.Id.FamilyName));
// 關鍵字搜尋
await Launcher.LaunchUriAsync(new Uri("ms-windows-store:Search?query=關鍵字"));
// 開啟更新頁
await Launcher.LaunchUriAsync(new Uri("ms-windows-store:Updates"));

三、App 與 Web 連結 (Web-to-App Linking)

在網頁中加入 Meta 標籤,IE/Edge 會顯示「切換到 App」或「取得此網站的 App」按鈕:

<meta name="msApplication-ID" content="套件名稱" />
<meta name="msApplication-PackageFamilyName" content="套件系列名稱" />
<meta name="msApplication-Arguments" content="啟動參數" />

App 端於 OnLaunched 透過 args.Arguments 取得參數。

四、核心 API 應用

  • CoreDispatcher:UI 執行緒排程器,RunAsync(CoreDispatcherPriority.Normal, () => { /* UI 更新 */ })。
  • CoreWindow:視窗事件 (Pointer、Key、SizeChanged 等) 與座標系。
  • CoreApplication:應用級別事件 (Suspending、Resuming、BackgroundActivated) 與屬性。

五、頁面與應用程式生命週期

層級事件/方法用途
應用程式OnLaunched / OnSuspending / OnResuming啟動、暫停儲存狀態、恢復
頁面OnNavigatedTo / OnNavigatedFrom / LoadState / SaveState導航參數、狀態保存/還原
視窗VisibilityChanged / SizeChanged輔助顯示、全螢幕切換

關鍵最佳實務

  1. 暫停時 (Suspending) 必須在 5 秒內完成狀態序列化,否則系統強制終止。
  2. 使用 SuspensionManager 輔助類自動處理頁面狀態儲存至 ApplicationData.Current.LocalFolder。
  3. 壓縮檔案需於清單宣告「檔案類型關聯」,否則無權存取 DocumentsLibrary 中的自訂副檔名。
  4. 商店 URI 配置 (ms-windows-store:) 僅在裝置安裝 Windows 8+ 時有效。

總結

掌握壓縮資料流、商店深層連結、Web-to-App 導流、Core API 多執行緒調度與 PLM 狀態管理,是打造專業級 Windows Store 應用的關鍵能力組合。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved