Windows Phone开发(28):隔离存储B
来源:互联网 发布:程序员代码图片 编辑:程序博客网 时间:2024/06/06 13:06
上一节我们聊了目录的操作,这一节我们继续来看看如何读写文件。
首先说一下题外话,许多朋友都在摇摆不定,三心二意,其实这样的学习态度是很不好的,如果你对Windows phone开发有兴趣,如果你真想学习,你就应该一心一意,静下心来学习。
如果你不喜欢Windows phone开发,那你不必要徘徊,你可以选择IOS、Android或者其它平台。
只要你选择了,你应该要相信你所选择的,记得有一句话是这样说的:选择你所爱的,爱你所选择的,虽然这个比方不大合适,但意思是相近的。
其实,说到底,不是编程有多么难学,而很多半途而废的,其根本问题就是学习态度,我们不应该说我们的长辈,像60、70后的这一辈人怎么落后,怎么跟不上时代了,对的,从知识的积累和技能上说,我们的长辈们的的确确跟不上时代了,但是,他们身上有一个优点,这个优点是我们80后,90后的年轻人身上所没有的,那就是执着,敢于吃苦的精神,这是事实,希望各位朋友要正视这一点,人不怕缺点多,就怕你不敢面对你的缺点。
作为青春年少的我们,更应该有一种“敢于直面惨淡的人生,敢于正视淋漓的鲜血”的勇气。
只要你喜欢,不用担心Windows phone的未来,就好像当年你不必要担心.NET的前途一个道理,也不要被一些新闻和评论吓倒,作为理性的主体,我们更应该分辨真伪,许多新闻评论都是在误导我们。
不要管它微软动作慢不慢,市场目前是很小,但你知道,存在必有其价值,IT巨头们都在激烈竞争,作为开发者,我们只需要脚踏实地去学习。
最近,谷歌和甲骨文的员工在努力学习法律知识,而微软的员工在努力学习市场营销学,其实从这些现象我们知道,无论开源闭源,都各有优缺点,能在二者之间取得一个平衡,才是王道。
好了,废话就说到这里,今天的内容很简单,所以我才说那么多题外话,目的就是告诉各位WP开发者,不要浮躁,只要你能把WP开发的技能练得出神入化,哪怕它市场很小,你也能赚大钱,马宁就是一个成功案例。
隔离存储的文件读写与我们过去在其它.NET开发中的文件读写是没有区别的,只是在WP上我们用IsolatedStorageFileStream,而不是传统的FileStream罢了,说白了,就是换了一个类名,所有操作都一样,至于你信不信,反正我是信了。
现在,我用一个示例来证明,读写文件有多么简单。
新建一个项目,在主页面上放一个文本框,用来输入要写入文件的内容,放两个按钮,一个用于写操作,一个用于读操作,再放一个TextBlock,用于显示从文件读入的内容。XAML布局如下所示。
<phone:PhoneApplicationPage x:Class="PhoneApp1.MainPage" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone" xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="d" d:DesignWidth="480" d:DesignHeight="768" FontFamily="{StaticResource PhoneFontFamilyNormal}" FontSize="{StaticResource PhoneFontSizeNormal}" Foreground="{StaticResource PhoneForegroundBrush}" SupportedOrientations="Portrait" Orientation="Portrait" shell:SystemTray.IsVisible="True"> <StackPanel> <StackPanel Margin="0,25" Orientation="Vertical"> <TextBox Name="txtContent" HorizontalAlignment="Stretch" Height="185" ScrollViewer.VerticalScrollBarVisibility="Auto" TextWrapping="Wrap"/> <Button Name="btnWrite" HorizontalAlignment="Stretch" Height="auto" Content="将内容写入到文件" Click="btnWrite_Click"/> </StackPanel> <StackPanel Margin="0,25" Orientation="Vertical"> <Button HorizontalAlignment="Stretch" Content="从文件中读入" Name="btnRead" Click="btnRead_Click"/> <TextBlock Name="txtDisplay" HorizontalAlignment="Stretch" Height="358" ScrollViewer.VerticalScrollBarVisibility="Auto" TextWrapping="Wrap" FontSize="35"/> </StackPanel> </StackPanel></phone:PhoneApplicationPage>
后台C#如下所示。
using System;using System.Collections.Generic;using System.Linq;using System.Net;using System.Windows;using System.Windows.Controls;using System.Windows.Documents;using System.Windows.Input;using System.Windows.Media;using System.Windows.Media.Animation;using System.Windows.Shapes;using Microsoft.Phone.Controls;// 引入此命名空间using System.IO;using System.IO.IsolatedStorage;namespace PhoneApp1{ public partial class MainPage : PhoneApplicationPage { // 常量 const string MyDir = "MyData"; const string testFileName = "file"; // 构造函数 public MainPage() { InitializeComponent(); this.Loaded += (sender, e) => { using (var iso = IsolatedStorageFile.GetUserStoreForApplication()) { if (iso.DirectoryExists(MyDir) == false) { iso.CreateDirectory(MyDir); } } }; } private void btnWrite_Click(object sender, RoutedEventArgs e) { using (var iso = IsolatedStorageFile.GetUserStoreForApplication()) { using (var sr = iso.CreateFile(MyDir + "\\" + testFileName)) { StreamWriter sw = new StreamWriter(sr); sw.Write(txtContent.Text); sw.Close(); sw.Dispose(); } } } private void btnRead_Click(object sender, RoutedEventArgs e) { using (var iso = IsolatedStorageFile.GetUserStoreForApplication()) { var sr = iso.OpenFile(MyDir + "\\" + testFileName, FileMode.Open, FileAccess.Read); StreamReader reader = new StreamReader(sr); string info = reader.ReadToEnd(); reader.Close(); reader.Dispose(); sr.Close(); sr.Dispose(); txtDisplay.Text = info; } } }}
上面的代码,我想各位能看得懂的,是的,就是这么简单,现在你相信了吧。
来,看看运后的结果吧。
- Windows Phone开发(28):隔离存储B
- Windows Phone开发(28):隔离存储B .
- Windows Phone开发(27):隔离存储A
- Windows Phone开发(29):隔离存储C
- Windows Phone开发(27):隔离存储A .
- Windows Phone开发(29):隔离存储C .
- Windows Phone 8中的隔离式存储机制分析
- 【转】Windows Phone在隔离存储里存取图片文件
- Windows Phone 7 开发 独立存储
- Windows Phone开发之独立存储二
- Windows Phone 独立存储
- windows phone:独立存储
- Windows Phone 隔离存储空间及文件操作
- Windows Phone 7 隔离存储空间资源管理器
- Windows Phone 7 开发——独立存储
- [Windows Phone] Windows Phone开发环境搭建
- Windows Phone开发(五)-- 导航控制
- Windows Phone开发(1):概论
- SSH的转发
- POJ 1734 Sightseeing trip Floyd求最小环
- MongoDB升级操作
- oracle专用模式修改为共享模式
- 关于提问的问题
- Windows Phone开发(28):隔离存储B
- 打印九九乘法表
- 16个经典面试问题回答思路
- mount状态下解决ora-01157
- Highways(POJ2485)
- TinyXML不支持UNICODE的解决方法【改造TinyXML】
- 微软SQL Server 2012新特性Silverlight报表客户端 - Power View
- 用DBCA配置database control
- java笔记