• c#学习之windows phone 以及windows app的开发


    两周的实验展示课已经结束了,看到别人的各种高大上的应用和使用各种游戏引擎的游戏,感觉都好高级。比较之下,感觉自己做的记事本和账单的结合的一个应用有一点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中直接添加空间好的多。

  • 相关阅读:
    动态规划
    Python第二天学习
    Python第一天学习---基础语法
    java易错知识点
    C语言---指针复习
    排序汇总
    课程设计---创建族谱管理系统
    Vue第五篇 Vue的生命周期
    Vue第四篇 Vue路由系统
    Vue第三篇 Vue组件
  • 原文地址:https://www.cnblogs.com/zyqBlog/p/4520847.html
Copyright © 2020-2023  润新知