Windows Phone 7平台开发日历应用程序教程

移动开发
Windows Phone 7是微软发布的手机操作系统,与诺基亚合作使它成为备受关注的平台。今天我们将给大家介绍如何在Windows Phone 7平台上开发日历应用程序,该日历程序实现的功能包括显示当前的日期,并可通过上下按钮来查看不同月份的日期。

在Windows Phone 7上实现一个日历的程序有很多种的方式,下面将用一种很简单的方法来实现一个日历的应用程序。日历主体是用一个WrapPanel面板加上多了Button控件来实现的,每个日期用一个Button来表示。WrapPanel根据其中Button元素的尺寸和其自身可能的大小自动地把其中的Button元素排列到下一行或下一列。该日历程序实现的功能包括显示当前的日期,可以通过上下按钮来查看不同月份的日期。

  1. <phone:PhoneApplicationPage     
  2.  
  3.     x:Class="CalendarControl.MainPage"   
  4.  
  5.     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"   
  6.  
  7.     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"   
  8.  
  9.     xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone"   
  10.  
  11.     xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone"   
  12.  
  13.     xmlns:d="http://schemas.microsoft.com/expression/blend/2008"   
  14.  
  15.     xmlns:toolkit="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone.Controls.Toolkit"   
  16.  
  17.     xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"   
  18.  
  19.     mc:Ignorable="d" d:DesignWidth="480" d:DesignHeight="768"   
  20.  
  21.     FontFamily="{StaticResource PhoneFontFamilyNormal}"   
  22.  
  23.     FontSize="{StaticResource PhoneFontSizeNormal}"   
  24.  
  25.     Foreground="{StaticResource PhoneForegroundBrush}"   
  26.  
  27.     SupportedOrientations="Portrait" Orientation="Portrait"   
  28.  
  29.     shell:SystemTray.IsVisible="True">    
  30.  
  31.      
  32.  
  33.     <phone:PhoneApplicationPage.Resources>    
  34.  
  35.         <ResourceDictionary>    
  36.  
  37.             <ResourceDictionary.MergedDictionaries>    
  38.  
  39.                 <ResourceDictionary Source="style.xaml"/>    
  40.  
  41.             </ResourceDictionary.MergedDictionaries>    
  42.  
  43.         </ResourceDictionary>    
  44.  
  45.     </phone:PhoneApplicationPage.Resources>    
  46.  
  47.          
  48.  
  49.     <!--LayoutRoot is the root grid where all page content is placed-->    
  50.  
  51.     <Grid x:Name="LayoutRoot" Background="Firebrick">    
  52.  
  53.         <Grid.RowDefinitions>    
  54.  
  55.             <RowDefinition Height="Auto"/>    
  56.  
  57.             <RowDefinition Height="*"/>    
  58.  
  59.         </Grid.RowDefinitions>    
  60.  
  61.      
  62.  
  63.         <!--TitlePanel contains the name of the application and page title-->    
  64.  
  65.         <StackPanel x:Name="TitlePanel" Grid.Row="0" Margin="12,17,0,28">    
  66.  
  67.             <TextBlock x:Name="PageTitle" Text="日历" Margin="9,-7,0,0" Style="{StaticResource PhoneTextTitle1Style}" Height="90" />    
  68.  
  69.         </StackPanel>    
  70.  
  71.      
  72.  
  73.         <!--ContentPanel - place additional content here-->    
  74.  
  75.         <Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">    
  76.  
  77.             <Grid>    
  78.  
  79.                 <Grid>    
  80.  
  81.                     <Button x:Name="BackBtn" VerticalAlignment="Top" HorizontalAlignment="Left" Style="{StaticResource RoundButton}" Height="72" Width="72" Margin="27,1,0,0" Click="OnChangeMonth" BorderBrush="White">    
  82.  
  83.                         <Image Source="/CalendarControl;component/Images/appbar.back.png" Height="42" Width="42" VerticalAlignment="Top" HorizontalAlignment="Left" />    
  84.  
  85.                     </Button>    
  86.  
  87.                     <TextBlock x:Name="MonthYear" Text="November 2010" Style="{StaticResource PhoneTextLargeStyle}" Margin="101,14,124,0" HorizontalAlignment="Center" VerticalAlignment="Top" Width="231" TextAlignment="Center" Foreground="White"/>    
  88.  
  89.                     <Button x:Name="NextBtn" VerticalAlignment="Top" HorizontalAlignment="Right" Style="{StaticResource RoundButton}" Height="72" Width="74" Margin="0,0,45,0" Click="OnChangeMonth" Foreground="White" BorderBrush="White">    
  90.  
  91.                         <Image Source="/CalendarControl;component/Images/appbar.next.png" Height="42" Width="42" VerticalAlignment="Top" HorizontalAlignment="Right" />    
  92.  
  93.                     </Button>    
  94.  
  95.                 </Grid>    
  96.  
  97.                 <ListBox x:Name="CalendarListBox" Margin="1,78,0,70" Height="459">    
  98.  
  99.                     <ListBoxItem Margin="12,0,0,0">    
  100.  
  101.                         <toolkit:WrapPanel x:Name="CalendarWrapPanel" HorizontalAlignment="Left" VerticalAlignment="Top"/>    
  102.  
  103.                     </ListBoxItem>    
  104.  
  105.                 </ListBox>    
  106.  
  107.             </Grid>    
  108.  
  109.      
  110.  
  111.         </Grid>    
  112.  
  113.     </Grid>    
  114.  
  115. </phone:PhoneApplicationPage>   
  116.  
  117. view sourceprint?using System;    
  118.  
  119. using System.Collections.Generic;    
  120.  
  121. using System.Linq;    
  122.  
  123. using System.Net;    
  124.  
  125. using System.Windows;    
  126.  
  127. using System.Windows.Controls;    
  128.  
  129. using System.Windows.Documents;    
  130.  
  131. using System.Windows.Input;    
  132.  
  133. using System.Windows.Media;    
  134.  
  135. using System.Windows.Media.Animation;    
  136.  
  137. using System.Windows.Shapes;    
  138.  
  139. using Microsoft.Phone.Controls;    
  140.  
  141. using Microsoft.Phone.Shell;    
  142.  
  143.      
  144.  
  145. namespace CalendarControl    
  146.  
  147. {    
  148.  
  149.     public partial class MainPage : PhoneApplicationPage    
  150.  
  151.     {    
  152.  
  153.         DateTime? _entryDate = DateTime.Now;//"?"加上之后表示可以有空值(null) Nullable<T>    
  154.  
  155.      
  156.  
  157.         public MainPage()    
  158.  
  159.         {    
  160.  
  161.             InitializeComponent();    
  162.  
  163.             InitializeCalendar(_entryDate.Value);//第一次进入日历程序  初始化当前日期的显示    
  164.  
  165.         }    
  166.  
  167.      
  168.  
  169.         /// <summary>    
  170.  
  171.         ///月份前进后退事件      
  172.  
  173.         /// </summary>    
  174.  
  175.         /// <param name="sender"></param>    
  176.  
  177.         /// <param name="e"></param>    
  178.  
  179.         private void OnChangeMonth(object sender, RoutedEventArgs e)    
  180.  
  181.         {    
  182.  
  183.             if (((Button)sender).Name == "NextBtn")//如果是点击下一个月的按钮    
  184.  
  185.                 _entryDate_entryDate = _entryDate.Value.AddMonths(1);    
  186.  
  187.             else   
  188.  
  189.                 _entryDate_entryDate = _entryDate.Value.AddMonths(-1);    
  190.  
  191.      
  192.  
  193.             CalendarListBox.Visibility = Visibility.Collapsed;    
  194.  
  195.      
  196.  
  197.             //初始化该日期的显示    
  198.  
  199.             InitializeCalendar(_entryDate.Value);    
  200.  
  201.         }    
  202.  
  203.      
  204.  
  205.         /// <summary>    
  206.  
  207.         ///  初始化日历不同月份的日期     
  208.  
  209.         /// </summary>    
  210.  
  211.         /// <param name="entryDate"></param>    
  212.  
  213.         protected void InitializeCalendar(DateTime entryDate)    
  214.  
  215.         {    
  216.  
  217.             MonthYear.Text = String.Format("{0:yyyy年 MM月 }", _entryDate.Value);    
  218.  
  219.      
  220.  
  221.             DateTime todaysDate = DateTime.Now;    
  222.  
  223.      
  224.  
  225.             int numDays = DateTime.DaysInMonth(entryDate.Year, entryDate.Month);//获取显示的月份的天数    
  226.  
  227.      
  228.  
  229.             int count = CalendarWrapPanel.Children.Count;//CalendarWrapPanel面板中检查按钮的数量    
  230.  
  231.             if (count > numDays)    
  232.  
  233.             {//从最后减去多余的日期的按钮   日期用的是按钮控件    
  234.  
  235.                 for (int i = 1; i <= count - numDays; i++)    
  236.  
  237.                     CalendarWrapPanel.Children.RemoveAt(count - i);    
  238.  
  239.             }    
  240.  
  241.             else   
  242.  
  243.             {//从最后加上缺少的日期的按钮    
  244.  
  245.                 int start = count + 1;    
  246.  
  247.                 for (int i = start; i <= numDays; i++)    
  248.  
  249.                 {    
  250.  
  251.                     Border border = new Border();    
  252.  
  253.                     border.Background = new SolidColorBrush(Color.FromArgb(255, 103, 183, 212));    
  254.  
  255.                     border.Margin = new Thickness(0, 0, 5, 5);    
  256.  
  257.                     border.Width = 80;    
  258.  
  259.                     border.Height = 80;    
  260.  
  261.                     border.CornerRadius = new CornerRadius(20);    
  262.  
  263.      
  264.  
  265.                     Button btn = new Button();    
  266.  
  267.                     btn.Name = "Day" + i;    
  268.  
  269.                     btn.Content = i.ToString();    
  270.  
  271.                     btn.BorderBrush = new SolidColorBrush(Colors.Transparent);    
  272.  
  273.                     btn.Width = 75;    
  274.  
  275.                     btn.Height = 75;    
  276.  
  277.                     btn.FontSize = 25;    
  278.  
  279.                     border.Child = btn;//Button放进Border里面    
  280.  
  281.                     btn.Style = this.Resources["HasDataButtonStyle"] as Style;    
  282.  
  283.      
  284.  
  285.                     CalendarWrapPanel.Children.Add(border);//将按钮添加到面板中    
  286.  
  287.                 }    
  288.  
  289.             }    
  290.  
  291.      
  292.  
  293.             for (int i = 0; i < numDays; i++)    
  294.  
  295.             {    
  296.  
  297.                 Border border = (Border)CalendarWrapPanel.Children[i];    
  298.  
  299.                 if (border != null)    
  300.  
  301.                 {    
  302.  
  303.                     Button btn = (Button)border.Child;    
  304.  
  305.      
  306.  
  307.                     DateTime currDate = new DateTime(entryDate.Year, entryDate.Month, i + 1);    
  308.  
  309.                     //如果是日期是今天则设置日期的按钮为橙色    
  310.  
  311.                     if (currDate.Date.CompareTo(DateTime.Now.Date) == 0)    
  312.  
  313.                     {    
  314.  
  315.                         border.Background = new SolidColorBrush(Color.FromArgb(255, 255, 165, 0));    
  316.  
  317.                         btn.Style = this.Resources["TodayHasDataButtonStyle"] as Style;    
  318.  
  319.                        // isToday = true;    
  320.  
  321.                     }    
  322.  
  323.                     else   
  324.  
  325.                     {    
  326.  
  327.                         border.Background = new SolidColorBrush(Color.FromArgb(255, 103, 183, 212));    
  328.  
  329.                     }    
  330.  
  331.                 }    
  332.  
  333.             }    
  334.  
  335.             CalendarWrapPanel.UpdateLayout();//更新CalendarWrapPanel的显示     
  336.  
  337.             CalendarListBox.Visibility = Visibility.Visible;//设置为可见    
  338.  
  339.      
  340.  
  341.         }    
  342.  
  343.     }    
  344.  
  345. }  

通过以上的代码,你将实现下图所示的日历应用。

日历 

【编辑推荐】

  1. 绑定在Windows Phone 7的静态类
  2. 不编程也开发 无代码开发Windows Phone 7应用工具
  3. 使用IronRuby开发Windows Phone 7应用程序
  4. Windows Phone 7应用程序统计 65%是付费下载
  5. Windows Phone 7设计评测报告

 

责任编辑:佚名 来源: 博客园
相关推荐

2010-11-03 15:10:04

SilverlightSilverlightWindows Pho

2011-03-21 09:05:40

IronRubyWindows Pho

2010-12-01 09:01:31

独立存储Windows Pho

2011-04-01 13:20:40

Windows Pho应用程序

2012-05-17 14:15:10

Windows Pho

2010-10-29 14:08:01

.NETWindows PhoiPhone

2013-07-30 13:38:27

Windows PhoWindows Pho

2013-07-31 14:50:32

Windows PhoWP应用程序生命周期

2012-05-28 15:37:20

WP程序生命周期

2011-10-25 10:24:03

Windows Pho

2011-12-06 10:45:16

云计算应用Windows Pho

2012-06-19 10:22:16

Windows Pho

2013-07-30 11:18:37

Windows PhoWindows Pho

2020-09-24 10:54:10

谷歌Flutter开发

2010-08-27 09:36:57

Windows Pho

2012-12-17 15:31:00

Windows PhoWindows PhoWindows Pho

2011-06-07 11:35:38

Windows Pho

2012-08-16 10:35:50

Windows Pho

2011-12-03 21:03:14

Windows Pho

2011-06-08 10:01:36

Windows Pho 应用程序
点赞
收藏

51CTO技术栈公众号