關于Windows Phone數據庫和Silverlight本地文件操作
Isolated Storage是針對各個獨立用戶分配的單獨的虛擬存儲空間,在Windows會存儲在\%AppData%\LocalLow\Microsoft\Silverlight\is, 而在Mac OS X會存儲在 /Users/<user>/Library/Application Support/Microsoft/Silverlight/is。
Isolated Storage有點像cookies,每個用戶獨立存儲,Isolated Storage的容量是有配額的,但是可以通過調用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里面。這個為開發帶來很多困難,例如我想把sqlite的數據庫文件存放到XAP里面一同發布,然后把這個數據文件存放到到Isolated Storage里面,以后可以對這數據庫繼續修改,數據還是保存在Isolated Storage里面。可是不能把XAP里面的文件拷貝到Isolated Storage里面,沒辦法使用預先定義的數據。
從我的文檔拷貝數據到Isolated Storage的時候出錯。
在Out of Browser中使用我的文檔
如果使用了Out of Browser,程序可以中使用我的文檔(My Documents)的文件。
在項目屬性中選擇 "Enable running application out of browser",然后點擊 "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.
使用內嵌資源文件
所謂內嵌資源文件就是把程序需要用到的offline文件打包到XAP包里面。可以參考 Silverlight如何內嵌資源,適用于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));
也可以使用圖片文件。
但是不能打開文件進行操作。
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(); } }
關于Windows Phone數據庫的思考
Windows Phone不支持直接操作物理文件。沿用了傳統Silverlight的做法,使用了Isolated Storage的虛擬目錄空間。我想Isolated Storage對于Silverlight來說是不錯的做法,程序不能直接操作物理文件,這樣有效防止病毒的傳播。但是Windows Phone從文件系統的角度看就是一臺PC,如果PC本地程序(例如Winform和WPF)都不能操作物理文件,那也太杯具了。如果這個問題一直不能解決,Windows Phone第三方數據庫永遠都會有突破,因為沒辦法把預先定義的數據讀取出來。
目前解決方法有二:
1. 等待微軟出SQL CE for Windows Phone。我們不可以做,不代表微軟不可以做,微軟可以寫原生代碼(native C++)的。理論上什么都能做出來。
2. 使用云和網絡存儲,沒想到微軟現在走的那么前,比google還絕,什么都用云。
之前認為微軟是重點關注桌面系統,因為大部分收入來源于Windows和Office,而google重點關注Web,由于沒有自身的操作系統,什么都想使用Web一統天下。但是從微軟發布IE9對HTML5的支持可以看到,微軟也對Web和云投入很大。但是基于Windows Phone來說,還是提供本地支持比較好,因為移動設備網絡連通性沒有其他電腦設備好,離線應用還具有很大市場。