博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Windows Phone 7 - DatePicker and TimePicker【转】
阅读量:6680 次
发布时间:2019-06-25

本文共 4142 字,大约阅读时间需要 13 分钟。

In this artcile we will discuss about DatePicker and TimePicker control in Window Phone 7. DatePicker and TimePicker control comes along with Silverlight Windows Phone Toolkit. DatePicker and TimePicker are important controls to select date in Windows Phone 7.

 

Download 

 

Let's write code to demonstrate how to use.

 

Step 1: Create a silverlight for Windows Phone project.

 

Step 2: Add reference of Microsoft.Phone.Controls.Toolkit.dll

 

Step 3: Add namespace of Microsoft.Phone.Controls.Toolkit in MainPage.xaml.

 

xmlns:toolkit="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone.Controls.Toolkit"

 

Step 4: Create instance of DatePicker and TimePicker in MainPage.xaml

 

<toolkit:DatePicker x:Name="datePicker" Margin="20, 0,0,0" Header="Select Date" Value="01/04/2011"/>

<toolkit:TimePicker x:Name="timePicker" Margin="20, 100,0,0" Header="Select Time" Value="20:20"/>

ValueStringFormat property of DatePicker and TimePicker is used to format the date and time.

Run the application, you will get date and time format as shown below.

Windows Phone 7 - DatePicker and TimePicker

Now apply ValueStringFormate to DatePicker and TimePicker.

<toolkit:DatePicker x:Name="datePicker" Margin="20, 0,0,0" Header="Select Date" Value="01/04/2011"ValueStringFormat="{}{0:D}"/>

<toolkit:TimePicker x:Name="timePicker" Margin="20, 100,0,0" Header="Select Time" Value="20:20"ValueStringFormat="{}{0:T}"/>

Run the application again and you will notice that date and time formatting has been applied.

Windows Phone 7 - Date and Time Format

Refer  for more options.

Step 5: There is one important eventhandler of DatePicker and TimePicker.

ValueChanged event triggers when value of DatePicker or TimePicker is changed.

Paste below code in the constructor of the MainPage.xaml.cs to attach ValueChanged event handler to DatePicker and TimePicker.

public MainPage()

{
   InitializeComponent();
   this.datePicker.ValueChanged += new EventHandler<DateTimeValueChangedEventArgs>(datePicker_ValueChanged);
   this.timePicker.ValueChanged += new EventHandler<DateTimeValueChangedEventArgs
>(timePicker_ValueChanged);
}

Alternative we can add event handler in MainPage.xaml as well like below.

 <toolkit:DatePicker x:Name="datePicker" Margin="20, 0,0,0" Header="Select Date" Value="1/4/2011"ValueChanged="datePicker_ValueChanged" />

<toolkit:TimePicker x:Name="timePicker" Margin="20, 100,0,0" Header="Select Time" Value="20:20"ValueChanged="timePicker_ValueChanged" />

Step 6: Now place below code in MainPage.xaml.cs.

void datePicker_ValueChanged(object sender, DateTimeValueChangedEventArgs e)

{
   DateTime date = (DateTime)e.NewDateTime;
   MessageBox.Show(date.ToString("d"
));
}

void timePicker_ValueChanged(object sender, DateTimeValueChangedEventArgs e)

{
   DateTime date = (DateTime)e.NewDateTime;
   MessageBox.Show(date.ToString("t"
));
}

Now run the application you will get below screen like shown. If you notice the icon in the application bar won't be displayed.

Windows Phone 7 - Date and Time Picker Application Bar

Add Toolkit.Content folder and cancel image with name ApplicationBar.Cancel.png and done image with name ApplicationBar.Check.png.

Windows Phone 7 - Application Bar Toolkit content

Now run the application again and the application bar icon will be visible now.

 

 

Windows Phone 7 - Date and Time Picker

 

On click of Select Date text box below screen will appear.

 

Windows Phone 7 - Select Date

 

On click of Done button from application bar value changed event will trigger and the message box will appear as shown below. ValueChanged event will trigger only if date is changed.

 

Windows Phone 7 - Accept Selected Date

 

On click of Select Time text box below screen will appear.

 

Windows Phone 7 - Select Time

 

On click of Done button from application bar value changed event will trigger and the message box will appear as shown below. ValueChanged event will trigger only if time is changed.

 

Windows Phone 7 - Accept Select Time

 

One can add DatePicker and TimePicker control dynamically also like below.

 

DatePicker datePicker = new DatePicker();

TimePicker timePicker = new TimePicker();

 

this.ContentPanel.Children.Add(datePicker);

this.ContentPanel.Children.Add(timePicker);

 

The ValueString property of DatePicker and TimePicker returnds the selected value of date and time as string.

 

MessageBox.Show(datePicker.ValueString);

 

This ends the article of DatePicker and TimePicker in Windows Phone 7.

原文连接:http://www.dotnetspeaks.com/DisplayArticle.aspx?ID=172

转载于:https://www.cnblogs.com/ouyating/archive/2012/05/07/2487749.html

你可能感兴趣的文章
kickstart部署centos6.2 x86_64
查看>>
salt 的用户管理
查看>>
我封装的全文检索之solr篇
查看>>
NFC的第一次接触
查看>>
RHEL7 Connection closed by foreign host.
查看>>
Nodejs开发框架之Loopback介绍
查看>>
微信小程序下拉刷新使用整理
查看>>
ubuntu12.04禁用客人会话
查看>>
我的友情链接
查看>>
JVM垃圾收集器与内存分配策略
查看>>
分析Linux 文件系统访问控制列表
查看>>
Confluence WIKI 安装、破解及添加汉化包(Windows)
查看>>
一起入门Citrix_XenDesktop7系列 二-- 跟着图片通过XenDesktop7交付(发布)应用与共享桌面...
查看>>
MyBatis学习手记(一)MaBatis入门
查看>>
SCTF-2014 writeup(部分)
查看>>
Elasticsearch 连接查询
查看>>
Retrofit入门
查看>>
设置Exchange 通讯组接收外部组织邮件
查看>>
观点:正在消逝的运维江湖
查看>>
istio 监控,遥测 (理论)
查看>>