|
在Windows Phone 7上实现一个日历的程序有很多种的方式,下面将用一种很简单的方法来实现一个日历的应用程序。日历主体是用一个WrapPanel面板加上多了Button控件来实现的,每个日期用一个Button来表示。WrapPanel根据其中Button元素的尺寸和其自身可能的大小自动地把其中的Button元素排列到下一行或下一列。该日历程序实现的功能包括显示当前的日期,可以通过上下按钮来查看不同月份的日期。
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;
using Microsoft.Phone.Shell;
namespace CalendarControl
{
public partial class MainPage : PhoneApplicationPage
{
DateTime? _entryDate = DateTime.Now;//"?"加上之后表示可以有空值(null) Nullable
public MainPage()
{
InitializeComponent();
InitializeCalendar(_entryDate.Value);//第一次进入日历程序 初始化当前日期的显示
}
///
///月份前进后退事件
///
///
///
private void OnChangeMonth(object sender, RoutedEventArgs e)
{
if (((Button)sender).Name == "NextBtn")//如果是点击下一个月的按钮
_entryDate = _entryDate.Value.AddMonths(1);
else
_entryDate = _entryDate.Value.AddMonths(-1);
CalendarListBox.Visibility = Visibility.Collapsed;
//初始化该日期的显示
InitializeCalendar(_entryDate.Value);
}
///
/// 初始化日历不同月份的日期
///
///
protected void InitializeCalendar(DateTime entryDate)
{
MonthYear.Text = String.Format("{0:yyyy年 MM月 }", _entryDate.Value);
DateTime todaysDate = DateTime.Now;
int numDays = DateTime.DaysInMonth(entryDate.Year, entryDate.Month);//获取显示的月份的天数
int count = CalendarWrapPanel.Children.Count;//CalendarWrapPanel面板中检查按钮的数量
if (count > numDays)
{//从最后减去多余的日期的按钮 日期用的是按钮控件
for (int i = 1; i |
|
|
|
|
|
|