WP8日曆(含農曆)APP

本文轉自:http://blog.csdn.net/yimiyuangguang/article/details/24179295

WP8日曆(含農曆)APP

 WP8日曆(含農曆)APP UI XAML(部分)

  1. <phone:PhoneApplicationPage xmlns:CustomControl="clr-namespace:CalendarApp.CustomControl"    
  2.     xmlns:Controls="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone.Controls.Toolkit"  
  3.     x:Class="CalendarApp.MainPage"  
  4.     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"  
  5.     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"  
  6.     xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone"  
  7.     xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone"  
  8.     xmlns:d="http://schemas.microsoft.com/expression/blend/2008"  
  9.     xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"  
  10.     mc:Ignorable="d"  
  11.     FontFamily="{StaticResource PhoneFontFamilyNormal}"  
  12.     FontSize="{StaticResource PhoneFontSizeNormal}"  
  13.     Foreground="{StaticResource PhoneForegroundBrush}"  
  14.     SupportedOrientations="Portrait" Orientation="Portrait"  
  15.     shell:SystemTray.IsVisible="True"  
  16.     Loaded="PhoneApplicationPage_Loaded">  
  17.   
  18.   
  19.     <Grid x:Name="LayoutRoot" Background="Transparent">  
  20.         <StackPanel x:Name="SPCalendar" Margin="0" Orientation="Vertical">  
  21.             <TextBlock Name="txtHead" Text="" Style="{StaticResource titleCss}" />  
  22.   
  23.             <!-- 日曆頭部 -->  
  24.             <StackPanel Orientation="Vertical">  
  25.                 <!-- 日曆按鈕 -->  
  26.                 <StackPanel Orientation="Horizontal" HorizontalAlignment="Center">  
  27.                     <Image Source="/Images/left.png" ManipulationStarted="Image_ManipulationStarted" />  
  28.                     <StackPanel Orientation="Horizontal" ManipulationStarted="StackPanel_ManipulationStarted">  
  29.                         <TextBlock Name="txtYear" Text="2014" Style="{StaticResource CHeadCss}" Width="110" />  
  30.                         <TextBlock Text="年" Style="{StaticResource CHeadCss}" />  
  31.   
  32.                         <TextBlock Name="txtMonth" Text="04" Style="{StaticResource CHeadCss}" Width="55" />  
  33.                         <TextBlock Text="月" Style="{StaticResource CHeadCss}" />  
  34.                     </StackPanel>  
  35.                     <Image Source="/Images/right.png" ManipulationStarted="Image_ManipulationStarted" />  
  36.                 </StackPanel>  
  37.   
  38.                 <!-- 日曆標題 -->  
  39.                 <StackPanel Orientation="Horizontal">  
  40.                     <TextBlock Text="日" Style="{StaticResource CTitlerGreen}" />  
  41.                     <TextBlock Text="一" Style="{StaticResource CTitleCss}" />  
  42.                     <TextBlock Text="二" Style="{StaticResource CTitleCss}" />  
  43.                     <TextBlock Text="三" Style="{StaticResource CTitleCss}" />  
  44.                     <TextBlock Text="四" Style="{StaticResource CTitleCss}" />  
  45.                     <TextBlock Text="五" Style="{StaticResource CTitleCss}" />  
  46.                     <TextBlock Text="六" Style="{StaticResource CTitlerGreen}" />  
  47.                 </StackPanel>  
  48.             </StackPanel>  
  49.   
  50.             <!-- 日曆內容 -->  
  51.             <Grid Name="gCalendar"  Background="Gray" Width="480" Height="615">  
  52.                 <Grid.RowDefinitions>  
  53.                     <RowDefinition Height="*" />  
  54.                     <RowDefinition Height="*" />  
  55.                     <RowDefinition Height="*" />  
  56.                     <RowDefinition Height="*" />  
  57.                     <RowDefinition Height="*" />  
  58.                     <RowDefinition Height="*" />  
  59.                 </Grid.RowDefinitions>  
  60.   
  61.                 <Grid.ColumnDefinitions>  
  62.                     <ColumnDefinition Width="*" />  
  63.                     <ColumnDefinition Width="*" />  
  64.                     <ColumnDefinition Width="*" />  
  65.                     <ColumnDefinition Width="*" />  
  66.                     <ColumnDefinition Width="*" />  
  67.                     <ColumnDefinition Width="*" />  
  68.                     <ColumnDefinition Width="*" />  
  69.                 </Grid.ColumnDefinitions>  
  70.             </Grid>  
  71.         </StackPanel>  
  72.     </Grid>  
  73. </phone:PhoneApplicationPage>  
後臺CS參考代碼(部分):
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Net;  
  5. using System.Windows;  
  6. using System.Windows.Controls;  
  7. using System.Windows.Navigation;  
  8. using Microsoft.Phone.Controls;  
  9. using Microsoft.Phone.Shell;  
  10. using CalendarApp.Resources;  
  11. using System.Collections.ObjectModel;  
  12. using Model;  
  13. using ChineseCalendar;  
  14. using CalendarApp.PageCode;  
  15. using CalendarApp.CustomControl;  
  16. using Microsoft.Phone.Controls.Primitives;  
  17.   
  18.   
  19. namespace CalendarApp  
  20. {  
  21.     public partial class MainPage : PhoneApplicationPage  
  22.     {  
  23.         #region 全局變量  
  24.         /// <summary>  
  25.         /// BGDateWeek 當月第一天星期數  
  26.         /// </summary>  
  27.         int BGDateWeek = new int();  
  28.   
  29.         /// <summary>  
  30.         /// ObservableCollection<CalendarModel> myCalendar  
  31.         /// </summary>  
  32.         ObservableCollection<CalendarModel> myCalendar = new ObservableCollection<CalendarModel> { };  
  33.         #endregion  
  34.   
  35.         public MainPage()  
  36.         {  
  37.             InitializeComponent();  
  38.         }  
  39.   
  40.         private void PhoneApplicationPage_Loaded(object sender, RoutedEventArgs e)  
  41.         {  
  42.             //當前日期數據  
  43.             DateTime dt = DateTime.Now;  
  44.             if (this.NavigationContext.QueryString.Count > 0 && this.NavigationContext.QueryString["t"] != null)  
  45.                 if (!DateTime.TryParse(this.NavigationContext.QueryString["t"].ToString(), out dt)) dt = DateTime.Now;  
  46.   
  47.             this.txtYear.Text = dt.ToString("yyyy");  
  48.             this.txtMonth.Text = dt.ToString("MM");  
  49.   
  50.             //加載當月日期數據  
  51.             GetMonthDate(dt.ToString("yyyy-MM-dd"));  
  52.         }  
  53.  
  54.  
  55.         #region 根據指定時間獲取日曆數據  
  56.         /// <summary>  
  57.         /// 根據指定時間獲取日曆數據  
  58.         /// </summary>  
  59.         /// <param name="dtMonth">時間 yyyy-MM</param>  
  60.         public void GetMonthDate(String dtMonth)  
  61.         {  
  62.             //初始化參數  
  63.             DateTime dt = DateTime.Now;  
  64.             if (!DateTime.TryParse(dtMonth, out dt)) dt = DateTime.Now;  
  65.   
  66.             //獲取選中日期的第一天、最後一天,以及第一天星期幾  
  67.             DateTime BGDate, EDDate, GLDate;  
  68.             BGDate = new DateTime(dt.Year, dt.Month, 1);  
  69.             EDDate = BGDate.AddMonths(1).AddDays(-1);  
  70.             getWeekIndex(BGDate.DayOfWeek.ToString());  
  71.   
  72.             //自定義控件  
  73.             CustomControl.CustomDate cd = null;  
  74.             CustomControl.CustomDateGreen cdGreen = null;  
  75.   
  76.             //初始化變量  
  77.             CalendarModel item = null;  
  78.   
  79.             //清空  
  80.             this.gCalendar.Children.Clear();  
  81.   
  82.             //循環添加數據  
  83.             int row = 0, col = 0;  
  84.             for (int i = 0, len = (EDDate - BGDate).Days; i <= len; i++)  
  85.             {  
  86.                 //設定行和列  
  87.                 if (i == 0)  
  88.                     col = BGDateWeek;  
  89.                 else col++;  
  90.   
  91.                 if (col > 6)  
  92.                 {  
  93.                     col = 0;  
  94.                     row++;  
  95.                 }  
  96.   
  97.                 GLDate = BGDate.AddDays(i);  
  98.                 item = new CalendarModel()  
  99.                 {  
  100.                     GLDay = GLDate.ToString("dd"),  
  101.                     GLDate = GLDate.ToString("yyyy-MM-dd"),  
  102.   
  103.                     NLDay = ChineseCalendarDate.GetChineseDate(GLDate),  
  104.                     NLDate = ChineseCalendarDate.GetChineseDateTime(GLDate)  
  105.                 };  
  106.   
  107.                 //年信息  
  108.                 String year = item.NLDate.Substring(0, item.NLDate.IndexOf("年") + 1);  
  109.                 this.txtHead.Text = "農曆" + year;  
  110.   
  111.                 //綁定數據  
  112.                 if (col == 0 || col == 6)  
  113.                 {  
  114.                     cdGreen = new CustomDateGreen();  
  115.                     cdGreen.DataContext = item;  
  116.                     Grid.SetColumn(cdGreen, col);  
  117.                     Grid.SetRow(cdGreen, row);  
  118.                     this.gCalendar.Children.Add(cdGreen);  
  119.                 }  
  120.                 else  
  121.                 {  
  122.                     cd = new CustomDate();  
  123.                     cd.DataContext = item;  
  124.                     Grid.SetColumn(cd, col);  
  125.                     Grid.SetRow(cd, row);  
  126.                     this.gCalendar.Children.Add(cd);  
  127.                 }  
  128.             }  
  129.         }  
  130.         #endregion  
  131.  
  132.  
  133.         #region 獲取星期索引  
  134.         /// <summary>  
  135.         /// 獲取星期索引  
  136.         /// </summary>  
  137.         /// <param name="week"></param>  
  138.         private void getWeekIndex(String week)  
  139.         {  
  140.             switch (week)  
  141.             {  
  142.                 case "Monday":   //週一  
  143.                     BGDateWeek = 1;  
  144.                     break;  
  145.                 case "Tuesday":  //週二  
  146.                     BGDateWeek = 2;  
  147.                     break;  
  148.                 case "Wednesday"://週三  
  149.                     BGDateWeek = 3;  
  150.                     break;  
  151.                 case "Thursday"://週四  
  152.                     BGDateWeek = 4;  
  153.                     break;  
  154.                 case "Friday":  //週五  
  155.                     BGDateWeek = 5;  
  156.                     break;  
  157.                 case "Saturday"://週六  
  158.                     BGDateWeek = 6;  
  159.                     break;  
  160.                 case "Sunday":  //週末  
  161.                     BGDateWeek = 0;  
  162.                     break;  
  163.             }  
  164.         }  
  165.         #endregion  
  166.  
  167.  
  168.         #region 時間選擇  
  169.         /// <summary>  
  170.         /// 時間選擇  
  171.         /// </summary>  
  172.         /// <param name="sender"></param>  
  173.         /// <param name="e"></param>  
  174.         private void Image_ManipulationStarted(object sender, System.Windows.Input.ManipulationStartedEventArgs e)  
  175.         {  
  176.             Image image = (Image)sender;  
  177.             if (image != null)  
  178.             {  
  179.                 int y = int.Parse(this.txtYear.Text);  
  180.                 int m = int.Parse(this.txtMonth.Text);  
  181.   
  182.                 if (((System.Windows.Media.Imaging.BitmapImage)(image.Source)).UriSource.ToString().Contains("left.png"))  
  183.                 {  
  184.                     //時間遞減  
  185.                     if (m - 1 <= 0)  
  186.                     {  
  187.                         //前一年  
  188.                         this.txtMonth.Text = "12";  
  189.                         this.txtYear.Text = (y - 1).ToString().Trim();  
  190.                     }  
  191.                     else  
  192.                     {  
  193.                         //當年,月遞減  
  194.                         m--;  
  195.                         if (m == 0) m = 1;  
  196.                         this.txtMonth.Text = m >= 10 ? m.ToString() : "0" + m.ToString();  
  197.                     }  
  198.                 }  
  199.                 else  
  200.                 {  
  201.                     //時間遞增  
  202.                     if (m + 1 > 12)  
  203.                     {  
  204.                         //後一年  
  205.                         this.txtMonth.Text = "01";  
  206.                         this.txtYear.Text = (y + 1).ToString().Trim();  
  207.                     }  
  208.                     else  
  209.                     {  
  210.                         //當年,月遞增  
  211.                         m++;  
  212.                         this.txtMonth.Text = m >= 10 ? m.ToString() : "0" + m.ToString();  
  213.                     }  
  214.                 }  
  215.   
  216.                 //獲取新時間  
  217.                 GetNewDate();  
  218.             }  
  219.         }  
  220.         #endregion  
  221.  
  222.  
  223.         #region 對應時間選擇  
  224.         /// <summary>  
  225.         /// 對應時間選擇  
  226.         /// </summary>  
  227.         /// <param name="sender"></param>  
  228.         /// <param name="e"></param>  
  229.         private void StackPanel_ManipulationStarted(object sender, System.Windows.Input.ManipulationStartedEventArgs e)  
  230.         {  
  231.             String time = this.txtYear.Text + "-" + this.txtMonth.Text;  
  232.             this.NavigationService.Navigate(new Uri("/CustomControl/CustomDatePicker.xaml?t=" + time, UriKind.Relative));  
  233.         }  
  234.         #endregion  
  235.  
  236.  
  237.         #region 獲取新時間  
  238.         /// <summary>  
  239.         /// 獲取新時間  
  240.         /// </summary>  
  241.         public void GetNewDate()  
  242.         {  
  243.             //當前日期數據  
  244.             DateTime dt = DateTime.Parse(this.txtYear.Text.Trim() + "-" + this.txtMonth.Text.Trim());  
  245.   
  246.             this.txtYear.Text = dt.ToString("yyyy");  
  247.             this.txtMonth.Text = dt.ToString("MM");  
  248.   
  249.             //加載當月日期數據  
  250.             GetMonthDate(dt.ToString("yyyy-MM-dd"));  
  251.         }  
  252.         #endregion  
  253.     }  
  254. }  


備註:由於在本實例中無法 使用 ChineseLunisolarCalendar 類 無法獲取 中國農曆的準確數據
      案例中的農曆數據 有誤,如有能人幫助解決。
      將不勝感激!
      自定義 類 根據公曆獲取 農曆日期數據: http://blog.csdn.net/yimiyuangguang/article/details/24301933


實例下載:

http://download.csdn.net/detail/yimiyuangguang/7221707


效果圖:



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章