隔离存储(Isolate Store) 是一个虚拟的文件系统 . 它可用于保存本地(客户端文件)。下面是微软的一些解释:
虚拟文件系统根目录对于每个机器当前登陆用户不同的, 它是一个隐藏的文件夹,存在于物理文件系统中. 每个application的不同标识将会使其映射到不同的文件夹中, 也就是说,将分配给每个不同的application 一个属于它的虚拟文件系统. .NET Framework version 2.0中的文件夹节构和隐藏架构同样在.NET Framework for Silverlight中
也用到了.
而相应的page.xaml.cs(位于源码包中的IsolatedStore_Demo文件夹)代码如下:
上面代码中就是将subject(主题),Recipient(收件人),Body(邮件内容)以键值对的方式存储在Isolate Store
虚拟存储区中.而相应的WCF服务这里只是一种象征性的发送,并无什么实质内容,它在调用成功后会将结果以Bool型传
递回来,以便进行接下来其它操作.
虚拟文件系统根目录对于每个机器当前登陆用户不同的, 它是一个隐藏的文件夹,存在于物理文件系统中. 每个application的不同标识将会使其映射到不同的文件夹中, 也就是说,将分配给每个不同的application 一个属于它的虚拟文件系统. .NET Framework version 2.0中的文件夹节构和隐藏架构同样在.NET Framework for Silverlight中
也用到了.
而在silverlight开发中,允许我们使用Isolate Store(位于System.IO.IsolatedStorage名空间),其下有几个
主要的类如:
主要的类如:
IsolatedStorageSettings (以Dictionary<<(TKey, TValue)>> 键值对的方式来存储数据,本文DEMO的演示内容)
IsolatedStorageFile (返回一个包括路径和文件的虚拟区域, 用于管理目录,文件等, 会在下一篇中演示)
IsolatedStorageFileStream (以流的方式读写指定路径的文件, 会在下一篇中演示)
下面就先演示一下IsolatedStorageSettings的使用方法:
假设我们有一个应用,它是一个本地邮件管理和发送程序(如OutLook等).而我们写完一封EMAIL时可能不会立即将
其发送出去,而是还生成一个Draft(草稿,以便日后进行编辑),当我们认为可以发送时才会去SEND。
IsolatedStorageFileStream (以流的方式读写指定路径的文件, 会在下一篇中演示)
下面就先演示一下IsolatedStorageSettings的使用方法:
假设我们有一个应用,它是一个本地邮件管理和发送程序(如OutLook等).而我们写完一封EMAIL时可能不会立即将
其发送出去,而是还生成一个Draft(草稿,以便日后进行编辑),当我们认为可以发送时才会去SEND。
下面就是这个应用的一个原型, 如下图:
![](https://i-blog.csdnimg.cn/blog_migrate/4745153f798e1281df4de4559dc4a673.jpeg)
我们可以在输入了相关信息之后,先将其保存为草稿. 当我们下次再运行这个应用时,做自动加载当前的草稿, 以
便我们进行编辑操作.当工作完成之后,就可以将其发送到Server端. 如果认为内容不好,我们也可以从Isolate Store
区中将该项内容删除.
![](https://i-blog.csdnimg.cn/blog_migrate/4745153f798e1281df4de4559dc4a673.jpeg)
我们可以在输入了相关信息之后,先将其保存为草稿. 当我们下次再运行这个应用时,做自动加载当前的草稿, 以
便我们进行编辑操作.当工作完成之后,就可以将其发送到Server端. 如果认为内容不好,我们也可以从Isolate Store
区中将该项内容删除.
下面就是它的page.xaml代码:
<
Grid
x:Name
="LayoutRoot"
Background
="AliceBlue"
>
< Grid.RowDefinitions >
< RowDefinition Height ="40" />
< RowDefinition Height ="40" />
< RowDefinition Height ="231" />
< RowDefinition Height ="*" />
</ Grid.RowDefinitions >
< Grid.ColumnDefinitions >
< ColumnDefinition Width ="122" />
< ColumnDefinition Width ="*" />
</ Grid.ColumnDefinitions >
< TextBlock Grid.Row ="0" Grid.Column ="0" Margin ="5,5,57,5" HorizontalAlignment ="Stretch" >
< Run Text ="收件人:" />
</ TextBlock >
< TextBox x:Name ="Recipient" Width ="200" Grid.Row ="0" Grid.Column ="1" HorizontalAlignment ="Left" Margin ="5,5,0,5" />
< TextBlock Grid.Row ="1" Grid.Column ="0" Margin ="5,5,57,5" HorizontalAlignment ="Stretch" >
< Run Text ="主 题:" />
</ TextBlock >
< TextBox x:Name ="Subject" Width ="200" Grid.Row ="1" Grid.Column ="1" HorizontalAlignment ="Left" Margin ="5,5,0,5" />
< TextBlock Grid.Row ="2" Grid.Column ="0" Margin ="5,5,57,36" HorizontalAlignment ="Stretch" >
< Run Text ="内 容:" />
</ TextBlock >
< TextBox x:Name ="Body" TextWrapping ="Wrap" Grid.Row ="2" Grid.Column ="1" HorizontalAlignment ="Stretch" Margin ="5,5,8,5" Grid.RowSpan ="1" />
< StackPanel Grid.ColumnSpan ="2" Grid.Row ="3" Orientation ="Horizontal" Grid.Column ="0" HorizontalAlignment ="Stretch" VerticalAlignment ="Stretch" Margin ="8,23.5,64,8" >
< Button x:Name ="SaveToDraft" Margin ="5,5,5,5" Content =" 保存为草稿(本地) " Click ="SaveToDraft_Click" />
< Button x:Name ="SaveToServer" Margin ="5,5,5,5" Content =" 上传到服务器 " Click ="SaveToServer_Click" />
< Button x:Name ="DeleteDraft" Margin ="5,5,5,5" Content =" 删除当前草稿(本地) " Click ="DeleteDraft_Click" />
</ StackPanel >
</ Grid >
< Grid.RowDefinitions >
< RowDefinition Height ="40" />
< RowDefinition Height ="40" />
< RowDefinition Height ="231" />
< RowDefinition Height ="*" />
</ Grid.RowDefinitions >
< Grid.ColumnDefinitions >
< ColumnDefinition Width ="122" />
< ColumnDefinition Width ="*" />
</ Grid.ColumnDefinitions >
< TextBlock Grid.Row ="0" Grid.Column ="0" Margin ="5,5,57,5" HorizontalAlignment ="Stretch" >
< Run Text ="收件人:" />
</ TextBlock >
< TextBox x:Name ="Recipient" Width ="200" Grid.Row ="0" Grid.Column ="1" HorizontalAlignment ="Left" Margin ="5,5,0,5" />
< TextBlock Grid.Row ="1" Grid.Column ="0" Margin ="5,5,57,5" HorizontalAlignment ="Stretch" >
< Run Text ="主 题:" />
</ TextBlock >
< TextBox x:Name ="Subject" Width ="200" Grid.Row ="1" Grid.Column ="1" HorizontalAlignment ="Left" Margin ="5,5,0,5" />
< TextBlock Grid.Row ="2" Grid.Column ="0" Margin ="5,5,57,36" HorizontalAlignment ="Stretch" >
< Run Text ="内 容:" />
</ TextBlock >
< TextBox x:Name ="Body" TextWrapping ="Wrap" Grid.Row ="2" Grid.Column ="1" HorizontalAlignment ="Stretch" Margin ="5,5,8,5" Grid.RowSpan ="1" />
< StackPanel Grid.ColumnSpan ="2" Grid.Row ="3" Orientation ="Horizontal" Grid.Column ="0" HorizontalAlignment ="Stretch" VerticalAlignment ="Stretch" Margin ="8,23.5,64,8" >
< Button x:Name ="SaveToDraft" Margin ="5,5,5,5" Content =" 保存为草稿(本地) " Click ="SaveToDraft_Click" />
< Button x:Name ="SaveToServer" Margin ="5,5,5,5" Content =" 上传到服务器 " Click ="SaveToServer_Click" />
< Button x:Name ="DeleteDraft" Margin ="5,5,5,5" Content =" 删除当前草稿(本地) " Click ="DeleteDraft_Click" />
</ StackPanel >
</ Grid >
而相应的page.xaml.cs(位于源码包中的IsolatedStore_Demo文件夹)代码如下:
public
partial
class
Page : UserControl
{
private IsolatedStorageSettings appSettings =
IsolatedStorageSettings.ApplicationSettings; // 获取当前应用(silverlight Application) IsolatedStorageSettings 实例,范围在当前应用
// IsolatedStorageSettings appSettings =
// IsolatedStorageSettings.SiteSettings; // 获取当前应用(silverlight Application) IsolatedStorageSettings 实例,范围在当前域
public Page()
{
InitializeComponent();
this .Loaded += new RoutedEventHandler(Page_Loaded);
}
void Page_Loaded( object sender, RoutedEventArgs e)
{
try
{
// 加载相应节点信息,首次运行时会报为空的错误
Recipient.Text = ( string ) appSettings[ " Recipient " ];
Subject.Text = ( string )appSettings[ " Subject " ];
Body.Text = ( string )appSettings[ " Body " ];
}
catch (System.Collections.Generic.KeyNotFoundException ex)
{
HtmlPage.Window.Alert(ex.Message);
}
}
private void SaveToDraft_Click( object sender, RoutedEventArgs e)
{
try
{ // 添加相应节点信息
appSettings.Add( " Recipient " , Recipient.Text);
appSettings.Add( " Subject " , Subject.Text);
appSettings.Add( " Body " , Body.Text);
HtmlPage.Window.Alert( " 已存入本地隔离存储区 " );
}
catch (ArgumentException ex)
{
HtmlPage.Window.Alert(ex.Message);
}
}
private void DeleteDraft_Click( object sender, RoutedEventArgs e)
{
try
{
// 移除相当节点信息
appSettings.Remove( " Recipient " );
appSettings.Remove( " Subject " );
appSettings.Remove( " Body " );
HtmlPage.Window.Alert( " 已从本地隔离存储区中删除该草稿 " );
}
catch (ArgumentException ex)
{
HtmlPage.Window.Alert(ex.Message);
}
}
private void SaveToServer_Click( object sender, RoutedEventArgs e)
{
// 创建EMAIL信息类,以此作为上传数据
EmailInfo emailInfo = new EmailInfo()
{
Recipientk__BackingField = Recipient.Text,
Subjectk__BackingField = Subject.Text,
Bodyk__BackingField = Body.Text
};
EmailClient emailClient = new EmailClient();
// 调用WCF方法
emailClient.AcceptCompleted += new EventHandler < AcceptCompletedEventArgs > (email_AcceptCompleted);
emailClient.AcceptAsync(emailInfo);
}
void email_AcceptCompleted( object sender, AcceptCompletedEventArgs e)
{
if (e.Result)
{
HtmlPage.Window.Alert( " 上传成功 " );
}
else
{
HtmlPage.Window.Alert( " 上传失败 " );
}
}
}
{
private IsolatedStorageSettings appSettings =
IsolatedStorageSettings.ApplicationSettings; // 获取当前应用(silverlight Application) IsolatedStorageSettings 实例,范围在当前应用
// IsolatedStorageSettings appSettings =
// IsolatedStorageSettings.SiteSettings; // 获取当前应用(silverlight Application) IsolatedStorageSettings 实例,范围在当前域
public Page()
{
InitializeComponent();
this .Loaded += new RoutedEventHandler(Page_Loaded);
}
void Page_Loaded( object sender, RoutedEventArgs e)
{
try
{
// 加载相应节点信息,首次运行时会报为空的错误
Recipient.Text = ( string ) appSettings[ " Recipient " ];
Subject.Text = ( string )appSettings[ " Subject " ];
Body.Text = ( string )appSettings[ " Body " ];
}
catch (System.Collections.Generic.KeyNotFoundException ex)
{
HtmlPage.Window.Alert(ex.Message);
}
}
private void SaveToDraft_Click( object sender, RoutedEventArgs e)
{
try
{ // 添加相应节点信息
appSettings.Add( " Recipient " , Recipient.Text);
appSettings.Add( " Subject " , Subject.Text);
appSettings.Add( " Body " , Body.Text);
HtmlPage.Window.Alert( " 已存入本地隔离存储区 " );
}
catch (ArgumentException ex)
{
HtmlPage.Window.Alert(ex.Message);
}
}
private void DeleteDraft_Click( object sender, RoutedEventArgs e)
{
try
{
// 移除相当节点信息
appSettings.Remove( " Recipient " );
appSettings.Remove( " Subject " );
appSettings.Remove( " Body " );
HtmlPage.Window.Alert( " 已从本地隔离存储区中删除该草稿 " );
}
catch (ArgumentException ex)
{
HtmlPage.Window.Alert(ex.Message);
}
}
private void SaveToServer_Click( object sender, RoutedEventArgs e)
{
// 创建EMAIL信息类,以此作为上传数据
EmailInfo emailInfo = new EmailInfo()
{
Recipientk__BackingField = Recipient.Text,
Subjectk__BackingField = Subject.Text,
Bodyk__BackingField = Body.Text
};
EmailClient emailClient = new EmailClient();
// 调用WCF方法
emailClient.AcceptCompleted += new EventHandler < AcceptCompletedEventArgs > (email_AcceptCompleted);
emailClient.AcceptAsync(emailInfo);
}
void email_AcceptCompleted( object sender, AcceptCompletedEventArgs e)
{
if (e.Result)
{
HtmlPage.Window.Alert( " 上传成功 " );
}
else
{
HtmlPage.Window.Alert( " 上传失败 " );
}
}
}
上面代码中就是将subject(主题),Recipient(收件人),Body(邮件内容)以键值对的方式存储在Isolate Store
虚拟存储区中.而相应的WCF服务这里只是一种象征性的发送,并无什么实质内容,它在调用成功后会将结果以Bool型传
递回来,以便进行接下来其它操作.
好了,今天的内容就到这里了:)
本文转自 daizhenjun 51CTO博客,原文链接:http://blog.51cto.com/daizhj/82660,如需转载请自行联系原作者