Windows Phone 7平台开发日历应用程序教程
创始人
2024-07-25 11:01:33
0

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

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

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

日历 

【编辑推荐】

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

 

相关内容

热门资讯

如何允许远程连接到MySQL数... [[277004]]【51CTO.com快译】默认情况下,MySQL服务器仅侦听来自localhos...
如何利用交换机和端口设置来管理... 在网络管理中,总是有些人让管理员头疼。下面我们就将介绍一下一个网管员利用交换机以及端口设置等来进行D...
施耐德电气数据中心整体解决方案... 近日,全球能效管理专家施耐德电气正式启动大型体验活动“能效中国行——2012卡车巡展”,作为该活动的...
20个非常棒的扁平设计免费资源 Apple设备的平面图标PSD免费平板UI 平板UI套件24平图标Freen平板UI套件PSD径向平...
Windows恶意软件20年“... 在Windows的早期年代,病毒游走于系统之间,偶尔删除文件(但被删除的文件几乎都是可恢复的),并弹...
德国电信门户网站可实时显示全球... 德国电信周三推出一个门户网站,直观地实时提供其安装在全球各地的传感器网络检测到的网络攻击状况。该网站...
为啥国人偏爱 Mybatis,... 关于 SQL 和 ORM 的争论,永远都不会终止,我也一直在思考这个问题。昨天又跟群里的小伙伴进行...
《非诚勿扰》红人闫凤娇被曝厕所... 【51CTO.com 综合消息360安全专家提醒说,“闫凤娇”、“非诚勿扰”已经被黑客盯上成为了“木...