30 Days of .NET [Windows Mobile Applications] - Day 01: Minutes to Midnight Countdown(午夜倒数器)
2009-05-20 20:34
621 查看
得到原作者 Chris Craft 的同意,本人可以翻译他的系列文章 30 Days of .NET [Windows Mobile Applications]并在博客园里发表。这是一个十分趣味性很高的系列,通过这个系列的学习,可以掌握Windows Mobile开发很多技巧,包括GPS,Bluetooth,界面编程,多线程等等。
在这个系列的文章,我不会逐句翻译,而是讨论需求的由来,对需求的分析思考,对实现的表述,以及对现有实现的改进想法,希望能成为原文的补充。第一篇文章是实现一个午夜倒数器。原文见 Day 01: Minutes to Midnight Countdown
其实很多时候,我们做事情光有想法,不付诸行动,或者行动中半途而废。作者在开始的时候也怀疑自己是否有决心把事情做完,最终的结果显示Chris Craft做到了,我希望也向Chris Craft学习,把这个系列的文章学一遍。如果你还是在Windows Mobile开发的门外徘徊,可以跟着这个系列每天学一点,想一点,不多也不少,打开Windows Mobile开发精彩之门,感受个中乐趣。
这些程序很简单,关键运用了Timer,DateTime和进程条(Progress Bar)。
Timer是通过控件使用的,所以回调时间在属性里进行配置,如下:
图1
图2
Interval设置为100表示每100毫秒回调一次timer_Tick(object sender, EventArgs e)。
private void timer_Tick(object sender, EventArgs e)
{
TimeSpan timeSpan = DateTime.Now.Date.AddDays(1) - DateTime.Now;
labelHours.Text = string.Format("{0} of 24 hours left", timeSpan.Hours);
labelMinutes.Text = string.Format("{0} of 60 minutes left", timeSpan.Minutes);
labelSeconds.Text = string.Format("{0} of 60 seconds left", timeSpan.Seconds);
labelTotalMinutes.Text = string.Format("{0} of 1440 total minutes left", timeSpan.TotalMinutes.ToString("#.0"));
labelTotalSeconds.Text = string.Format("{0} of 86400 total seconds left", timeSpan.TotalSeconds);
progressBarTotal.Value = 86400 - (int) timeSpan.TotalSeconds;
progressBarHours.Value = 24 - timeSpan.Hours;
progressBarMinutes.Value = 60 - timeSpan.Minutes;
progressBarSeconds.Value = 60 - timeSpan.Seconds;
progressBarTotalMinutes.Value = 1440 - (int) timeSpan.TotalMinutes;
progressBarTotalSeconds.Value = 86400 - (int) timeSpan.TotalSeconds;
}
这个回调函数的算法也很简单,DateTime.Now.Date.AddDays(1)取出午夜的时间DateTime对象,然后和当前时间相减得到TimeSpan的对象timeSpan,从timeSpan的取出当前和午夜的时间差呈现到Label以及ProgressBar上。在total minutes left上,作者做了处理,因为有时候会显示小数错误"X.666666666 of 1440 total minutes left".通过字符串的格式化以后timeSpan.TotalMinutes.ToString("#.0"),显示正常。
安装文件: minutes2Midnight.cab
源代码: minuates2Midnight.zip
.NET Compact Framework, WinCE, Windows Mobile开发系列
Jake's Blog in 博客园 -- 精简开发 无线生活
在这个系列的文章,我不会逐句翻译,而是讨论需求的由来,对需求的分析思考,对实现的表述,以及对现有实现的改进想法,希望能成为原文的补充。第一篇文章是实现一个午夜倒数器。原文见 Day 01: Minutes to Midnight Countdown
需求
这个需求的由来,作者Chris Craft打算写一个30 Days of .NET [Windows Mobile Applications]开发的系列文章,然后想到自己是否有决心写完,每个人都自己的生活:家庭,朋友,事业,兴趣已经梦想。每个人一天都有而且只有24小时,我们可以知道的是到底还有多少时间剩下在这一天。其实很多时候,我们做事情光有想法,不付诸行动,或者行动中半途而废。作者在开始的时候也怀疑自己是否有决心把事情做完,最终的结果显示Chris Craft做到了,我希望也向Chris Craft学习,把这个系列的文章学一遍。如果你还是在Windows Mobile开发的门外徘徊,可以跟着这个系列每天学一点,想一点,不多也不少,打开Windows Mobile开发精彩之门,感受个中乐趣。
准备工作
学习这个系列,需要安装Visual Studio 2008和Windows Mobile 6 SDK。 Visual Studio 2008 Express 下载见 Visual Studio Express, Windows Mobile 6 SDK 下载于 Windows Mobile 6 Professional and Standard Software Development Kits Refresh。
实现
这些程序很简单,关键运用了Timer,DateTime和进程条(Progress Bar)。Timer是通过控件使用的,所以回调时间在属性里进行配置,如下:
图1
图2
Interval设置为100表示每100毫秒回调一次timer_Tick(object sender, EventArgs e)。
private void timer_Tick(object sender, EventArgs e)
{
TimeSpan timeSpan = DateTime.Now.Date.AddDays(1) - DateTime.Now;
labelHours.Text = string.Format("{0} of 24 hours left", timeSpan.Hours);
labelMinutes.Text = string.Format("{0} of 60 minutes left", timeSpan.Minutes);
labelSeconds.Text = string.Format("{0} of 60 seconds left", timeSpan.Seconds);
labelTotalMinutes.Text = string.Format("{0} of 1440 total minutes left", timeSpan.TotalMinutes.ToString("#.0"));
labelTotalSeconds.Text = string.Format("{0} of 86400 total seconds left", timeSpan.TotalSeconds);
progressBarTotal.Value = 86400 - (int) timeSpan.TotalSeconds;
progressBarHours.Value = 24 - timeSpan.Hours;
progressBarMinutes.Value = 60 - timeSpan.Minutes;
progressBarSeconds.Value = 60 - timeSpan.Seconds;
progressBarTotalMinutes.Value = 1440 - (int) timeSpan.TotalMinutes;
progressBarTotalSeconds.Value = 86400 - (int) timeSpan.TotalSeconds;
}
这个回调函数的算法也很简单,DateTime.Now.Date.AddDays(1)取出午夜的时间DateTime对象,然后和当前时间相减得到TimeSpan的对象timeSpan,从timeSpan的取出当前和午夜的时间差呈现到Label以及ProgressBar上。在total minutes left上,作者做了处理,因为有时候会显示小数错误"X.666666666 of 1440 total minutes left".通过字符串的格式化以后timeSpan.TotalMinutes.ToString("#.0"),显示正常。
安装文件: minutes2Midnight.cab
源代码: minuates2Midnight.zip
.NET Compact Framework, WinCE, Windows Mobile开发系列
Jake's Blog in 博客园 -- 精简开发 无线生活
相关文章推荐
- 30 Days of .NET [Windows Mobile Applications] - Day 03: GPS Compass(GPS指南针)
- 30 Days of .NET [Windows Mobile Applications] - Day 02: Bluetooth Manager(蓝牙管理器)
- 30 Days of .NET [Windows Mobile Applications] - Day 04: Mileage Tracker(里程耗油计算程序)
- 30 Days of .NET Windows Mobile Applications
- The Windows SDK team is proud to announce that the RTM release of the Microsoft Windows SDK for Windows Server 2008 and .NET Fra
- Tira Launches Network To Promote Creation Of Mobile Java Applications @ JDJ
- enbdding Fonts for wp from:http://www.jeffblankenburg.com/2010/10/24/31-days-of-windows-phone-day-24-embedding-fonts/
- How to Spend the First 10 Minutes of Your Day
- Foundations of WF: an Introduction to Windows Workflow Foundation (Expert's Voice in .Net)
- Debugging Applications for Microsoft .NET and Microsoft Windows: Notes-Prerequisites to Debugging
- .NET Internationalization: The Developer's Guide to Building Global Windows and Web Applications
- How to launch Windows applications (Notepad) / Call Batch files from a C#/.NET console application.
- Rules to Better Windows Forms Applications
- Simulate a Windows Service using ASP.NET to run scheduled jobs
- How to solve Windows system crashes in minutes
- How 30 Minutes a Day Can Increase Your Intelligence
- Debugging Applications for Microsoft .NET and Microsoft Windows: Notes-Books
- iOS每次启动都提示:Application windows are expected to have a root view controller at the end of application
- 解决error 25541 failed to open xml fileC:\Windows\Microsoft.NET\Framework64\v2.0.50727\CONFIG\machine
- 黄聪:.Net Framework Initialization Error – Unable to find a version of the runtime to run this application解决方案