在Windows应用商店应用中添加约束以获取日期

时间:2014-06-29 12:53:36

标签: c# windows xaml windows-store-apps

我制作了一个名为Scheduler的简单Windows 8.1商店应用。它就像一个ToDo应用程序,它要求用户填写个人或官方会议等的详细信息,并在特定的一天通知用户他们开会。

我想添加一个约束,应用程序会在保存详细信息之前检查当前日期,如果计划已过时,则会显示一个消息框用户。

如何实现?

public MainPage()
{
        this.InitializeComponent();
        fillYear(2014, 2114);
        fillMonth();
        fillDays();
        cboxMonth.SelectionChanged += cboxMonth_SelectionChanged;
        cboxYear.SelectionChanged += cboxYear_SelectionChanged;
        lstViewTaskCategories.Items.Add("Personal");
        lstViewTaskCategories.Items.Add("Official");
        lstViewTaskCategories.SelectedIndex = 0;
    }

    void cboxYear_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
        fillDays();
    }

    void cboxMonth_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
        fillDays();
    }

    void fillDays()
    {
        cboxDay.Items.Clear();
        cboxDay.Items.Add("");

        try
        {
            int days = DateTime.DaysInMonth(Convert.ToInt32(cboxYear.SelectedValue), getMonth(cboxMonth.SelectedValue.ToString()));
            for (int i = 1; i <= days; i++)
                cboxDay.Items.Add(i);
            cboxDay.SelectedIndex = 0;
        }
        catch (Exception ex)
        {
        }
    }

    int getMonth(string monthName)
    {
        switch (monthName)
        {
            case "January":
                return 1;
            case "February":
                return 2;
            case "March":
                return 3;
            case "April":
                return 4;
            case "May":
                return 5;
            case "June":
                return 6;
            case "July":
                return 7;
            case "August":
                return 8;
            case "September":
                return 9;
            case "October":
                return 10;
            case "November":
                return 11;
            case "December":
                return 12;
            default:
                return -1;

        }
    }

    void fillMonth()
    {
        cboxMonth.Items.Add("");
        cboxMonth.Items.Add("January");
        cboxMonth.Items.Add("February");
        cboxMonth.Items.Add("March");
        cboxMonth.Items.Add("April");
        cboxMonth.Items.Add("May");
        cboxMonth.Items.Add("June");
        cboxMonth.Items.Add("July");
        cboxMonth.Items.Add("August");
        cboxMonth.Items.Add("September");
        cboxMonth.Items.Add("October");
        cboxMonth.Items.Add("November");
        cboxMonth.Items.Add("December");
        cboxMonth.SelectedIndex = 0;
    }

    void fillYear(int startYear, int endYear)
    {
        cboxYear.Items.Add("");

        for (int i = startYear; i <= endYear; i++)
            cboxYear.Items.Add(i);

        cboxYear.SelectedIndex = 0;
    } 

1 个答案:

答案 0 :(得分:0)

将组合框的值解析为DateTime对象。

DateTime selectedDate = new DateTime(int.Parse(cboxYear.Text), GetMonth(cboxMonth.Text), int.Parse(cboxDay.Text));  

然后您可以比较验证的值