Windows 通用应用尝试开发 “51单片机汇编”总结

一、前言

  终于完成windows通用应用“51单片机汇编”,半年前开始玩WindowsPhone开发的第一个真正意义上的App(还很多缺点=_=)。开发从1月中旬考完试到今天,期间实习了半个月,玩了几天,算起来基本弄了3个多星期吧。不多说,总结总结。

二、开发数据准备

  应用中主要的数据是单片机的汇编指令,我主要用XML文件来储存数据,没有使用SQLLite数据库,数据格式如下图:

xml文件的数据是我手输入的,所以这是比较烦的。(可能有更简洁的办法获取数据)。

  而xml文件每个每个节点对应实例,如Code字节下的实例C#代码如下:

 public Code(String uniqueId, String title, String subtitle, String imagepath, String insertTime, String description, String exmaple, String comment,string collectFlag)
        {
            this.UniqueId = uniqueId;
            this.Title = title;
            this.Subtitle = subtitle;
            this.ImagePath = imagepath;
            this.InsertTime = insertTime;
            this.Description = description;
            this.Example = exmaple;
            this.Comment = comment;
            this.CollectFlag = collectFlag;
        }

     
        public Code()
        {
            // TODO: Complete member initialization
        }
        public string UniqueId { get; set; }
        public string Title { get; set; }
        public string Subtitle { get; set; }
        public string ImagePath { get; set; }

        public string InsertTime { get; set; }
        public string Description { get; set; }
        public string Example { get; set; }
        public string Comment { get; set; }
        public string CollectFlag { get; set; }
        public override string ToString()
        {
            return this.Title;
        }
    }

  从xml文件取出数据则利用Linq to Xml,部分C#代码如下:

 public static async Task<Code> GetCodeAsync(string uniqueId)
        {
            //await _sampleDataSource.GetSampleDataAsync();
            //var matches = _sampleDataSource.Groups.SelectMany(group => group.Items).SelectMany(item=>item.Codes).Where((code)=>code.UniqueId.Equals(uniqueId));
            //if (matches.Count() == 1) return matches.First();
            //return null;
            //Uri dataUri = new Uri("ms-appx:///DataModel/SampleData.xml");
            //StorageFile file = await StorageFile.GetFileFromApplicationUriAsync(dataUri);
            StorageFolder localfolder = ApplicationData.Current.LocalFolder;
            StorageFile XMLfile = await localfolder.CreateFileAsync(FILENAME, CreationCollisionOption.OpenIfExists);
            string xmlText = await FileIO.ReadTextAsync(XMLfile);
            XDocument xmlObject;
            xmlObject = XDocument.Parse(xmlText);
            var data = (from query in xmlObject.Descendants("Code")
                        where query.Element("UniqueId").Value == uniqueId
                        select new Code((string)query.Element("UniqueId"),
                               (string)query.Element("Title"),
                                (string)query.Element("Subtitle"),
                                (string)query.Element("ImagePath"),
                                (string)query.Element("StoreFlag"),
                                (string)query.Element("Description"),
                              (string)query.Element("Example"),
                               (string)query.Element("Comment"),
                               XmlDataService.CheckHasAttributes(query))).FirstOrDefault();
            return data;
        }

PS:原本在程序中,我是直接从 利用Uri:ms-appx:///DataModel/SampleData.xml获取xml文件,同时后续操作对文件进行数据的改写,这种办法在WindowsPhone8.1完全可行,但在Windows8.1中,该文件是只读的,不能这样操作,否则会出现ACCESSDENY错误。我只能在程序第一次启动时,把SampleData.xml文件复制到LocalFolder里了,然后App以后对数据进行读写,都是对复制的文件进行读写(相比读取Installation里的文件,读取LocalFolder的文件,Windows应用没有发现什么不同,但在WinodwsPhone中,读取数据的操作相对慢点,在我的Lumia620 内存512M,感觉比较卡,不知道有什么办法解决)。负责文件操作主要是在App.xaml.cs,C#代码如下:

public App()
        {
            this.InitializeComponent();

            if ((string)localsetting.Values["IsHubFirstOpen"] != "true")
            {
                localsetting.Values["IsHubFirstOpen"] = "true";
                LoadXmlFile();
            }
            this.Suspending += this.OnSuspending;
        }
....
 public static async void LoadXmlFile()
        {
            string FILENAME = "CodeCopyFile.XML";
            StorageFolder localfolder = ApplicationData.Current.LocalFolder;
            var dataUri = new Uri("ms-appx:///DataModel/SampleData.xml");
            StorageFile file = await StorageFile.GetFileFromApplicationUriAsync(dataUri);  
            await file.CopyAsync(localfolder, FILENAME, NameCollisionOption.ReplaceExisting);
        }

二、对Xml文件数据进行改写

  这个App需要对用户的数据进行存储,我直接把用户自己的数据写到原文件中,比如App:

在Windows8.1中:

在WindowsPhone8.1中:

在两种平台都需要把用户自己的笔记保存下来。这里主要是对xml文件进行改写,十分简单,记录笔记方法C#代码如下:

public static async Task<bool> WriteComment(string uniqueId, string comment)
        {
            StorageFolder localfolder = ApplicationData.Current.LocalFolder;
            StorageFile XMLfile = await localfolder.CreateFileAsync(FILENAME, CreationCollisionOption.OpenIfExists);
            using (Stream stream = await XMLfile.OpenStreamForReadAsync())
            {
                XDocument xmlObject = XDocument.Load(stream);
                foreach (var codeValue in xmlObject.Descendants("Code").ToArray())
                {
                    if ((string)codeValue.Element("UniqueId") == uniqueId)
                    {
                        using (Stream newstream = await XMLfile.OpenStreamForWriteAsync())//这里需要注意,在修改编辑已存在的xml文件时需要设置Length=0,position=0,重新写。
                        {
                            DateTime time = DateTime.Now;
                            var insertTime = string.Format("修改时间:{0}年{1}月{2}日 {3}:{4}", time.Year, time.Month, time.Day, time.TimeOfDay.Hours, time.TimeOfDay.Minutes);
                            codeValue.SetElementValue("Comment", comment);
                            codeValue.SetElementValue("StoreFlag", insertTime);//修改时间
                            newstream.SetLength(0);
                            xmlObject.Save(newstream);
                            //   await newstream.FlushAsync();
                        }
                        return true;
                    }
                }
            }


            return false;
        }

三、UI

  本应用后台核心代码基本是对数据进行读写,这部分代码写好后,我就进行两个平台的UI设计了,说实话比较喜欢UI设计,挺好玩的。因为最开始学的就是WindowsPhone,WindowsPhone平台上的UI设计比较熟悉,比较快弄好,但是弄好后,一些界面在不同分辨率的模拟器手机会有点不同呈现,还需要之后的修改,比较喜欢自己设计的这个页面:

感觉挺好的。

  至于Windows8.1,之前没有学过开发windowsApp,比较费力,而且该应用在Windows上体验应该比较差吧,若屏幕纵向,完全不能看的节奏。在设计在Windows的App的 UI时,很多参考了@MS-UAP  http://www.cnblogs.com/ms-uap/  上面博客园的通用应用设计,比如TopBar基本是用MS-UAP的。

四、后续

  后续的话,代码整理整理。还有设计方面,在应用上小细节加一些WPF动画,感觉挺炫的;应用功能方面,还可以增加单片机定时器、串口速率等计算工具...弄完了,爽!!最后祝大家羊年快乐,心愿成真,身体健康。祝自己在大三下学期找个好实习,大四找个好工作,万事如意!!!

大家感兴趣的可以,下载看看,初次开发,多多建议!

51单片机汇编 Windows Store 链接 :http://apps.microsoft.com/windows/app/51/b8ffef35-7fa7-45d5-9cd5-f2672bf9a8fe

51单片机汇编 WindowsPhone Store 链接:http://www.windowsphone.com/s?appid=3519c5ac-f013-4663-8eb9-fd536fdd8c8f

51单片机汇编 代码:http://pan.baidu.com/s/1sjpyXat   密码:7e6l

原文地址:https://www.cnblogs.com/NEIL-X/p/4294542.html