自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

Windows Phone開發(fā)(28):隔離存儲B

移動開發(fā)
今天的內(nèi)容很簡單,目的就是告訴各位WP開發(fā)者,不要浮躁,只要你能把WP開發(fā)的技能練得出神入化,哪怕它市場很小,你也能賺大錢,馬寧就是一個成功案例。

上一節(jié)我們聊了目錄的操作,這一節(jié)我們繼續(xù)來看看如何讀寫文件。

首先說一下題外話,許多朋友都在搖擺不定,三心二意,其實這樣的學(xué)習(xí)態(tài)度是很不好的,如果你對Windows phone開發(fā)有興趣,如果你真想學(xué)習(xí),你就應(yīng)該一心一意,靜下心來學(xué)習(xí)。

如果你不喜歡Windows phone開發(fā),那你不必要徘徊,你可以選擇IOS、Android或者其它平臺。

只要你選擇了,你應(yīng)該要相信你所選擇的,記得有一句話是這樣說的:選擇你所愛的,愛你所選擇的,雖然這個比方不大合適,但意思是相近的。

其實,說到底,不是編程有多么難學(xué),而很多半途而廢的,其根本問題就是學(xué)習(xí)態(tài)度,我們不應(yīng)該說我們的長輩,像60、70后的這一輩人怎么落后,怎么 跟不上時代了,對的,從知識的積累和技能上說,我們的長輩們的的確確跟不上時代了,但是,他們身上有一個優(yōu)點,這個優(yōu)點是我們80后,90后的年輕人身上 所沒有的,那就是執(zhí)著,敢于吃苦的精神,這是事實,希望各位朋友要正視這一點,人不怕缺點多,就怕你不敢面對你的缺點。

作為青春年少的我們,更應(yīng)該有一種“敢于直面慘淡的人生,敢于正視淋漓的鮮血”的勇氣。

只要你喜歡,不用擔(dān)心Windows phone的未來,就好像當(dāng)年你不必要擔(dān)心.NET的前途一個道理,也不要被一些新聞和評論嚇倒,作為理性的主體,我們更應(yīng)該分辨真?zhèn)危S多新聞評論都是在誤導(dǎo)我們。

不要管它微軟動作慢不慢,市場目前是很小,但你知道,存在必有其價值,IT巨頭們都在激烈競爭,作為開發(fā)者,我們只需要腳踏實地去學(xué)習(xí)。

最近,谷歌和甲骨文的員工在努力學(xué)習(xí)法律知識,而微軟的員工在努力學(xué)習(xí)市場營銷學(xué),其實從這些現(xiàn)象我們知道,無論開源閉源,都各有優(yōu)缺點,能在二者之間取得一個平衡,才是王道。

好了,廢話就說到這里,今天的內(nèi)容很簡單,所以我才說那么多題外話,目的就是告訴各位WP開發(fā)者,不要浮躁,只要你能把WP開發(fā)的技能練得出神入化,哪怕它市場很小,你也能賺大錢,馬寧就是一個成功案例。

隔離存儲的文件讀寫與我們過去在其它.NET開發(fā)中的文件讀寫是沒有區(qū)別的,只是在WP上我們用IsolatedStorageFileStream,而不是傳統(tǒng)的FileStream罷了,說白了,就是換了一個類名,所有操作都一樣,至于你信不信,反正我是信了。

現(xiàn)在,我用一個示例來證明,讀寫文件有多么簡單。

新建一個項目,在主頁面上放一個文本框,用來輸入要寫入文件的內(nèi)容,放兩個按鈕,一個用于寫操作,一個用于讀操作,再放一個TextBlock,用于顯示從文件讀入的內(nèi)容。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="將內(nèi)容寫入到文件" 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.         // 構(gòu)造函數(shù) 
  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.     } 

上面的代碼,我想各位能看得懂的,是的,就是這么簡單,現(xiàn)在你相信了吧。

來,看看運后的結(jié)果吧。

責(zé)任編輯:閆佳明 來源: oschina
相關(guān)推薦

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隔離式存儲機(jī)制

2012-08-01 10:26:33

Windows Pho

2010-04-21 17:07:54

Windows Pho

2013-07-30 12:37:56

Windows PhoWindows Pho

2010-12-01 09:01:31

獨立存儲Windows Pho

2013-04-16 17:02:50

Windows Pho概論

2013-04-19 16:34:56

Windows PhoWindows Pho

2013-07-30 11:18:37

Windows PhoWindows Pho

2011-06-07 12:42:15

Windows Pho

2013-04-17 14:00:06

Windows PhoWindows Pho

2010-04-08 17:40:23

Windows Pho

2010-12-14 18:48:49

微軟

2013-07-31 13:13:50

Windows PhoMVVM模式

2012-06-04 14:47:58

Windows Pho

2013-04-19 16:52:24

Windows PhoWindows Pho

2013-07-31 12:50:39

搭建Windows PWindows Pho

2013-04-17 13:27:04

Windows PhoWindows Pho

2011-06-07 11:35:38

Windows Pho
點贊
收藏

51CTO技術(shù)棧公眾號