您的位置:首页 > 其它

WP7 开发(九) WP7控件开发(六)-DeepZoom技术

2012-08-07 10:51 316 查看
2011年11月14日

-DeepZoom:源于遥感影像的金字塔显示方式,提供了与高分辨率图像进行交互的能力,可以快速缩放图像而不影响应用的性能,加载或平移图像时可以光滑过度

-应用:高分辨率、极大图像的浏览,3D合成图像,广告宣传

-分类:Single和Collection

-DeepZoom Composer制作工具下载地址:http://www.microsoft.com/downloads/en/details.aspx?FamilyID=457B17B7-52BF-4BDA-

87A3-FA8A4673F8BF

-MulitScaleImage:打开、放大和平移多分辨率图像,快速任意缩放展示大图形、高分辨率图像的应用程序 -属性:Source,UseSprings-springs animations,ViewportOrigin-CenterPoint,ViewportWidth-ZoomLevel

posted @ 2011-11-14 14:48 zhangze 阅读(127) 评论(0) 编辑

WP7 开发(八) WP7控件开发(五)-Silverlight Tookit控件集
工具(控件集): 1.Contextmenu;2.ToggleSwitch;3.WrapPanel;4.DataPicker; 5.TimePicker;6.AutoCompleteBox;7.NavigationTransition; 8.ListPicker;9.LongListSelector

1.Contextmenu:长按弹出菜单;不能在容器控件中使用 <toolkit:ContextMenuService.ContextMenu>(需额外添加代码) <toolkit:ContentMenu Height="400" Margin="123,168,0,0" HorizontalAlignment="Left" VerticalAlignment="Top" Width="200" BorderBrush="Red" BorderThickness="5"> <toolkit:MenuItem Header="Test1" Click="MenuItem_Click"/> <toolkit:MenuItem Header="Test2" Click="MenuItem_Click"/> <toolkit:Separator/>//分隔符控件 <toolkit:MenuItem Header="Test3" Click="MenuItem_Click"/> </toolkit:ContentMenu> </toolkit:ContextMenuService.ContextMenu>

---菜单增加图片--- <toolkit:ContextMenuService.ContextMenu> <toolkit:ContextMenu Height="400" Margin="123,168,0,0" HorizontalAlignment="Left" Name="contextMenu1" VerticalAlignment="Top" Width="200" BorderBrush="Red" BorderThickness="5"> <toolkit:MenuItem Header="Test1" Click="MenuItem_Click"/> <toolkit:MenuItem Header="Test2" Click="MenuItem_Click"/> <toolkit:Separator/> <toolkit:MenuItem Click="MenuItem_Click"> <toolkit:MenuItem.Header> <StackPanel Orientation="Horizontal"> <Image Width="50" Height="50" Source="ApplicationIcon.png"/> <TextBlock Text="Test3"/> </StackPanel> </toolkit:MenuItem.Header> </toolkit:MenuItem> </toolkit:ContextMenu> </toolkit:ContextMenuSeervice.ContextMenu>

2.ToggleSwitch:有两种状态Checked和UnChecked <toolkit:ToggleSwitch Header="ToggleSwitch" Content="是" HorizontalAlignment="Left" Margin="74,106,0,0" Name="toggleSwitch1" Height="116"VeiticalAlignment="Top" Width="271" Checked="toggleSwitch1_Checked" UnChecked="toggleSwitch1_UnChecked"/> <toolkit:ToggleSwitch Header="ToggleSwitch" Height="116" HorizontalAlignment="Left" Margin="74,106,0,0" Name="toggleSwitch2" VerticalAllignment="Top" Width="271" Checked="toggleSwitch2_Checked" UnChecked="toggleSwitch2_UnChecked"/>

(对于中文文字需代码改写)在代码中: 在Checked事件中:toggleSwitch2.Content="是"; 在UnChecked事件中:toggleSwitch2.Content="否";

3.WrapPanel:包含在控件里的元素从左到右或从上到下一次安排位置,并会自动换行(子控件自动排列,如:颜色、日历) <controlsToolkit:WrapPanel x:Name="wp1" Width="35" Margin="15,0,15,201" Height="300" VerticalAlignment="Bottom" Orientation="Horizontal"/>

在页面加载时,代码: for(int i=0;i<30;i++) { wp1.Children.Add(new Rectangle({Height=50,Width=50, Fill=new SolidColorBrush(Color.Yellow)}); wp1.Children.Add(new Rectangle({Height=50,Width=50, Fill=new SolidColorBrush(Color.Purple)}); wp1.Children.Add(new Rectangle({Height=50,Width=50, Fill=new SolidColorBrush(Color.Red)}); wp1.Children.Add(new Rectangle({Height=50,Width=50, Fill=new SolidColorBrush(Color.Black)}); wp1.Children.Add(new Rectangle({Height=50,Width=50, Fill=new SolidColorBrush(Color.Brown)}); wp1.Children.Add(new Rectangle({Height=50,Width=50, Fill=new SolidColorBrush(Color.Cyan)}); wp1.Children.Add(new Rectangle({Height=50,Width=50, Fill=new SolidColorBrush(Color.Green)}); wp1.Children.Add(new Rectangle({Height=50,Width=50, Fill=new SolidColorBrush(Color.Gray)}); }

4.DataPicker:日期控件 <toolkit:DataPicker Margin="41,121,75,0" Height="77" Name="dataPiaker1" VerticalAlignment="Top" Foreground="White" HorizontalContentAlignment="Center" ValueCahnge="dataPicker1_ValueChanged"> <toolkit:DataPicker.Backgroung> <ImageBrush ImageSource="Image/hkd16.jpg"/> </toolkit:DataPicker.Background> </toolkit:DataPicker>

属性:ValueStringFormat:设置显示时间格式 Description:Short data format(DataPicker default) XAML:"{}{0:d}" Sample:9/20/2010 Description:Short data format(TimePicker default) XAML:"{}{0:t}" Sample:4:00 PM Description:Long data format XAML:"{}{0:D}" Sample:Monday,September 20,2010 Description:Long time format XAML:"{}{0:T}" Sample:4:00:00 PM Description:Explicit month/day/year fromat XAML:"{}{0:MM-dd-yyyy}" Sample:09-20-2010 Description:Explicit 24-hour time fromat with text XAML:"{}The 24-hour time is {0:HH:mm}." Sample:The 24-hour time is 16:00

对于日期下的两个按钮设置图片: (1).需先添加一个文件夹,命名为:Toolkit.Content (2).加载图片:对应的系统目录(注意对图片的属性的修改) (3).Content/Copy if New

5.TimePicker <toolkit:TimePicker HorizontalAlignment="Left" Margin="92,275,0,0" Name="timePicker1" Width="302" VerticalAlignment="Top" ValueStringFormat="{}12小时{0:d}" ValueCahnged="timePicker1_ValueChanged"/>

(工程中新建文件夹:Toolkit.Content,将两个ApplicationBarIconButton的图片放进去,并加入到工程中)

在对应的事件的代码:(年月日和时间) this.PageTitle.Text=e.NewDataTime.ToString();

6.AutoCompleteBox:输入文字时,可以列出联想词 (不过需后台,加入词库!) <phone:PhoneApplicationPage.Resources>//字库以资源方式载入 <data:LoremIpsum x:Key="words"/> //自定义资源 </phone:PhoneApplicationPage.Resources> <toolkit:AutoCompeleteBox HorizontalAlignment="Left" Margin="82,32,0,0" Name="autoCompleteBox1" VerticalAlignment="Top" Width="291" Height="72" ItemSource="{StaticResourcewords}"/> 属性: 1.FilterMode:匹配方式 2.IsDropDownOpen:下拉列表是否打开 3.IsTextCompletionEnable:自动完成匹配填写 4.MaxDropDownHeight:下拉列表的最大高度 5.MinimumPopulateDelay:最小匹配时间 6.MinimumPrefixLength:最短匹配长度 事件: DropDownClosed/DropDownCloseing DropDownOpened/DropDownOpening

在项目中,加一个文本,写入你的词库

7.NavigationTransition:实现页面的切换效果 将App.xaml.cs中InitializePhoneAppliction()函数里的RootFrame进行修改:RootFrame=new TransitionFrame();(注意) 五种动画效果: -RollTransition -RotateTransition -SlideTransition -SwivelTransition -TurnstileTransition

//进入页面动画效果 <toolkit:TransitionService.NavigationInTransition> <toolkit:NavigationInTransition> <toolkit:NavigationInTransition.Backward> //这是旋转动画效果 <toolkit:RotateTransition Mode="In180Clockwise"/> </toolkit:NavigationInTransition.Backward> <toolkit:NavigationInTransition.Forward> <toolkit:RotateTransition Mode="In180Clockwise"/> <toolkit:NavigationInTransition.Forward> </toolkit:NavigationInTransition> </toolkit:TransitionService.NavigationInTransition> //退出页面动画效果 <toolkit:TransitionService.NavigationOutTransition> <toolkit:NavigationOutTransition> <toolkit:NavigationOutTransition.Backward> <toolkit:RotateTransition Mode="Out180Clockwise"/> </toolkit:NavigationOutTransition.Backward> <toolkit:NavigationOutTransition.Forward> <toolkit:RotateTransition Mode="Out180Clockwise"/> </toolkit:NavigationOutTransition.Forward> </toolkit:NavigationOutTransition> </toolkit:TransitionService.NavigationOutTransition>

8.ListPicker:简单列表框 属性:Full Mode 完整页面的列表框 属性:ListPickerMode的使用 (对于本题的字符串需要一个字符串的命名空间: xmlns:sys="clr-namespace:System;assembly=mscorlib") <toolkit:ListPicker Height="220" Name="listPicker1" HorizontalAlignment="Left" Margin="12,381,0,0" Width="438" VerticalAlignment="Top" Header="Test1" SelectionChanged="listPicker1_SelectionChanged"> <sys:String>Red</sys:String> <sys:String>Green</sys:String> <sys:String>Blue</sys:String> </toolkit:ListPicker>

<toolkit:ListPicker ItemsSource="{Binding}" Height="88" Name="listPicker2" HorizontalAlignment="Left" Margin="6,513,0,0" VerticalAlignment="Top" Width="444" Header="Test2" FellModeHeader="Colors"> <toolkit:ListPicker.ItemTemplate> <DataTemplate> <StackPanel Orientation="Horizontal"> <Rectangle Fill="{Binding}" Width="24" Height="24"/> <TextBlock Text="{Binding}" Margin="12 0 0 0"/> </StackPanle> </DataTemplate> </toolkit:ListPicker.ItemTemplate> <toolkit:ListPicker.FullModeItemTemplate> <DataTemplate> <StackPanel Orientation="Horizontal"> <Rectangle Fill="{Binding}" Width="24" Height="24"/> <TextBlock Text="{Binding}" Margin="12 0 0 0"/> </StackPanle> </DataTemplate> </toolkit:ListPicker.FullModeItemTemplate> </toolkit:ListPicker>

对于数据绑定:后台代码; static readonly string[] AccentColors={"purple","blue","park","teal","red","dark"}; 在构造函数中:this.DataContent=AccentColors;

9.LongListSelector:是一个归类列表;把数据进行绑定和分类 <toolkit:LongListSelector HorizontalAlignment="Left" Margin="12,162,0,0" Name="longListSelector1" Height="213" VerticalAlignment="Top" Width="438" ItemsSource="{StaticResource people}">

//设置以什么样的方式显示分类列表,如果没有这个设置,分类列表以直列显示 <toolkit:LongListSelector.GroupItemsPanel> //分类列表内的item显示内容和方式 <toolkit:LongListSelector.GroupItemTemplate> //设置列表分类的头的显示内容和方式 <toolkit:LongListSelector.GroupHeaderTemplate> //设置列表里的内容的显示方式 <toolkit:LongListSelector.ItemTemplate>

UserControl:UserControl类;同样基于UIElement 对于便于打开和关闭使用Popup控件,放在最外面 <Popup x:Name="WaitingWnd" IsOpen="Falsh"> <Grid x:Name="LayoutRoot" Background="Transparent"> <TextBlock Height="44" HorizontalAlignment="Left" Margin="0,144,0,0" Name="textBlock1" Text="请等待。。。" VerticalAlignment="Top" Foreground="Gray" Width="200" FontSize="32" TextAlignment="Center"/> <Image Height="150" HorizontalAlignment="Left" Margin="22,6,0,0" Name="image1" Stretch="Fill" Width="150" VerticalAlignment="Top"/> </Grid> </Popup>

后台代码:声明变量 DispatcherTimer Timer=null;//定时器 int Count=1; public double Speed{get;set;}

在load事件中: Timer=new DispatcherTimer(); Timer.Interval=TimSpan.FranFillireconds(Speed); Timer.Tick=new EventHndler(Timer_Tick);

在Timer_Tick事件中://动态加载 this.image1.Source=new BitmapImage(new Uri("Arrets/Loading00"+Count+".png",UriKind.Relative)); Count=(Count==0?1:Count+1);

public void WaitingBegin() { Timer.Start(); WaitingWnd.IsOpen=true; } public void WaitingEnd() { Timer.Stop(); WaitingWnd.IsOpen=false; }

写好后,就可使用,直接放到窗体上 在页面加载时; 调用WaitingBegin()方法

posted @ 2011-11-14 14:47 zhangze 阅读(676) 评论(0) 编辑

WP7 开发(七) WP7控件开发(四)-Panorama全景视图,Pivot枢轴视图
-Panorama视图:使用一个超过屏幕宽度的长水平画布,提供独特显示控件,数据和服务的方式,是一种选项、导航和数据的多屏幕 滚动菜单

-结构包括:Background,Title,Items,Background图片高度要为800像素,宽度大于480像素,小于2000像素

-Pivot视图:快捷管理应用中的视图或页面,过滤数据,视图分类

-结构包括:Title,Header,PivotItem

-使用注意:Header文字不要显示过长,以免其他PivotItem中的Header无法显示,在PivotItem中,不要使用ScrollViewer 或ListBox这类带有手势的控件

posted @ 2011-11-14 14:47 zhangze 阅读(210) 评论(0) 编辑

WP7 开发(六) WP7控件开发(三)-绘图控件 地图控件
-InkPresenter:产生手写效果

示例代码:

Stroke newStroke;

private void inkPresenter1_LostMouseCapture(object sender, MouseEventArgs e)

{

newStroke = null;//失去焦点时销毁上一次的Stroke

}

private void inkPresenter1_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)

{

inkPresenter1.CaptureMouse();//开始捕获鼠标移动路径

StylusPointCollection spc = new StylusPointCollection();

spc.Add(e.StylusDevice.GetStylusPoints(inkPresenter1));

newStroke = new Stroke();

this.inkPresenter1.Strokes.Add(newStroke);

}

private void inkPresenter1_MouseMove(object sender, MouseEventArgs e)

{

if (newStroke != null)

{

//记录鼠标在inkPresenter1上的移动的点

newStroke.StylusPoints.Add(e.StylusDevice.GetStylusPoints(inkPresenter1));

}

}

-Path:通过Markup Syntax来绘制一系列的线条,通过Geometries来绘制形状

示例:

<Path Height="428" HorizontalAlignment="Left"

Margin="12,127,0,0" Name="path1" Stroke="Red" StrokeThickness="10" VerticalAlignment="Top" Width="456" Fill="Green">

<Path.Data>

<EllipseGeometry Center="200,200" RadiusX="100" RadiusY="30"/>

</Path.Data>

</Path>

-Ellipse:绘制圆形或椭圆形

-Rectangle:绘制矩形

-Line:绘制两点间的连线

-Polygon:绘制封闭多边形

-Polyline:绘制封闭、开发多边形

-Glyphs:绘制字母、符号、字符等,主要用来显示Unicode字符,需要加载字体库从网上下载字体库,对于 使用不多的情况使用,否则会消耗系统的资源

-Map控件:在使用时,首先要申请授权验证密钥 显示模式设置有Road、Arial

-显示缩放按钮:ZoomBarVisibility属性

-显示比例尺:ScaleVisibility属性

-加标记:用到Pushpin类

//为地图加标记

Pushpin pin = new Pushpin();

pin.Location = new GeoCoordinate(30.3441333,120.342155132);

pin.Width = 200;

pin.Height = 200;

pin.Content = "济南";

pin.Background = new SolidColorBrush(Colors.Red);

mymap.Children.Add(pin);

-绘制多变型区域:用到MapPolygon类 实例化 设置Locations属性即可

//在地图上绘制多边形

MapPolygon polygon = new MapPolygon();

polygon.Fill = new SolidColorBrush(Colors.Red);

polygon.Stroke = new SolidColorBrush(Colors.Yellow);

polygon.StrokeThickness = 5;

polygon.Opacity = 0.7;

polygon.Locations = new LocationCollection()

{

new GeoCoordinate(30,120),

new GeoCoordinate(30,130),

new GeoCoordinate(30,160),

new GeoCoordinate(30,140)

};

mymap.Children.Add(polygon);

-绘制多边线:用到MapPolyline 实例化 设置Stroke属性和Locatatoins

//在地图上绘制多边线

MapPolyline polyline = new MapPolyline();

polyline.Stroke = new SolidColorBrush(Colors.Red);

polyline.StrokeThickness = 5;

polyline.Opacity = 0.5;

polyline.Locations = new LocationCollection()

{

new GeoCoordinate(30.3424242,120.3432444),

new GeoCoordinate(30.3424242,120.3432444)

};

mymap.Children.Add(polyline);

-在地图上添加图片

//在地图上添加图片

Image image = new Image();

image.Width = 100;

image.Height = 100;

image.Source = new BitmapImage(new Uri("Images/Pic1.jpg",UriKind.Relative));

MapLayer imagelayer = new MapLayer();

imagelayer.AddChild(image,new GeoCoordinate(30,120),PositionOrigin.BottomLeft);

mymap.Children.Add(imagelayer);

posted @ 2011-11-14 14:46 zhangze 阅读(215) 评论(0) 编辑

WP7 开发(五) wp7控件开发(二)
 1、PasswordBox:用于输入自定义遮罩字符的密码框控件,属性PasswordChar用来显示输入的密码替换符号

2、TextBox:可以让软键盘产生不同的键面效果;InputScope属性可通过代码获取属性列表

typeof(InputScopeNameValue).GetFields(BindingFlags.Public|BindingFlags.Static)

3、Image控件:只能加载png和jpg的图片

4、MediaElement:播放本地或网络上的多媒体;支持多媒体格式:WAV、MP3,支持mms、rtspt、rtsp流媒体协议;-属性IsMuted是否静音Stretch设置视频画面在控件中的填充方式AutoPlay是否自动播放;-事件MediaOpened MediaEnded MediaFailed 注意:其对视频的格式和帧大小有相应的要求

5、TextBlack:在页面上显示文字

6、CheckBox:多选控件;不能通过Width和Height来改变大小;可以通过其属性RenderTransform的ScaleTransform(ScaleX、ScaleY)变形改变大小

7、RadioButton:单选按钮;其改变大小的方式同CheckBox

8、ProgressBar:两种类型:显示确切进度和不断重复的动画

9、Slider:可以设置水平、垂直方向的滑竿

10、Thumb:通过拖动,获取连续的坐标,三个基本事件DragDelta(鼠标移动时)、DragStart(开始拖动)、DragComplete(拖动完成)

11、ListBox:相当一个容器,可以通过ListItem来组合多个控件而得到不同功能的List

posted @ 2011-11-14 14:45 zhangze 阅读(135) 评论(0) 编辑

WP7 开发(四) WP7控件开发(一)
一、UIElement控件通用属性

-Height/Width:用户设置的控件大小,是预期的大小

-ActualHeight/ActualWidth:获取控件的实际大小

-MaxHeight/MaxWidth & MinHeight/MinWidth :设置控件大小的三个属性()如果值发生冲突,首先要保证的是Min然后是Max,但是这两个值一定要在Height/Width设置值之间才有效

-Cursor:设置/获取控件光标形状

-DataContext:设置或获取控件数据内容

-HorizontalAlignment/VerticalAlignment:设置/获取控件水平/垂直方向的对其方式

-Language:设置/获取localization/globalization语言信息

-Margin:设置/获取控件与页面的边距

-Name:设置/获取控件的名称

-Parent:设置获取控件的父容器

-Resource:设置/获取控件资源字典,使资源像样式那样通过引用资源窜名,在Xaml中使用

-Style:设置/或获取控件的外观样式,也可以先定义好绑定到多个控件上

-Tag:为控件添加标签说明

-CacheMOde:设置/获取一个值,该值指示应在可能高速缓存已呈现的内容

-Clip:设置/获取控件剪裁效果

-DesireSize:获取系统布局大小,对于布局的调整很有用

-Opacity:设置/获取控件的透明度

-OpacityMask:设置/获取一个控件蒙版,来产生蒙版透明效果

-UseLayoutRounding:设置/获取控件及子控件是否按子像素进行布局,使控件外观圆滑清晰

-Visiblity:设置/获取控件是否可见

-Background:设置/获取控件的背景效果

-Projection:设置/获取控件的3-D透视效果

-Foreground:设置/获取字体颜色

-RenderTransformOrigin:设置/获取变形的起始点

-RenderTransform:设置/获取变形效果

-Template:设置或获取控件模板

-IsHitTestVisible:获取或设置控件是否接受焦点事件

-HorizontalContentsAlignment/VerticalContentsAligment:获取或设置控件内容的对齐方式

二、容器控件(Grid,Canvas,StackPanel,Border,PopUp,ScrollView)

1、Grid的使用

2、Canvas的使用:用来精确定位容器内的控件坐标,以及层次关系

3、StackPanel使用:设置控件的排列顺序

4、Border使用:可以容纳其它控件

5、PopUp:自身可以被打开和关闭,通常用来制作自定义的消息弹出框和输入框

7、ScrollView:拖动显示其包含的子控件的内容

三、按钮控件的使用(Button,HyperlinkButton,RepeatButton,ToggleButton)

1、HyperlinkButton:用于连接本地或Web地址,当链接Web地址时,一定要在在标签里加上TargetName=“_Self”的属性值

2、RepeatButton:按下后不断触发Click事件

3、ToggleButton:开关按钮,三种状态Checked、Unchecked、Indeterminate

posted @ 2011-11-14 14:40 zhangze 阅读(368) 评论(0) 编辑

WP7 开发(三) Isolate Storage(保存数据)

windows phone 7 目前版本上已确定没有文件系统(也就是说filestream、OpenFileDialog这样的功能都是不能使用了)和数据库系统了,Windows Phone 7 支持访问数据几种方式为: XML、Isolated Storage[独立存储]、Cloud[云存储],Windows Phone 7 上没有本地数据库API可以利用 。

Isolated Storage[独立存储]有两种方式在本地存储你的数据。第一是通过库中的键/值对,叫做IsolatedStorageSettings。第二是通过创建真实的文件和目录,叫做IsolatedStorageFile。

其实使用Isolate Storage的最大好处就是安全性了,因为只有本程序可以访问该区域,而其他程序是无法访问的。这样也就可以对一此敏感数据的保存不用自已再加密了。但是这个区域是有限的(默认为2GB),不能够保存很大的数据,以及长期保存数据。如果您非要保存大量数据,以及长期保存的话,目前只能保存在云端而不是本地。

  相关的API详细信息可以查看

http://msdn.microsoft.com/en-us/library/system.io.isolatedstorage(VS.95).aspx

(1)IsolatedStorageSettings

  IsolatedStorageSettings允许你在一个字典中存储键/值对(注意,无需任何设定),然后再读取出来。这些数据会一直保存着,无论应用程序停止/启动,或者关机等等。除非你删除它,或者用户卸载你的应用程序,否则它一直存在。要记住的一点是在它被添加到字典中之前你无法读取它。

  using System.IO.IsolatedStorage;

  IsolatedStorageSettings settings = IsolatedStorageSettings.ApplicationSettings; //使用 ApplicationSettings 属性可创建用于在独立存储中存储键/值对的字典的新实例。 //ApplicationSettings 特定于某个用户和某个应用程序。应用程序范围由应用程序的完整路径决定。 private void InitializeSettings() { if (settings.Contains("emailFlag")) { EmailFlag.IsChecked = (bool)settings["emailFlag"]; } else settings.Add("emailFlag", false); }

private void EmailFlag_Unchecked(object sender, RoutedEventArgs e) { settings["emailFlag"] = false; }

private void EmailFlag_Checked(object sender, RoutedEventArgs e) { settings["emailFlag"] = true; }

(2)IsolatedStorageFile

  IsolatedStorageFile表示包含文件和目录的独立存储区。使用IsolatedStorageFile是一种让你可以在用户的设备中存储真实文件的机制。

该类使独立存储的虚拟文件系统抽象化。IsolatedStorageFile 对象对应于特定的独立存储范围,在该范围中存在由 IsolatedStorageFileStream 对象表示的文件。应用程序可以使用独立存储将数据保存在文件系统中这些数据自己的独立部分,而不必在文件系统中指定特定的路径。

虚拟文件系统的根位于物理文件系统上经过模糊处理的每用户文件夹中。由主机提供的每个唯一标识符都映射为不同的根,该根为每个应用程序提供它自己的虚拟文件系统。应用程序不能从它自己的文件系统导航到另一个文件系统中。

因为独立存储区在特定程序集的范围内,所以其他大多数托管代码都不能访问您的代码的数据(高度受信任的托管代码和管理工具可以从其他程序集访问存储区)。非托管代码可以访问任何独立存储区。

1. 打开Isolate Storage:

  首先引入命名空间System.IO.IsolatedStorage;

   打开storage IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForApplication();

  2. 创建文件夹:

  isf.CreateDirectory("MyFolder");

  3. 创建文件:

    StreamWriter writer = new StreamWriter(new IsolatedStorageFileStream("MyFolder\\my          File.txt", FileMode.OpenOrCreate, isf));

writer.WriteLine(“Hello World!”);

writer.Close();

  4. 读取文件:

    StreamReader reader = new StreamReader(new IsolatedStorageFileStream("MyFolder\\myFile.txt", FileMode.Open, isf));

string text = reader.ReadLine();

  5. 删除文件:

  isf.DeleteFile("MyFolder\\myFile.txt");

  6. 删除文件夹:

  isf.DeleteDirectory("MyFolder");

  7. 判断文件或文件夹是否存在:

  isf.FileExit("MyFolder\\myFile.txt");

  isf.DirectoryExit("MyFolder");

  8.也可以使用IsolateStorageFileStream来操作文件或文件夹,用法和FileStream 基本相同.

  例子:   在一个子目录中创建了一个文本文件,并读取文件中的内容。我们还可以创建和删除目录,子目录及文件。创建一个新的

  IsolatedStorageFile对象,并使用一个IsolatedStorageFileStream对象将它写入到驱动器中。

  using System.IO.IsolatedStorage;   using System.IO;

  private void SaveButton_Click(object sender, RoutedEventArgs e)   {    //为程序获取一个虚拟的本地存储   IsolatedStorageFile fileStorage = IsolatedStorageFile.GetUserStoreForApplication();

  //创建一个新的文件夹   fileStorage.CreateDirectory("textFiles");

  //创建一个txt文件的流    StreamWriter fileWriter = new StreamWriter(new IsolatedStorageFileStream("textFiles\\newText.txt",  

   FileMode.OpenOrCreate, fileStorage));    //向文件中写出内容   fileWriter.WriteLine(writeText.Text);   //关闭StreamWriter.    fileWriter.Close();   }

  private void GetButton_Click(object sender, RoutedEventArgs e)   {    //为程序获取一个虚拟的本地存储    IsolatedStorageFile fileStorage = IsolatedStorageFile.GetUserStoreForApplication();    //创建一个新的StreamReader    StreamReader fileReader = null;

   try    {   //读取文件   fileReader = new StreamReader(new IsolatedStorageFileStream("textFiles\\newText.txt", FileMode.Open,

     fileStorage));    //读取内容    string textFile = fileReader.ReadLine();   viewText.Text = textFile;    fileReader.Close();   }    catch    {    viewText.Text = "Need to create directory and the file first.";   }   }
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: