您的位置:知识库 » .NET技术

关于Windows Phone数据库和Silverlight本地文件操作

作者: Jake Lin  来源: 博客园  发布时间: 2010-11-08 21:55  阅读: 2591 次  推荐: 1   原文链接   [收藏]  
摘要:最近在调试C# Sqlite for Windows Phone 7,了解了一下Silverlight的本地文件操作,把想法记录下来。 Isolated Storage(独立存储空间)

  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的地址。

image

  无论浏览器版本的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里面,没办法使用预先定义的数据。

image

从我的文档拷贝数据到Isolated Storage的时候出错。

在Out of Browser中使用我的文档

如果使用了Out of Browser,程序可以中使用我的文档(My Documents)的文件。

image

  在项目属性中选择 "Enable running application out of browser",然后点击 "Out-of-Browser Settings",然后选择"Required elevated trust when running outside the browser"

image

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));

  也可以使用图片文件。

image

但是不能打开文件进行操作。

SaveFileDialog

SaveFileDialog 为用户提供了把文件保存到其他目录的可能性,但是其具有限制性,必须由用户操作,不能直接通过程序把文件保存到其他位置上。

image

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来说,还是提供本地支持比较好,因为移动设备网络连通性没有其他电脑设备好,离线应用还具有很大市场。

1
0
 
标签:Silverlight wp7

.NET技术热门文章

    .NET技术最新文章

      最新新闻

        热门新闻