|
Isolated Storage是針對(duì)各個(gè)獨(dú)立用戶分配的單獨(dú)的虛擬存儲(chǔ)空間,在Windows會(huì)存儲(chǔ)在/%AppData%/LocalLow/Microsoft/Silverlight/is, 而在Mac OS X會(huì)存儲(chǔ)在 /Users/<user>/Library/Application Support/Microsoft/Silverlight/is。
Isolated Storage有點(diǎn)像cookies,每個(gè)用戶獨(dú)立存儲(chǔ),Isolated Storage的容量是有配額的,但是可以通過調(diào)用System.IO.IsolatedStorage.IsolatedStorageFile.IncreaseQuotaTo()來增加容量。
下圖為Isolated Storage的地址。
無論瀏覽器版本的Silverlight還是Out Of Browser都可以自由使用Isolated Storage
IsolatedStorageFile theStore = IsolatedStorageFile.GetUserStoreForApplication();
FileStream fs = new System.IO.IsolatedStorage.IsolatedStorageFileStream(@"wp.db", File
Mode.OpenOrCreate, FileAccess.ReadWrite, FileShare.ReadWrite, theStore);
可以在Isolated Storage里面自由的增加,修改刪除文件和目錄。
theStore.CreateDirectory("db");
但是不能把Isolated Storage外面的文件拷貝到Isolated Storage里面。這個(gè)為開發(fā)帶來很多困難,例如我想把sqlite的數(shù)據(jù)庫文件存放到XAP里面一同發(fā)布,然后把這個(gè)數(shù)據(jù)文件存放到到Isolated Storage里面,以后可以對(duì)這數(shù)據(jù)庫繼續(xù)修改,數(shù)據(jù)還是保存在Isolated Storage里面。可是不能把XAP里面的文件拷貝到Isolated Storage里面,沒辦法使用預(yù)先定義的數(shù)據(jù)。
從我的文檔拷貝數(shù)據(jù)到Isolated Storage的時(shí)候出錯(cuò)。
在Out of Browser中使用我的文檔
如果使用了Out of Browser,程序可以中使用我的文檔(My Documents)的文件。
在項(xiàng)目屬性中選擇 "Enable running application out of browser",然后點(diǎn)擊 "Out-of-Browser Settings",然后選擇"Required elevated trust when running outside the browser"
if (App.Current.HasElevatedPermissions)
{
FileStream stream = new FileStream(Environment.GetFolderPath(Environment.Special
Folder.MyDocuments) + @"/wp.db", FileMode.OpenOrCreate);
string streamobject = new StreamReader(stream).ReadToEnd();
}
配置完畢以后就可以使用我的文檔的文件了。使用我的文檔的文件,App.Current.HasElevatedPermissions必須為true.
使用內(nèi)嵌資源文件
所謂內(nèi)嵌資源文件就是把程序需要用到的offline文件打包到XAP包里面。可以參考 Silverlight如何內(nèi)嵌資源,適用于Windows Phone
XDocument xDoc = XDocument.Load(@"db/wp.xml");
程序可以讀取xml文件。
Image image = new Image();
image.Source = new BitmapImage(new Uri("Images/" + station.Image, UriKind.Relative));
也可以使用圖片文件。
但是不能打開文件進(jìn)行操作。
SaveFileDialog
SaveFileDialog 為用戶提供了把文件保存到其他目錄的可能性,但是其具有限制性,必須由用戶操作,不能直接通過程序把文件保存到其他位置上。
SaveFileDialog textDialog;
public MainPage()
{
InitializeComponent();
textDialog = new SaveFileDialog();
textDialog.Filter = "Text Files | *.txt";
textDialog.DefaultExt = "txt";
}
private void button1_Click(object sender, RoutedEventArgs e)
{
bool? result = textDialog.ShowDialog();
if (result == true)
{
System.IO.Stream fileStream = textDialog.OpenFile();
System.IO.StreamWriter sw = new System.IO.StreamWriter(fileStream);
sw.WriteLine("Writing some text in the file.");
sw.Flush();
sw.Close();
}
}
關(guān)于Windows Phone數(shù)據(jù)庫的思考
Windows Phone不支持直接操作物理文件。沿用了傳統(tǒng)Silverlight的做法,使用了Isolated Storage的虛擬目錄空間。我想Isolated Storage對(duì)于Silverlight來說是不錯(cuò)的做法,程序不能直接操作物理文件,這樣有效防止病毒的傳播。但是Windows Phone從文件系統(tǒng)的角度看就是一臺(tái)PC,如果PC本地程序(例如Winform和WPF)都不能操作物理文件,那也太杯具了。如果這個(gè)問題一直不能解決,Windows Phone第三方數(shù)據(jù)庫永遠(yuǎn)都會(huì)有突破,因?yàn)闆]辦法把預(yù)先定義的數(shù)據(jù)讀取出來。
目前解決方法有二:
1. 等待微軟出SQL CE for Windows Phone。我們不可以做,不代表微軟不可以做,微軟可以寫原生代碼(native C++)的。理論上什么都能做出來。
2. 使用云和網(wǎng)絡(luò)存儲(chǔ),沒想到微軟現(xiàn)在走的那么前,比google還絕,什么都用云。
之前認(rèn)為微軟是重點(diǎn)關(guān)注桌面系統(tǒng),因?yàn)榇蟛糠质杖雭碓从赪indows和Office,而google重點(diǎn)關(guān)注Web,由于沒有自身的操作系統(tǒng),什么都想使用Web一統(tǒng)天下。但是從微軟發(fā)布IE9對(duì)HTML5的支持可以看到,微軟也對(duì)Web和云投入很大。但是基于Windows Phone來說,還是提供本地支持比較好,因?yàn)?a href=/yuedu/yidong/ target=_blank class=infotextkey>移動(dòng)設(shè)備網(wǎng)絡(luò)連通性沒有其他電腦設(shè)備好,離線應(yīng)用還具有很大市場(chǎng)。
NET技術(shù):關(guān)于Windows Phone數(shù)據(jù)庫和Silverlight本地文件操作,轉(zhuǎn)載需保留來源!
鄭重聲明:本文版權(quán)歸原作者所有,轉(zhuǎn)載文章僅為傳播更多信息之目的,如作者信息標(biāo)記有誤,請(qǐng)第一時(shí)間聯(lián)系我們修改或刪除,多謝。