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

來源:互聯網
上載者:User

上一節我們聊了目錄的操作,這一節我們繼續來看看如何讀寫檔案。

首先說一下題外話,許多朋友都在搖擺不定,三心二意,其實這樣的學習態度是很不好的,如果你對Windows phone開發有興趣,如果你真想學習,你就應該一心一意,靜下心來學習。

如果你不喜歡Windows phone開發,那你不必要徘徊,你可以選擇IOS、Android或者其它平台。
只要你選擇了,你應該要相信你所選擇的,記得有一句話是這樣說的:選擇你所愛的,愛你所選擇的,雖然這個比方不大合適,但意思是相近的。

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

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

只要你喜歡,不用擔心Windows phone的未來,就好像當年你不必要擔心.NET的前途一個道理,也不要被一些新聞和評論嚇倒,作為理性的主體,我們更應該分辨真偽,許多新聞評論都是在誤導我們。
不要管它微軟動作慢不慢,市場目前是很小,但你知道,存在必有其價值,IT巨頭們都在激烈競爭,作為開發人員,我們只需要腳踏實地去學習。
最近,Google和甲骨文的員工在努力學習法律知識,而微軟的員工在努力學習市場營銷學,其實從這些現象我們知道,無論開源閉源,都各有優缺點,能在二者之間取得一個平衡,才是王道。

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

隔離儲存的檔案讀寫與我們過去在其它.NET開發中的檔案讀寫是沒有區別的,只是在WP上我們用IsolatedStorageFileStream,而不是傳統的FileStream罷了,說白了,就是換了一個類名,所有操作都一樣,至於你信不信,反正我是信了。

現在,我用一個樣本來證明,讀寫檔案有多麼簡單。
建立一個項目,在首頁面上放一個文字框,用來輸入要寫入檔案的內容,放兩個按鈕,一個用於寫操作,一個用於讀操作,再放一個TextBlock,用於顯示從檔案讀入的內容。XAML布局如下所示。

 

<phone:PhoneApplicationPage<br /> x:Class="PhoneApp1.MainPage"<br /> xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"<br /> xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"<br /> xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone"<br /> xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone"<br /> xmlns:d="http://schemas.microsoft.com/expression/blend/2008"<br /> xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"<br /> mc:Ignorable="d" d:DesignWidth="480" d:DesignHeight="768"<br /> FontFamily="{StaticResource PhoneFontFamilyNormal}"<br /> FontSize="{StaticResource PhoneFontSizeNormal}"<br /> Foreground="{StaticResource PhoneForegroundBrush}"<br /> SupportedOrientations="Portrait" Orientation="Portrait"<br /> shell:SystemTray.IsVisible="True"><br /> <StackPanel><br /> <StackPanel Margin="0,25" Orientation="Vertical"><br /> <TextBox Name="txtContent" HorizontalAlignment="Stretch" Height="185" ScrollViewer.VerticalScrollBarVisibility="Auto" TextWrapping="Wrap"/><br /> <Button Name="btnWrite" HorizontalAlignment="Stretch" Height="auto" Content="將內容寫入到檔案" Click="btnWrite_Click"/><br /> </StackPanel><br /> <StackPanel Margin="0,25" Orientation="Vertical"><br /> <Button HorizontalAlignment="Stretch" Content="從檔案中讀入" Name="btnRead" Click="btnRead_Click"/><br /> <TextBlock Name="txtDisplay" HorizontalAlignment="Stretch" Height="358" ScrollViewer.VerticalScrollBarVisibility="Auto" TextWrapping="Wrap" FontSize="35"/><br /> </StackPanel><br /> </StackPanel></p><p></phone:PhoneApplicationPage>

後台C#如下所示。

using System;<br />using System.Collections.Generic;<br />using System.Linq;<br />using System.Net;<br />using System.Windows;<br />using System.Windows.Controls;<br />using System.Windows.Documents;<br />using System.Windows.Input;<br />using System.Windows.Media;<br />using System.Windows.Media.Animation;<br />using System.Windows.Shapes;<br />using Microsoft.Phone.Controls;<br />// 引入此命名空間<br />using System.IO;<br />using System.IO.IsolatedStorage;</p><p>namespace PhoneApp1<br />{<br /> public partial class MainPage : PhoneApplicationPage<br /> {<br /> // 常量<br /> const string MyDir = "MyData";<br /> const string testFileName = "file";</p><p> // 建構函式<br /> public MainPage()<br /> {<br /> InitializeComponent();<br /> this.Loaded += (sender, e) =><br /> {<br /> using (var iso = IsolatedStorageFile.GetUserStoreForApplication())<br /> {<br /> if (iso.DirectoryExists(MyDir) == false)<br /> {<br /> iso.CreateDirectory(MyDir);<br /> }<br /> }<br /> };<br /> }</p><p> private void btnWrite_Click(object sender, RoutedEventArgs e)<br /> {<br /> using (var iso = IsolatedStorageFile.GetUserStoreForApplication())<br /> {<br /> using (var sr = iso.CreateFile(MyDir + "\\" + testFileName))<br /> {<br /> StreamWriter sw = new StreamWriter(sr);<br /> sw.Write(txtContent.Text);<br /> sw.Close();<br /> sw.Dispose();<br /> }<br /> }<br /> }</p><p> private void btnRead_Click(object sender, RoutedEventArgs e)<br /> {<br /> using (var iso = IsolatedStorageFile.GetUserStoreForApplication())<br /> {<br /> var sr = iso.OpenFile(MyDir + "\\" + testFileName, FileMode.Open, FileAccess.Read);<br /> StreamReader reader = new StreamReader(sr);<br /> string info = reader.ReadToEnd();<br /> reader.Close();<br /> reader.Dispose();<br /> sr.Close();<br /> sr.Dispose();<br /> txtDisplay.Text = info;<br /> }<br /> }<br /> }<br />}

 

上面的代碼,我想各位能看得懂的,是的,就是這麼簡單,現在你相信了吧。

來,看看運後的結果吧。

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.