Example #1
0
        private void RenewBtn_Click(object sender, RoutedEventArgs e)
        {
            Details.Width = new GridLength(0);
            isDetail      = false;
            Uri        uri = new Uri(@"查看详情.png", UriKind.Relative);
            ImageBrush ib  = new ImageBrush();

            detailImage.Source = new BitmapImage(uri);
            tooltipTxt.Text    = "查看详情";

            StaticGlobal.pickeddays.Clear();
            StaticGlobal.holidayanddate.Clear();
            StaticGlobal.dateandholiday.Clear();
            StaticGlobal.singleweekendList.Clear();
            StaticGlobal.doubleweekendList.Clear();
            StaticGlobal.totalholidayNum = 0;

            StaticGlobal.pickedNum  = 0;
            summerbeginTxt.Text     = "";
            summerendTxt.Text       = "";
            winterbeginTxt.Text     = "";
            winterendTxt.Text       = "";
            holidayPanel.Visibility = Visibility.Collapsed;
            detailImage.Visibility  = Visibility.Collapsed;
            YearPanel.Children.Clear();
            int thisYear = DateTime.Now.Year;

            for (int i = 1; i <= 12; i++)
            {
                MonthControl monthcontrol = new MonthControl();
                monthcontrol.MonthControlLoad(thisYear, i);
                YearPanel.Children.Add(monthcontrol);
            }
            CommitBtn.IsEnabled    = false;
            RenewBtn.IsEnabled     = false;
            StaticGlobal.allowPick = true;
        }
Example #2
0
        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            //读年份xml文件
            FileStream   fs         = File.Open("Year.xml", FileMode.Open, FileAccess.Read);
            StreamReader sr         = new StreamReader(fs);
            string       text       = sr.ReadToEnd();
            var          yearResult = XmlSerializeHelper.DeSerialize <int>(text);

            fs.Close();   //一定要加上这一句

            ////测试代码
            //int sum = Test.add(7, 8);
            //int sub = Compute.Sub(40, 4);

            int thisYear = DateTime.Now.Year;

            if (thisYear == yearResult)
            {
                //读xml文件中的假期原始数据
                FileStream   fsholoiday   = File.Open("HolidayOriginalData.xml", FileMode.Open, FileAccess.Read);
                StreamReader srholoiday   = new StreamReader(fsholoiday);
                string       textholoiday = srholoiday.ReadToEnd();
                var          list         = XmlSerializeHelper.DeSerialize <List <HolidayMessage> > (textholoiday);
                StaticGlobal.holidayList = list;
                fsholoiday.Close();
            }
            else
            {
                //写入年份xml文件
                using (Stream fStream = new FileStream("Year.xml", FileMode.Create,
                                                       FileAccess.Write, FileShare.None))
                {
                    XmlSerializer xmlFormat = new XmlSerializer(typeof(int));
                    xmlFormat.Serialize(fStream, thisYear);
                }

                //获取今年所有法定假日
                DateTime beginDate = (DateTime.Parse("2017-12-31"));
                DateTime endDate   = (DateTime.Parse("2018-12-31"));
                while (beginDate < endDate)
                {
                    beginDate = beginDate.AddDays(1);
                    StaticGlobal.holidayList.Add(new HolidayMessage(beginDate.ToString("yyyy-MM-dd")));
                }
                string urlbase = "http://api.goseek.cn/Tools/holiday?date=";
                string url     = string.Empty;
                string str     = string.Empty;
                for (int i = 0; i < StaticGlobal.holidayList.Count; i++)
                {
                    url = urlbase + StaticGlobal.holidayList[i].Day.Replace("-", "");
                    WebRequest wRequest = WebRequest.Create(url);
                    wRequest.Method      = "GET";
                    wRequest.ContentType = "text/html;charset=UTF-8";
                    WebResponse  wResponse = wRequest.GetResponse();
                    Stream       stream    = wResponse.GetResponseStream();
                    StreamReader reader    = new StreamReader(stream, System.Text.Encoding.UTF8);
                    str = reader.ReadToEnd();                   //url返回的值
                    Result result = JsonToObject <Result>(str); //注意,返回json格式为{"code":10001,"data":2},所以Result类的属性名称必须也是code和data,不然json转对象时会出错
                    StaticGlobal.holidayList[i].Flag = result.data;
                }

                //写入xml文件
                using (Stream fStream = new FileStream("HolidayOriginalData.xml", FileMode.Create,
                                                       FileAccess.Write, FileShare.None))
                {
                    XmlSerializer xmlFormat = new XmlSerializer(typeof(List <HolidayMessage>));
                    xmlFormat.Serialize(fStream, StaticGlobal.holidayList);
                }
            }

            Seek.init(thisYear);   //初始化,new一个seek类对象
            ////测试代码
            //int test1 = Seek.test_year();
            //判断今年是否是闰年
            if (IsLeap(thisYear))   //是闰年
            {
                StaticGlobal.days = new int[13] {
                    0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
                }
            }
            ;
            else
            {
                StaticGlobal.days = new int[13] {
                    0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
                }
            };

            StaticGlobal.weekdayDic.Add("Sunday", 0);
            StaticGlobal.weekdayDic.Add("Monday", 1);
            StaticGlobal.weekdayDic.Add("Tuesday", 2);
            StaticGlobal.weekdayDic.Add("Wednesday", 3);
            StaticGlobal.weekdayDic.Add("Thursday", 4);
            StaticGlobal.weekdayDic.Add("Friday", 5);
            StaticGlobal.weekdayDic.Add("Saturday", 6);

            //read_festival
            for (int i = 0; i < StaticGlobal.holidayList.Count; i++)
            {
                DateTime date  = Convert.ToDateTime(StaticGlobal.holidayList[i].Day);
                int      month = date.Month;
                int      day   = date.Day;
                Seek.read_festival(month, day, StaticGlobal.holidayList[i].Flag);
            }

            ////测试代码
            //List<HolidayMessage> onetwo = (from r in StaticGlobal.holidayList where r.Flag == 0 select r).ToList();
            //int count = onetwo.Count;
            //int test2 = Seek.test_festival();

            int  holidayindex = 0;
            bool isHoliday    = false;

            for (int i = 0; i < StaticGlobal.holidayList.Count; i++)
            {
                switch (StaticGlobal.holidayList[i].Flag)
                {
                case 2:
                    int m = i;
                    while (StaticGlobal.holidayList[m].Flag == 2)
                    {
                        StaticGlobal.holidayDic.Add(StaticGlobal.holidayList[m].Day,
                                                    StaticGlobal.holidays[holidayindex]);
                        isHoliday = true;
                        m        += 1;
                    }
                    if (isHoliday)
                    {
                        holidayindex += 1;
                        isHoliday     = false;
                    }
                    i = m - 1;
                    break;

                case 1:
                    StaticGlobal.holidayDic.Add(StaticGlobal.holidayList[i].Day, "周末");
                    break;

                default:
                    StaticGlobal.holidayDic.Add(StaticGlobal.holidayList[i].Day, "");
                    break;
                }
            }

            //中秋和国庆混在一起的情况
            //由于StaticGlobal.holidays中先中秋,所以这里只找中秋
            List <string> MiddleList = (StaticGlobal.holidayDic.Where(q => q.Value == "中秋").Select(q => q.Key)).ToList();

            if (MiddleList.Count > 7)
            {
                for (int i = 0; i < MiddleList.Count; i++)
                {
                    StaticGlobal.holidayDic[MiddleList[i]] = "中秋&&国庆";
                }
            }

            //初始化
            for (int i = 0; i < StaticGlobal.holidays.Length; i++)
            {
                StaticGlobal.holidayanddate.Add(StaticGlobal.holidays[i], null);
            }

            //加载日历
            for (int i = 1; i <= 12; i++)
            {
                MonthControl monthcontrol = new MonthControl();
                monthcontrol.MonthControlLoad(thisYear, i);
                YearPanel.Children.Add(monthcontrol);
            }
        }