- Silverlight 教程
- Silverlight - 首頁
- Silverlight - 概述
- Silverlight - 環境設定
- Silverlight - 入門
- Silverlight - XAML 概述
- Silverlight - 專案型別
- Silverlight - 固定佈局
- Silverlight - 動態佈局
- 受約束與不受約束
- Silverlight - CSS
- Silverlight - 控制元件
- Silverlight - 按鈕
- Silverlight - 內容模型
- Silverlight - ListBox
- Silverlight - 模板
- Silverlight - 可視狀態
- Silverlight - 資料繫結
- Silverlight - 瀏覽器整合
- Silverlight - 瀏覽器外執行
- Silverlight - 應用程式和資源
- Silverlight - 檔案訪問
- Silverlight - 檢視模型
- Silverlight - 輸入處理
- Silverlight - 隔離儲存
- Silverlight - 文字
- Silverlight - 動畫
- Silverlight - 影片和音訊
- Silverlight - 列印
- Silverlight 有用資源
- Silverlight - 快速指南
- Silverlight - 有用資源
- Silverlight - 討論
Silverlight - 隔離儲存
第三種檔案訪問機制是隔離儲存機制,它提供與登入使用者關聯的儲存。API 透過.NET System.IO名稱空間中的Stream類呈現資料。因此,與我們迄今為止看到的其他機制一樣,您可以使用System.IO中的其他型別來處理流,從而可以儲存文字資料或二進位制資料。
一些重要的特性包括:
這種儲存機制被稱為隔離儲存,因為儲存是分割槽的,Silverlight應用程式只能訪問某些部分。
您無法訪問任何舊的儲存資料。首先,儲存是按使用者分割槽的。Silverlight應用程式無法訪問與登入和執行應用程式的使用者不同的使用者的儲存。
這與您的 Web 應用程式可能使用的任何身份驗證機制無關。這是一點需要注意的重要事項,因為一些共享計算機的人不會使用單獨的 Windows 帳戶,他們習慣於只是登入和登出他們使用的網站。
使用隔離儲存
隔離儲存並非 Silverlight 獨有。該 API 最初是為Windows 窗體引入的,以便在部分信任場景中從 Web 啟動的應用程式能夠在本地儲存資料。實現方式不同,無法從 Silverlight 訪問完整的.NET Framework 的隔離儲存,反之亦然。
但是,如果您使用過它,這裡的步驟看起來會非常熟悉。
您首先請求使用者特定的儲存。在這種情況下,我們正在請求應用程式的儲存。如果我們想要網站上所有 XAP 共享的每個站點儲存,我們將呼叫GetUserStoreForSite。
兩種方法都會返回一個IsolatedStorageFile物件,這是一個不太有用的名稱,因為它表示的是一個目錄,而不是一個檔案。
要訪問檔案,您需要向IsolatedStorageFile請求一個Stream。
我們使用IsolatedStorageFileStream類,其建構函式要求您傳遞IsolatedStorageFile物件作為引數。
因此,我們正在儲存中建立一個新檔案。檔案在磁碟上的確切位置未知。
包含目錄包含隨機元素,以便無法猜測檔名。
如果沒有這個,惡意網站可能會將檔案放在使用者的計算機上,然後構建檔案 URL 來開啟它,希望能誘使使用者點選本地執行程式的連結。
Windows 中內建了各種其他安全措施來防止這種情況發生,但這是另一層防禦措施,以防其他措施已被停用或繞過。
該檔案將儲存在使用者的配置檔案中的某個位置,但您只能知道這麼多。您的IsolatedStorageFileStream不會報告其真實位置。
讓我們來看一個簡單的示例,該示例跟蹤應用程式執行的次數。以下是 XAML 程式碼。
<UserControl x:Class = "StoreRunCount.MainPage"
xmlns = "http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x = "http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d = "http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc = "http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable = "d"
d:DesignHeight = "300" d:DesignWidth = "400">
<Grid x:Name = "LayoutRoot" Background = "White">
<TextBlock x:Name = "runCountText" FontSize = "20" />
</Grid>
</UserControl>
以下是使用隔離儲存的 C# 程式碼。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.IO.IsolatedStorage;
using System.IO;
namespace StoreRunCount {
public partial class MainPage : UserControl {
const string RunCountFileName = "RunCount.bin";
public MainPage() {
InitializeComponent();
int runCount = 0;
using (var store = IsolatedStorageFile.GetUserStoreForApplication()) {
if (store.FileExists(RunCountFileName)) {
using (var stm = store.OpenFile(RunCountFileName,
FileMode.Open, FileAccess.Read))
using (var r = new BinaryReader(stm)) {
runCount = r.ReadInt32();
}
}
runCount += 1;
using (var stm = store.OpenFile(RunCountFileName,
FileMode.Create, FileAccess.Write))
using (var w = new BinaryWriter(stm)) {
w.Write(runCount);
}
}
runCountText.Text = "You have run this application " + runCount.ToString() + " time(s)";
}
}
}
編譯並執行上述程式碼後,您將看到以下網頁,該網頁將向您顯示您執行此應用程式的次數。
增加配額
如果初始數量不足,應用程式可能會請求更多空間。不能保證請求會成功。Silverlight 將詢問使用者是否同意為應用程式提供更多空間。
順便說一句,您只允許響應使用者輸入(例如單擊)請求更多儲存空間。如果您嘗試在其他時間請求它,例如外掛載入時或在計時器處理程式中,Silverlight 將自動失敗請求,甚至不會提示使用者。額外的配額僅適用於使用者正在互動的應用程式。
IsolatedStorageFile物件提供三個用於管理配額的成員:
- AvailableFreeSpace (可用空間)
- IncreaseQuotaTo (增加配額到)
- Quota (配額)
AvailableFreeSpace (可用空間)
AvailableFreeSpace 屬性告訴您還有多少配額可用。
請注意,即使是空的子目錄也會消耗一部分配額,因為作業系統需要分配磁碟空間來表示該目錄。因此,可用空間可能小於總配額減去所有檔案的大小之和。
IncreaseQuotaTo (增加配額到)
如果您沒有足夠的可用空間繼續操作,則可以透過呼叫IncreaseQuotaTo方法來請求更多空間。
Quota (配額)
在這裡,我們使用第三個屬性Quota來發現當前的配額大小,然後我們新增所需的額外數量以獲得我們新的請求配額。
該方法返回True或False以指示我們是否已分配我們請求的內容。請注意,Silverlight 可能會決定分配比您請求的更多空間。
這是一個簡單的示例,用於在單擊按鈕時增加配額。以下是 XAML 程式碼。
<UserControl x:Class = "ChangeQuota.MainPage"
xmlns = "http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x = "http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d = "http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc = "http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable = "d"
d:DesignHeight = "300" d:DesignWidth = "400">
<Grid x:Name = "LayoutRoot" Background = "White">
<TextBlock x:Name = "infoText" FontSize = "20" TextWrapping = "Wrap" />
<Button x:Name = "increaseQuota" Content = "Increase" HorizontalAlignment = "Center"
FontSize = "20"
VerticalAlignment = "Center" Click = "increaseQuota_Click" />
</Grid>
</UserControl>
以下是增加配額的單擊事件的實現。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.IO.IsolatedStorage;
namespace ChangeQuota {
public partial class MainPage : UserControl {
public MainPage() {
InitializeComponent();
}
private void increaseQuota_Click(object sender, RoutedEventArgs e) {
using (IsolatedStorageFile isoStore =
IsolatedStorageFile.GetUserStoreForApplication()) {
long newQuota = isoStore.Quota + 10240;
if (isoStore.IncreaseQuotaTo(newQuota)) {
infoText.Text = "Quota is " + isoStore.Quota + ", free space: " +
isoStore.AvailableFreeSpace;
} else {
infoText.Text = "Meanie!";
}
}
}
}
}
編譯並執行上述程式碼後,您將看到以下輸出。
單擊增加後,將出現提示。它要求將配額增加到比現有配額大 10KB。
單擊是後,它將打印出可用的配額數量。
我們建議您執行上述示例以更好地理解。