Windows Phone开发(28):隔离存储B

移动开发
今天的内容很简单,目的就是告诉各位WP开发者,不要浮躁,只要你能把WP开发的技能练得出神入化,哪怕它市场很小,你也能赚大钱,马宁就是一个成功案例。

上一节我们聊了目录的操作,这一节我们继续来看看如何读写文件。

首先说一下题外话,许多朋友都在摇摆不定,三心二意,其实这样的学习态度是很不好的,如果你对Windows phone开发有兴趣,如果你真想学习,你就应该一心一意,静下心来学习。

如果你不喜欢Windows phone开发,那你不必要徘徊,你可以选择IOS、Android或者其它平台。

只要你选择了,你应该要相信你所选择的,记得有一句话是这样说的:选择你所爱的,爱你所选择的,虽然这个比方不大合适,但意思是相近的。

其实,说到底,不是编程有多么难学,而很多半途而废的,其根本问题就是学习态度,我们不应该说我们的长辈,像60、70后的这一辈人怎么落后,怎么 跟不上时代了,对的,从知识的积累和技能上说,我们的长辈们的的确确跟不上时代了,但是,他们身上有一个优点,这个优点是我们80后,90后的年轻人身上 所没有的,那就是执着,敢于吃苦的精神,这是事实,希望各位朋友要正视这一点,人不怕缺点多,就怕你不敢面对你的缺点。

作为青春年少的我们,更应该有一种“敢于直面惨淡的人生,敢于正视淋漓的鲜血”的勇气。

只要你喜欢,不用担心Windows phone的未来,就好像当年你不必要担心.NET的前途一个道理,也不要被一些新闻和评论吓倒,作为理性的主体,我们更应该分辨真伪,许多新闻评论都是在误导我们。

不要管它微软动作慢不慢,市场目前是很小,但你知道,存在必有其价值,IT巨头们都在激烈竞争,作为开发者,我们只需要脚踏实地去学习。

最近,谷歌和甲骨文的员工在努力学习法律知识,而微软的员工在努力学习市场营销学,其实从这些现象我们知道,无论开源闭源,都各有优缺点,能在二者之间取得一个平衡,才是王道。

好了,废话就说到这里,今天的内容很简单,所以我才说那么多题外话,目的就是告诉各位WP开发者,不要浮躁,只要你能把WP开发的技能练得出神入化,哪怕它市场很小,你也能赚大钱,马宁就是一个成功案例。

隔离存储的文件读写与我们过去在其它.NET开发中的文件读写是没有区别的,只是在WP上我们用IsolatedStorageFileStream,而不是传统的FileStream罢了,说白了,就是换了一个类名,所有操作都一样,至于你信不信,反正我是信了。

现在,我用一个示例来证明,读写文件有多么简单。

新建一个项目,在主页面上放一个文本框,用来输入要写入文件的内容,放两个按钮,一个用于写操作,一个用于读操作,再放一个TextBlock,用于显示从文件读入的内容。XAML布局如下所示。

  1. <phone:PhoneApplicationPage  
  2.     x:Class="PhoneApp1.MainPage" 
  3.     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
  4.     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
  5.     xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone" 
  6.     xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone" 
  7.     xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
  8.     xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
  9.     mc:Ignorable="d" d:DesignWidth="480" d:DesignHeight="768" 
  10.     FontFamily="{StaticResource PhoneFontFamilyNormal}" 
  11.     FontSize="{StaticResource PhoneFontSizeNormal}" 
  12.     Foreground="{StaticResource PhoneForegroundBrush}" 
  13.     SupportedOrientations="Portrait" Orientation="Portrait" 
  14.     shell:SystemTray.IsVisible="True"
  15.     <StackPanel> 
  16.         <StackPanel Margin="0,25" Orientation="Vertical"
  17.             <TextBox Name="txtContent" HorizontalAlignment="Stretch" Height="185" ScrollViewer.VerticalScrollBarVisibility="Auto" TextWrapping="Wrap"/> 
  18.             <Button Name="btnWrite" HorizontalAlignment="Stretch" Height="auto" Content="将内容写入到文件" Click="btnWrite_Click"/> 
  19.         </StackPanel> 
  20.         <StackPanel Margin="0,25" Orientation="Vertical"
  21.             <Button HorizontalAlignment="Stretch" Content="从文件中读入" Name="btnRead" Click="btnRead_Click"/> 
  22.             <TextBlock Name="txtDisplay" HorizontalAlignment="Stretch" Height="358" ScrollViewer.VerticalScrollBarVisibility="Auto" TextWrapping="Wrap" FontSize="35"/> 
  23.         </StackPanel> 
  24.     </StackPanel> 
  25. </phone:PhoneApplicationPage> 

后台C#如下所示。

  1. using System; 
  2. using System.Collections.Generic; 
  3. using System.Linq; 
  4. using System.Net; 
  5. using System.Windows; 
  6. using System.Windows.Controls; 
  7. using System.Windows.Documents; 
  8. using System.Windows.Input; 
  9. using System.Windows.Media; 
  10. using System.Windows.Media.Animation; 
  11. using System.Windows.Shapes; 
  12. using Microsoft.Phone.Controls; 
  13. // 引入此命名空间 
  14. using System.IO; 
  15. using System.IO.IsolatedStorage; 
  16. namespace PhoneApp1 
  17.     public partial class MainPage : PhoneApplicationPage 
  18.     { 
  19.         // 常量 
  20.         const string MyDir = "MyData"
  21.         const string testFileName = "file"
  22.         // 构造函数 
  23.         public MainPage() 
  24.         { 
  25.             InitializeComponent(); 
  26.             this.Loaded += (sender, e) => 
  27.                 { 
  28.                     using (var iso = IsolatedStorageFile.GetUserStoreForApplication()) 
  29.                     { 
  30.                         if (iso.DirectoryExists(MyDir) == false
  31.                         { 
  32.                             iso.CreateDirectory(MyDir); 
  33.                         } 
  34.                     } 
  35.                 }; 
  36.         } 
  37.         private void btnWrite_Click(object sender, RoutedEventArgs e) 
  38.         { 
  39.             using (var iso = IsolatedStorageFile.GetUserStoreForApplication()) 
  40.             { 
  41.                 using (var sr = iso.CreateFile(MyDir + "\\" + testFileName)) 
  42.                 { 
  43.                     StreamWriter sw = new StreamWriter(sr); 
  44.                     sw.Write(txtContent.Text); 
  45.                     sw.Close(); 
  46.                     sw.Dispose(); 
  47.                 } 
  48.             } 
  49.         } 
  50.         private void btnRead_Click(object sender, RoutedEventArgs e) 
  51.         { 
  52.             using (var iso = IsolatedStorageFile.GetUserStoreForApplication()) 
  53.             { 
  54.                 var sr = iso.OpenFile(MyDir + "\\" + testFileName, FileMode.Open, FileAccess.Read); 
  55.                 StreamReader reader = new StreamReader(sr); 
  56.                 string info = reader.ReadToEnd(); 
  57.                 reader.Close(); 
  58.                 reader.Dispose(); 
  59.                 sr.Close(); 
  60.                 sr.Dispose(); 
  61.                 txtDisplay.Text = info; 
  62.             } 
  63.         } 
  64.     } 

上面的代码,我想各位能看得懂的,是的,就是这么简单,现在你相信了吧。

来,看看运后的结果吧。

责任编辑:闫佳明 来源: oschina
相关推荐

2013-04-19 15:35:54

Windows Pho隔离存储

2013-04-19 16:14:36

Windows PhoWindows Pho

2014-12-22 14:21:57

Windows Pho隔离式存储机制

2012-08-01 10:26:33

Windows Pho

2013-07-30 12:37:56

Windows PhoWindows Pho

2010-04-21 17:07:54

Windows Pho

2010-12-01 09:01:31

独立存储Windows Pho

2011-06-07 12:42:15

Windows Pho

2013-04-17 14:00:06

Windows PhoWindows Pho

2013-07-30 11:18:37

Windows PhoWindows Pho

2013-04-19 16:34:56

Windows PhoWindows Pho

2013-04-16 17:02:50

Windows Pho概论

2010-04-08 17:40:23

Windows Pho

2013-04-17 14:47:19

Windows PhoWindows Pho

2011-06-07 11:35:38

Windows Pho

2013-07-31 13:03:51

Windows PhoWindows Pho

2010-07-16 15:29:02

Windows Pho

2012-08-16 10:35:50

Windows Pho

2013-04-17 13:27:04

Windows PhoWindows Pho

2010-12-14 18:48:49

微软
点赞
收藏

51CTO技术栈公众号