獨立存儲:
IsolatedStorage獨立存儲空間是儲存應用程式的一些資料已經配置檔案,獨立存儲空間相對于其他的wp程式是獨立的,也就是說每個wp程式都會有自己的獨立存儲空間,每個wp程式互相之間不能通路;
Isolated Storage又叫做隔離存儲空間,Windows Phone 7手機上用來本地存儲資料。下圖是一個存儲應用的檔案夾結構圖:
Isolated Storage用來建立與維護本地存儲。WP7上的架構和Windows下的Silverlight類似,所有的讀寫操作都隻限于隔離存儲空間并且無法直接通路磁層作業系統的檔案系統。這樣能夠防止非法的通路以及其他應用的破壞,增強安全性。
提示:如果你有兩個應用想要共用一個同一個資料,則沒法通過本地存儲實作。你可以使用web服務等。
提示:WP7下的隔離存儲空間沒有配額的限制。應用應該隻儲存必要的資料。當Windows Phone隻剩下10%的可用空間,使用者會收到一個提醒并可能停止目前應用。對使用者來講這是一個很差的使用者體驗。
在隔離存儲空間下可以進行目錄操作、檔案操作、應用程式配置資訊等。
以上内容參考自:http://blog.csdn.net/shenzhoulong001/article/details/7452194
執行個體内容:
界面ListBox上動态綁定資料,ListBoxItem為超連結HyperlinkButton,内容是檔案名,可以連接配接到其他XAML顯示出來。
Save按鈕功能是建立檔案清單。MainPanel可以在Load時加載顯示出來。
界面UI代碼:
<Grid x:Name="Root"
VerticalAlignment="Stretch"
HorizontalAlignment="Stretch"
Loaded="Root_Loaded">
<Button Name="button1"
Margin="8,300,332,392"
Content="Save"
Click="button1_Click" />
<ListBox Height="228"
HorizontalAlignment="Left"
Margin="12,417,0,0"
Name="listBox1"
VerticalAlignment="Top"
Width="460">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel HorizontalAlignment="Stretch"
VerticalAlignment="Stretch">
<HyperlinkButton HorizontalAlignment="Left"
VerticalAlignment="Top"
Width="300"
Height="72"
Content="{Binding}"
Click="mylistBoxItem_Click"
/>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</Grid>
解釋:
在ListBox上動态加載資料顯示時,必須使用以下代碼,
<ListBox.ItemTemplate>
<DataTemplate>
</DataTemplate>
</ListBox.ItemTemplate>
Content="{Binding}"表示該内容是綁定的資料。
-----------------------------------------------------------------------------
Save按鈕事件:
private void button1_Click(object sender, RoutedEventArgs e)
{
newFile("test1.txt", "這是第一個檔案!");
newFile("test2.txt", "這是第二個檔案!");
newFile("test3.txt", "這是第三個檔案!");
//添加綁定
var appStorage = IsolatedStorageFile.GetUserStoreForApplication();
string[] fileNames= appStorage.GetFileNames();
this.listBox1.ItemsSource = fileNames;
}
public void newFile(string fileName, string content) {
//建立檔案
var appStorage = IsolatedStorageFile.GetUserStoreForApplication();
try {
if (!appStorage.FileExists(fileName)) {
using ( var file=appStorage.CreateFile(fileName)){
using (var writer = new StreamWriter(file)){
writer.Write(content);
}
}
}
}catch (Exception ee){
MessageBox.Show(ee.ToString());
}
}
----------------------------------------------------------------------
HyperLinkButton事件:
private void mylistBoxItem_Click(object sender, RoutedEventArgs e)
{
HyperlinkButton myLictBoxItem = (HyperlinkButton)sender;
string uri = string.Format("/PhoneApp7;component/Page.xaml?id={0}",myLictBoxItem.Content);
NavigationService.Navigate(new Uri(uri, UriKind.Relative));
}
解釋:
NavigationService實作了Uri的定義與跳轉
----------------------------------------------------------------------
新界面加載資料:
private void PhoneApplicationPage_Loaded(object sender, RoutedEventArgs e)
{
string fileName = this.NavigationContext.QueryString["id"];
//讀出檔案的内容
IsolatedStorageFile appStorage = IsolatedStorageFile.GetUserStoreForApplication();
if (appStorage.FileExists(fileName)) {
using (var file = appStorage.OpenFile(fileName, System.IO.FileMode.Open, System.IO.FileAccess.ReadWrite)){
using (var reader = new StreamReader(file)){
textBlock1.Text = fileName;
textBlock2.Text = reader.ReadToEnd();
}
}
}
}