初学WP7开发——成长天数计算器及DatePicker控件的简单使用方法

刚开始学WP7开发,刚接触C#和Silverlight,做了一个简单的计算器,计算今天是你生命中的多少天。

现建立新项目。

我使用了一个Silverlight的DatePicker控件,所以要在Reference中引用Microsoft.Phone.Controls.Toolkit

在xaml的<phone:PhoneApplicationPage 后面插入

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

以下是xaml文件的代码。

 1 <phone:PhoneApplicationPage 
2 x:Class="DayOfMyLife.MainPage"
3 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
4 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
5 xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone"
6 xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone"
7 xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
8 xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
9 xmlns:toolkit="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone.Controls.Toolkit"
10 mc:Ignorable="d" d:DesignWidth="480" d:DesignHeight="768"
11 FontFamily="{StaticResource PhoneFontFamilyNormal}"
12 FontSize="{StaticResource PhoneFontSizeNormal}"
13 Foreground="{StaticResource PhoneForegroundBrush}"
14 SupportedOrientations="Portrait" Orientation="Portrait"
15 shell:SystemTray.IsVisible="True">
16
17 <!--LayoutRoot is the root grid where all page content is placed-->
18 <Grid x:Name="LayoutRoot" Background="Transparent">
19 <Grid.RowDefinitions>
20 <RowDefinition Height="Auto"/>
21 <RowDefinition Height="*"/>
22 </Grid.RowDefinitions>
23
24 <!--TitlePanel contains the name of the application and page title-->
25 <StackPanel x:Name="TitlePanel" Grid.Row="0" Margin="12,17,0,28">
26 <TextBlock x:Name="PageTitle" Text="生命计算器" Margin="9,-7,0,0" Style="{StaticResource PhoneTextTitle1Style}"/>
27 </StackPanel>
28
29 <!--ContentPanel - place additional content here-->
30 <Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
31 <toolkit:DatePicker Margin="0,42,0,-42" Name="DP" Value="1/1/1990"/>
32 <TextBlock Height="34" HorizontalAlignment="Left" Margin="9,115,0,0" Name="textBlock4" Text="" VerticalAlignment="Top" />
33 <Button Content="计算" Height="72" HorizontalAlignment="Left" Margin="290,115,0,0" Name="button1" VerticalAlignment="Top" Width="160" Click="button1_Click" />
34 <TextBlock Height="45" HorizontalAlignment="Left" Margin="9,195,0,0" Name="textBlock5" Text="结果:" VerticalAlignment="Top" Width="72" FontSize="25.333"/>
35 <TextBlock Height="130" HorizontalAlignment="Left" Margin="9,246,0,0" Name="result" Text="" VerticalAlignment="Top" Width="441" FontSize="25.333" TextWrapping="Wrap" />
36 </Grid>
37 </Grid>
38
39 </phone:PhoneApplicationPage>

 

以下是mainPage.xaml.cs文件的代码

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;

namespace DayOfMyLife
{
public partial class MainPage : PhoneApplicationPage
{
// Constructor
public MainPage()
{
InitializeComponent();
}

private void button1_Click(object sender, RoutedEventArgs e)
{
string myBrithday = DP.Value.ToString();
DateTime Brithday = DateTime.Parse(myBrithday);
DateTime d1 = DateTime.Now;
var ts = d1 - Brithday;
int day1 = ts.Days;
result.Text = "今天是你生命中的" + day1.ToString() + "天 #^_^#";
}
}
}

 

现在你运行程序的话,ApplicationBar中的按钮没有图片。如上图。

现在在你的根目录下新建一个“Toolkit.Content”的文件夹。

在C:\Program Files (x86)\Microsoft SDKs\Windows Phone\v7.1\Icons\dark(64位系统路径)

C:\Program Files \Microsoft SDKs\Windows Phone\v7.1\Icons\dark(32位系统路径)

中选取你想要的图片,添加到“Toolkit.Content”的文件夹中,

并命名为分别命名为“ApplicationBar.Cancel.png”和“ApplicationBar.Check.png”图片的属性的“Build Action"一定要改为"Content",否则图片无法显示。

这样你的程序就可以运行了。一下是成功运行的截图。

 

下面是关于DatePicker控件的使用方法。

先在Reference中引用Microsoft.Phone.Controls.Toolkit

在xaml的<phone:PhoneApplicationPage 后面插入

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

然后再xaml文件中的第三个<Gird>标签中间插入

 <toolkit:DatePicker Name="DP" Value="1/1/1990"/>

上面这段代码定义了一个名字为DP,初始值为1990年1月1日的DatePicker控件。

在xam.cs文件中的DP.Value.Tostring()是将DatePicker中设定的值转为string型。

以上仅仅是DatePicker控件的一种简单属性。不过用这个控件输入日期是非常酷的。

对应的还有一个TimePicker的控件,是滑动输入时间的。

 

我是一个初学者,以上代码有什么错误,或者您有更好地方法帮助我改进,欢迎您告诉我。

posted @ 2011-11-12 19:58  CimiChen  阅读(595)  评论(0编辑  收藏  举报