1. 程式人生 > >WP8日歷(含農歷)APP

WP8日歷(含農歷)APP

bmc inpu adc alignment 日歷 data- substr started rgb

WP8日歷(含農歷)APP

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

<phone:PhoneApplicationPage xmlns:CustomControl="clr-namespace:CalendarApp.CustomControl"  
    xmlns:Controls="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone.Controls.Toolkit"
    x:Class="CalendarApp.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone"
    xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d"
    FontFamily="{StaticResource PhoneFontFamilyNormal}"
    FontSize="{StaticResource PhoneFontSizeNormal}"
    Foreground="{StaticResource PhoneForegroundBrush}"
    SupportedOrientations="Portrait" Orientation="Portrait"
    shell:SystemTray.IsVisible="True"
    Loaded="PhoneApplicationPage_Loaded">


    <Grid x:Name="LayoutRoot" Background="Transparent">
        <StackPanel x:Name="SPCalendar" Margin="0" Orientation="Vertical">
            <TextBlock Name="txtHead" Text="" Style="{StaticResource titleCss}" />

            <!-- 日歷頭部 -->
            <StackPanel Orientation="Vertical">
                <!-- 日歷button -->
                <StackPanel Orientation="Horizontal" HorizontalAlignment="Center">
                    <Image Source="/Images/left.png" ManipulationStarted="Image_ManipulationStarted" />
                    <StackPanel Orientation="Horizontal" ManipulationStarted="StackPanel_ManipulationStarted">
                        <TextBlock Name="txtYear" Text="2014" Style="{StaticResource CHeadCss}" Width="110" />
                        <TextBlock Text="年" Style="{StaticResource CHeadCss}" />

                        <TextBlock Name="txtMonth" Text="04" Style="{StaticResource CHeadCss}" Width="55" />
                        <TextBlock Text="月" Style="{StaticResource CHeadCss}" />
                    </StackPanel>
                    <Image Source="/Images/right.png" ManipulationStarted="Image_ManipulationStarted" />
                </StackPanel>

                <!-- 日歷標題 -->
                <StackPanel Orientation="Horizontal">
                    <TextBlock Text="日" Style="{StaticResource CTitlerGreen}" />
                    <TextBlock Text="一" Style="{StaticResource CTitleCss}" />
                    <TextBlock Text="二" Style="{StaticResource CTitleCss}" />
                    <TextBlock Text="三" Style="{StaticResource CTitleCss}" />
                    <TextBlock Text="四" Style="{StaticResource CTitleCss}" />
                    <TextBlock Text="五" Style="{StaticResource CTitleCss}" />
                    <TextBlock Text="六" Style="{StaticResource CTitlerGreen}" />
                </StackPanel>
            </StackPanel>

            <!-- 日歷內容 -->
            <Grid Name="gCalendar"  Background="Gray" Width="480" Height="615">
                <Grid.RowDefinitions>
                    <RowDefinition Height="*" />
                    <RowDefinition Height="*" />
                    <RowDefinition Height="*" />
                    <RowDefinition Height="*" />
                    <RowDefinition Height="*" />
                    <RowDefinition Height="*" />
                </Grid.RowDefinitions>

                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="*" />
                    <ColumnDefinition Width="*" />
                    <ColumnDefinition Width="*" />
                    <ColumnDefinition Width="*" />
                    <ColumnDefinition Width="*" />
                    <ColumnDefinition Width="*" />
                    <ColumnDefinition Width="*" />
                </Grid.ColumnDefinitions>
            </Grid>
        </StackPanel>
    </Grid>
</phone:PhoneApplicationPage>

後臺CS參考代碼(部分):

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using CalendarApp.Resources;
using System.Collections.ObjectModel;
using Model;
using ChineseCalendar;
using CalendarApp.PageCode;
using CalendarApp.CustomControl;
using Microsoft.Phone.Controls.Primitives;


namespace CalendarApp
{
    public partial class MainPage : PhoneApplicationPage
    {
        #region 全局變量
        /// <summary>
        /// BGDateWeek 當月第一天星期數
        /// </summary>
        int BGDateWeek = new int();

        /// <summary>
        /// ObservableCollection<CalendarModel> myCalendar
        /// </summary>
        ObservableCollection<CalendarModel> myCalendar = new ObservableCollection<CalendarModel> { };
        #endregion

        public MainPage()
        {
            InitializeComponent();
        }

        private void PhoneApplicationPage_Loaded(object sender, RoutedEventArgs e)
        {
            //當前日期數據
            DateTime dt = DateTime.Now;
            if (this.NavigationContext.QueryString.Count > 0 && this.NavigationContext.QueryString["t"] != null)
                if (!DateTime.TryParse(this.NavigationContext.QueryString["t"].ToString(), out dt)) dt = DateTime.Now;

            this.txtYear.Text = dt.ToString("yyyy");
            this.txtMonth.Text = dt.ToString("MM");

            //載入當月日期數據
            GetMonthDate(dt.ToString("yyyy-MM-dd"));
        }


        #region 依據指定時間獲取日歷數據
        /// <summary>
        /// 依據指定時間獲取日歷數據
        /// </summary>
        /// <param name="dtMonth">時間 yyyy-MM</param>
        public void GetMonthDate(String dtMonth)
        {
            //初始化參數
            DateTime dt = DateTime.Now;
            if (!DateTime.TryParse(dtMonth, out dt)) dt = DateTime.Now;

            //獲取選中日期的第一天、最後一天。以及第一天星期幾
            DateTime BGDate, EDDate, GLDate;
            BGDate = new DateTime(dt.Year, dt.Month, 1);
            EDDate = BGDate.AddMonths(1).AddDays(-1);
            getWeekIndex(BGDate.DayOfWeek.ToString());

            //自己定義控件
            CustomControl.CustomDate cd = null;
            CustomControl.CustomDateGreen cdGreen = null;

            //初始化變量
            CalendarModel item = null;

            //清空
            this.gCalendar.Children.Clear();

            //循環加入數據
            int row = 0, col = 0;
            for (int i = 0, len = (EDDate - BGDate).Days; i <= len; i++)
            {
                //設定行和列
                if (i == 0)
                    col = BGDateWeek;
                else col++;

                if (col > 6)
                {
                    col = 0;
                    row++;
                }

                GLDate = BGDate.AddDays(i);
                item = new CalendarModel()
                {
                    GLDay = GLDate.ToString("dd"),
                    GLDate = GLDate.ToString("yyyy-MM-dd"),

                    NLDay = ChineseCalendarDate.GetChineseDate(GLDate),
                    NLDate = ChineseCalendarDate.GetChineseDateTime(GLDate)
                };

                //年信息
                String year = item.NLDate.Substring(0, item.NLDate.IndexOf("年") + 1);
                this.txtHead.Text = "農歷" + year;

                //綁定數據
                if (col == 0 || col == 6)
                {
                    cdGreen = new CustomDateGreen();
                    cdGreen.DataContext = item;
                    Grid.SetColumn(cdGreen, col);
                    Grid.SetRow(cdGreen, row);
                    this.gCalendar.Children.Add(cdGreen);
                }
                else
                {
                    cd = new CustomDate();
                    cd.DataContext = item;
                    Grid.SetColumn(cd, col);
                    Grid.SetRow(cd, row);
                    this.gCalendar.Children.Add(cd);
                }
            }
        }
        #endregion


        #region 獲取星期索引
        /// <summary>
        /// 獲取星期索引
        /// </summary>
        /// <param name="week"></param>
        private void getWeekIndex(String week)
        {
            switch (week)
            {
                case "Monday":   //周一
                    BGDateWeek = 1;
                    break;
                case "Tuesday":  //周二
                    BGDateWeek = 2;
                    break;
                case "Wednesday"://周三
                    BGDateWeek = 3;
                    break;
                case "Thursday"://周四
                    BGDateWeek = 4;
                    break;
                case "Friday":  //周五
                    BGDateWeek = 5;
                    break;
                case "Saturday"://周六
                    BGDateWeek = 6;
                    break;
                case "Sunday":  //周末
                    BGDateWeek = 0;
                    break;
            }
        }
        #endregion


        #region 時間選擇
        /// <summary>
        /// 時間選擇
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Image_ManipulationStarted(object sender, System.Windows.Input.ManipulationStartedEventArgs e)
        {
            Image image = (Image)sender;
            if (image != null)
            {
                int y = int.Parse(this.txtYear.Text);
                int m = int.Parse(this.txtMonth.Text);

                if (((System.Windows.Media.Imaging.BitmapImage)(image.Source)).UriSource.ToString().Contains("left.png"))
                {
                    //時間遞減
                    if (m - 1 <= 0)
                    {
                        //前一年
                        this.txtMonth.Text = "12";
                        this.txtYear.Text = (y - 1).ToString().Trim();
                    }
                    else
                    {
                        //當年,月遞減
                        m--;
                        if (m == 0) m = 1;
                        this.txtMonth.Text = m >= 10 ? m.ToString() : "0" + m.ToString();
                    }
                }
                else
                {
                    //時間遞增
                    if (m + 1 > 12)
                    {
                        //後一年
                        this.txtMonth.Text = "01";
                        this.txtYear.Text = (y + 1).ToString().Trim();
                    }
                    else
                    {
                        //當年,月遞增
                        m++;
                        this.txtMonth.Text = m >= 10 ? m.ToString() : "0" + m.ToString();
                    }
                }

                //獲取新時間
                GetNewDate();
            }
        }
        #endregion


        #region 相應時間選擇
        /// <summary>
        /// 相應時間選擇
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void StackPanel_ManipulationStarted(object sender, System.Windows.Input.ManipulationStartedEventArgs e)
        {
            String time = this.txtYear.Text + "-" + this.txtMonth.Text;
            this.NavigationService.Navigate(new Uri("/CustomControl/CustomDatePicker.xaml?

t=" + time, UriKind.Relative)); } #endregion #region 獲取新時間 /// <summary> /// 獲取新時間 /// </summary> public void GetNewDate() { //當前日期數據 DateTime dt = DateTime.Parse(this.txtYear.Text.Trim() + "-" + this.txtMonth.Text.Trim()); this.txtYear.Text = dt.ToString("yyyy"); this.txtMonth.Text = dt.ToString("MM"); //載入當月日期數據 GetMonthDate(dt.ToString("yyyy-MM-dd")); } #endregion } }


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


實例下載:

http://pan.baidu.com/s/1gd5UU5d


效果圖:

技術分享

技術分享

WP8日歷(含農歷)APP