c#學習之windows phone 以及windows app的開發

來源:互聯網
上載者:User

標籤:

兩周的實驗展示課已經結束了,看到別人的各種高大上的應用和使用各種遊戲引擎的遊戲,感覺都好進階。比較之下,感覺自己做的記事本和賬單的結合的一個應用有一點low。但是畢竟是自己通過查閱各種網上資料和書本做出來的。還是,應該高興一小下吧。

下面說一下windows phone 和windows app的開發過程中遇到的問題。

最主要的是剛開始對檔案的儲存不知道用什麼方法去儲存。起初,從網上找到了一些資料,上面寫道使用var appStorage = IsolatedStorageFile.GetUserStoreForApplication();

這種方法儲存,當點擊確定按鈕是就開始儲存。代碼如下:

  private void quedingbutton_Click(object sender, RoutedEventArgs e)        {            var appStorage = IsolatedStorageFile.GetUserStoreForApplication();            //構建檔案全名,檔案名稱由時間和檔案名稱組成.txt.            StringBuilder filename = new StringBuilder();            var data = dataPicker1.ValueString;//擷取到使用者建立事件時填寫的時間                     filename.Append(data);                      filename.Append("_");            var data2 = timerPick1.ValueString;//            filename.Append(data2);            filename.Append("_");            filename.Append(filenametextbox.Text.ToString());   //擷取到使用者建立事件時填寫的事件的名稱                    filename.Append(".txt");            string mycfilename = filename.ToString();            //建立檔案,並向檔案中填寫內容,建立檔案名稱中不能含有/或者:,否則建立失敗            string mycfilename1 = mycfilename.Replace("/", "_");            string mycfilename2 = mycfilename1.Replace(":", "_");            try            {                if (!appStorage.FileExists(mycfilename2))                {                    using (var file = appStorage.CreateFile(mycfilename2))                    {                        using (var writer = new StreamWriter(file))                        {                            writer.WriteLine(this.filecontent.Text);                        }                    }                }                else                {                    IsolatedStorageFileStream stream = appStorage.OpenFile(mycfilename2, FileMode.Open);                    using (StreamWriter writer= new StreamWriter(stream))                    {                        //向檔案中寫入內容                        //filecontent.Text = reader.ReadLine();                        writer.WriteLine(filecontent.Text);                        writer.Close();                    }

但是這是在windows phone7中使用的,在windows phone和windowsapp中根本沒法使用。

之後也想過使用資料庫來儲存(因為正在學習資料庫),但是最後還是不知道什麼原因的放棄掉了。最後,通過繼續查閱資料。最終還是決定使用xml來隱藏檔。

使用方法如下:

  private async void quedingbtn_Click(object sender, RoutedEventArgs e)        {            StorageFolder storage = await ApplicationData.Current.LocalFolder.GetFolderAsync("MyList");            XmlDocument _doc = new XmlDocument();            if (mingcheng.Text == string.Empty || jiage.Text == string.Empty || shuliang.Text == string.Empty)            {                await new MessageDialog("請輸入").ShowAsync();            }            else            {                XmlElement _item = _doc.CreateElement(mingcheng.Text);                _item.SetAttribute("mingcheng", mingcheng.Text);                _item.SetAttribute("shuliang", shuliang.Text);                _item.SetAttribute("jiage", jiage.Text);                                _doc.AppendChild(_item);                StorageFile file2 = await storage.CreateFileAsync(mingcheng.Text + ".xml", CreationCollisionOption.ReplaceExisting);                await _doc.SaveToFileAsync(file2);                Frame.Navigate(typeof(MainPage2), file2);            }

不過xml還是有一個恒大的缺點就是當資料量很大時,非常低效,非常。不過我想對於這個應用應該還是足夠的。

 

資料的綁定,使用binding和自己寫的類中的屬性來綁定

eg::

<Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0" Background="#FFF9F970 ">            <ListBox Name="shijianlistbox"   HorizontalAlignment="Stretch" VerticalAlignment="Stretch"                      SelectionChanged="shijianlistbox_SelectionChanged">                <ListBox.ItemTemplate>                    <DataTemplate>                        <StackPanel>                            <!--<Button  Name="shijianButton"                                             FontSize="32"                                             Foreground="Black"                                             Content="{Binding filename}"                                                                            />-->                            <TextBlock Name="shijiantextblock"  FontSize="32"                                             Foreground="Black"                                       Text="{Binding filename}"/>                            <StackPanel Orientation="Horizontal">                               <TextBlock Name="shijianneirong"                                       Text="{Binding createdate}"                                       Foreground="Black"                                       Margin="10">                                </TextBlock>                                <TextBlock Name="noteDateCreated"                                                                              Text="{Binding createdatatime}"                                       Foreground="Black"                                       Margin="10">                                </TextBlock>                            </StackPanel>                                                   </StackPanel>                    </DataTemplate>                </ListBox.ItemTemplate>            </ListBox>

使用綁定 Binding filename  、Binding createdate、  Binding createdatatime來使xaml中的控制項textbox 或textblock中的text綁定。

最後說一下我自己感覺還不錯的地方吧,這個介面就是一個textblock和幾個按鈕還有就是一個空的listbox,裡面沒有加任何其他的控制項。向裡面添加其他的內容比如說textblock和button都是在後面的控制項的定義中實現的:

   async void MainPage_Loaded(object sender, RoutedEventArgs e)        {            list2.Items.Clear();            StorageFolder storage = await ApplicationData.Current.LocalFolder.CreateFolderAsync("MyList", CreationCollisionOption.OpenIfExists);            var files = await storage.GetFilesAsync();            {                foreach (StorageFile file in files)                {                    Grid a = new Grid();                    ColumnDefinition col = new ColumnDefinition();                    GridLength gl = new GridLength(600);                    col.Width = gl;                    a.ColumnDefinitions.Add(col);                    ColumnDefinition col2 = new ColumnDefinition();                    GridLength gl2 = new GridLength(200);                    col2.Width = gl;                    a.ColumnDefinitions.Add(col2);                    TextBlock txbx = new TextBlock();                    txbx.Text = file.DisplayName;                    Grid.SetColumn(txbx, 0);                    HyperlinkButton btn = new HyperlinkButton();                    btn.Width = 200;                    btn.Content = "查看詳細";                    btn.Name = file.DisplayName;                    btn.Click += (s, ea) =>                    {                        Frame.Navigate(typeof(chakan2), file);                    };                    Grid.SetColumn(btn, 1);                    a.Children.Add(txbx);                    a.Children.Add(btn);                    list2.Items.Add(a);                }            }        }

感覺這樣就可以輕鬆擷取控制項的內容,個人認為這樣比在listBox中直接增加空間好的多。

 

c#學習之windows phone 以及windows app的開發

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.