コード例 #1
0
ファイル: CoursePage.xaml.cs プロジェクト: arcsinW/zhangkong
        private void RefreshTiles()
        {
            if(App.IsCourseImported == false)
                return;
            TimeSpan t1 = new TimeSpan(App.StartDate.Ticks);
            TimeSpan t2 = new TimeSpan(DateTime.Now.Ticks);

            TimeSpan offset = t2.Subtract(t1);//计算天数之差

            if (offset.TotalDays < 0)
            {
                ShellTile ttf = ShellTile.ActiveTiles.First();
                IconicTileData data1 = new IconicTileData();

                data1.Title = "掌控校园";
                data1.Count = 0;
                data1.IconImage = new Uri("/toolargemiddle.png", UriKind.Relative);
                data1.SmallIconImage = new Uri("/toolargesmall.png", UriKind.Relative);
                data1.WideContent1 = "";
                data1.WideContent2 = "";
                data1.WideContent3 = "";
                ttf.Update(data1);
                return;
            }

            int weekNumber = (int)offset.Days / 7;//计算周数

            string dataFromIsoStore =
                Convert.ToString(IsolatedStorageSettings.ApplicationSettings["ParsedWeek" + weekNumber.ToString()]);

            WeeklyCourses weeklyCourses = new WeeklyCourses();
            weeklyCourses = JsonConvert.DeserializeObject<WeeklyCourses>(dataFromIsoStore);

            int dayofweek = ((int)DateTime.Now.DayOfWeek - 1 + 7) % 7;
            DailyCourses dailyCourses = weeklyCourses.WeeklyCoursesCollention[dayofweek];

            bool latestCourse = true;
            ShellTile TileToFind = ShellTile.ActiveTiles.First();
            IconicTileData data = new IconicTileData();

            data.Title = "掌控校园";
            data.Count = 0;
            data.IconImage = new Uri("/toolargemiddle.png", UriKind.Relative);
            data.SmallIconImage = new Uri("/toolargesmall.png", UriKind.Relative);
            data.WideContent1 = "";
            data.WideContent2 = "";
            data.WideContent3 = "";

            int count = 0;
            for (int i = 0; i < dailyCourses.DailyCoursesCollection.Count; i++)
            {
                Course course = dailyCourses.DailyCoursesCollection[i];
                if (course.CourseName != "")
                {
                    if (DateTime.Now < course.StartTime.AddMinutes(20)) //保证在上课20分钟内不会更新磁贴
                    {
                        count++;
                        if (latestCourse)
                        {
                            data.IconImage = new Uri("/toolargemiddle.png", UriKind.Relative);
                            data.SmallIconImage = new Uri("/toolargesmall.png", UriKind.Relative);
                            data.WideContent1 = course.CourseName;
                            data.WideContent2 = "时间:" + course.StartTime.ToShortTimeString() + "--" +
                                                course.EndTime.ToShortTimeString();
                            data.WideContent3 = "地点:" + course.Classroom;
                            latestCourse = false;
                        }
                    }
                }
            }

            data.Count = count;
            TileToFind.Update(data);
        }
コード例 #2
0
ファイル: CoursePage.xaml.cs プロジェクト: arcsinW/zhangkong
        private void ResponseCallback(IAsyncResult result)
        {
            Stream stream = null;

            string postbackdata = String.Empty;

            try
            {
                //获取异步操作返回的的信息
                HttpWebRequest httpWebRequest = (HttpWebRequest)result.AsyncState;
                //结束对 Internet 资源的异步请求
                WebResponse webResponse = httpWebRequest.EndGetResponse(result);
                stream = webResponse.GetResponseStream();
                using (StreamReader sr = new StreamReader(stream))
                {
                    postbackdata = sr.ReadToEnd();
                }
            }
            catch
            {
                Dispatcher.BeginInvoke(() =>
                {
                    MessageBox.Show("未能连接到远程服务器,请检查本地网络!", "程序异常", MessageBoxButton.OK);
                    ProgressBar.Visibility = Visibility.Collapsed;

                });
                return;
            }
            finally
            {
                if (stream != null)
                {
                    stream.Close();
                }
            }

            if (postbackdata.StartsWith("{"))
            {
                IsolatedStorageSettings.ApplicationSettings["postback"] = postbackdata;
                IsolatedStorageSettings.ApplicationSettings.Save();

                //因为HttpWebRequest是异步,不在UI线程上。所以要改变UI线程上的控件属性就要用Dispatcher.BeginInvoke()。
                Dispatcher.BeginInvoke(() =>
                {
                    try
                    {
                        //提示:必须先切割字符串
                        JObject json = JObject.Parse(postbackdata);

                        int termLength = Convert.ToInt32(json["Length"]);

                        //把最原始的信息存入独立存储
                        for (int i = 0; i < termLength; i++)
                        {
                            IsolatedStorageSettings.ApplicationSettings["Week" + i.ToString()] = json["Data"][i].ToString();
                        }
                        IsolatedStorageSettings.ApplicationSettings["TermLength"] = termLength;
                        IsolatedStorageSettings.ApplicationSettings.Save();

                        string[] startTimeofCourse = new string[]
                        {
                            "8:10:0", "9:0:0", "10:15:0", "11:05:0", "12:40:0", "14:30", "15:20:0", "16:25:0", "17:15:0",
                            "19:10:0", "20:0:0", "20:50:0"
                        };

                        string[] endTimeofCourse = new[]
                        {
                            "8:55:0", "9:45:0", "11:0:0", "11:50:0", "14:0:0", "15:15:0", "16:05:0", "17:10:0", "18:00",
                            "19:55:0", "20:45:0", "21:35:0"
                        };

                        DateTime startTime = new DateTime(2015, 8, 31);

                        for (int k = 0; k < termLength; k++)
                        {
                            string weeklyCourseData = json["Data"][k].ToString();

                            JObject jsonData = JObject.Parse(weeklyCourseData);

                            WeeklyCourses weekly = new WeeklyCourses();
                            weekly.StartTime = startTime;
                            weekly.Number = k + 1;
                            for (int i = 0; i < 7; i++)
                            {
                                DailyCourses daily = new DailyCourses();

                                for (int j = 0; j < 12; )
                                {
                                    bool whetherIgnore = false;//是否跳过中午那节课,用于课程结束时间的判断

                                    Course course = new Course();

                                    course.CourseName = jsonData["WeeklyList"][i]["DailyList"][(j)]["Name"].ToString();
                                    if (course.CourseName == "教研" || course.CourseName == "中秋节" ||
                                        course.CourseName == "国庆节" || course.CourseName == "报到注册" ||
                                        course.CourseName == "考试周" || course.CourseName == "元旦节")
                                        course.CourseName = "";

                                    course.Classroom = jsonData["WeeklyList"][i]["DailyList"][(j)]["ClassRoom"].ToString();
                                    course.CourseType = jsonData["WeeklyList"][i]["DailyList"][(j)]["Type"].ToString();
                                    course.Credits = jsonData["WeeklyList"][i]["DailyList"][(j)]["Credit"].ToString();
                                    course.ClassHours = jsonData["WeeklyList"][i]["DailyList"][(j)]["ClassHours"].ToString();
                                    course.TeacherName = jsonData["WeeklyList"][i]["DailyList"][(j)]["Teacher"].ToString();
                                    course.StartTime =
                                        DateTime.Parse(startTime.Year + "/" + startTime.Month + "/" + startTime.Day +
                                                       " " + startTimeofCourse[j]);

                                    int length = 1;
                                    //循环查询下一节课是否和本节相同
                                    while (j + length < 12 && course.CourseName == jsonData["WeeklyList"][i]["DailyList"][(j+length)]["Name"].ToString()
                                               && course.Classroom == jsonData["WeeklyList"][i]["DailyList"][(j+length)]["ClassRoom"].ToString()
                                        )
                                    {
                                        length++;
                                        if (length + j == 4 && App.MaximumCourseNumber == 11) //跳过中午那节课
                                        {
                                            j++;
                                            whetherIgnore = true;
                                        }
                                    }

                                    j += length;
                                    if (whetherIgnore)
                                        course.EndTime =
                                            DateTime.Parse(startTime.Year + "/" + startTime.Month + "/" + startTime.Day +
                                                           " " + endTimeofCourse[j - 2]);
                                    else
                                        course.EndTime =
                                            DateTime.Parse(startTime.Year + "/" + startTime.Month + "/" + startTime.Day +
                                                           " " + endTimeofCourse[j - 1]);

                                    course.Length = length;
                                    daily.DailyCoursesCollection.Add(course);
                                }
                                startTime = startTime.AddDays(1);
                                weekly.WeeklyCoursesCollention.Add(daily);
                            }
                            var dataofWeeks = JsonConvert.SerializeObject(weekly);
                            IsolatedStorageSettings.ApplicationSettings["ParsedWeek" + k.ToString()] = dataofWeeks;
                        }

                        App.IsCourseImported = true;
                        IsolatedStorageSettings.ApplicationSettings["IsCourseImported"] = App.IsCourseImported;
                        IsolatedStorageSettings.ApplicationSettings.Save();
                        LeadingInCanvas.Visibility = Visibility.Collapsed;
                        ProgressBar.Visibility = Visibility.Collapsed;
                        CaculateCourseNumber();
                        InitializeCourseGrid();
                        RefreshTiles();
                        StartPeriodicAgent();

                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.Message);
                    }
                });
            }
            else
            {
                Dispatcher.BeginInvoke(() =>
                {
                    MessageBox.Show(postbackdata);
                    PasswordCanvas.Visibility = System.Windows.Visibility.Visible;
                    ProgressBar.Visibility = Visibility.Collapsed;
                    PasswordTextBox.Focus();
                });

            }
        }
コード例 #3
0
ファイル: CoursePage.xaml.cs プロジェクト: arcsinW/zhangkong
        private void LoadCourseFromIsoStore()
        {
            int i = 0, k = 0;//i控制最大课程的数量,k控制课程编号

            for (; i < 4; i++, k++)
            {
                TextBlock textBlock = new TextBlock();
                textBlock.Text = (k + 1).ToString();
                textBlock.TextAlignment = TextAlignment.Center;
                textBlock.VerticalAlignment = VerticalAlignment.Center;
                SolidColorBrush brush = new SolidColorBrush(Color.FromArgb((byte)200, (byte)255, (byte)255, (byte)255));
                textBlock.Foreground = brush;
                FontSize = 20;
                textBlock.FontFamily = new FontFamily("Microsoft YaHei");
                Grid.SetRow(textBlock, i);
                Grid.SetColumn(textBlock, 0);
                courseGrid.Children.Add(textBlock);
            }

            if (App.MaximumCourseNumber == 12)
            {
                TextBlock textBlock = new TextBlock();
                textBlock.Text = "午";
                textBlock.TextAlignment = TextAlignment.Center;
                textBlock.VerticalAlignment = VerticalAlignment.Center;
                SolidColorBrush brush = new SolidColorBrush(Color.FromArgb((byte)200, (byte)255, (byte)255, (byte)255));
                textBlock.Foreground = brush;
                FontSize = 20;
                textBlock.FontFamily = new FontFamily("Microsoft YaHei");
                Grid.SetRow(textBlock, 4);
                Grid.SetColumn(textBlock, 0);
                courseGrid.Children.Add(textBlock);
                i++;
            }

            for (; i < App.MaximumCourseNumber; i++, k++)
            {
                TextBlock textBlock = new TextBlock();
                textBlock.Text = (k + 1).ToString();
                textBlock.TextAlignment = TextAlignment.Center;
                textBlock.VerticalAlignment = VerticalAlignment.Center;
                SolidColorBrush brush = new SolidColorBrush(Color.FromArgb((byte)200, (byte)255, (byte)255, (byte)255));
                textBlock.Foreground = brush;
                FontSize = 20;
                textBlock.FontFamily = new FontFamily("Microsoft YaHei");
                Grid.SetRow(textBlock, i);
                Grid.SetColumn(textBlock, 0);
                courseGrid.Children.Add(textBlock);
            }

            string dataFromIsoStore =
                Convert.ToString(IsolatedStorageSettings.ApplicationSettings["ParsedWeek" + tempraryWeekNumber.ToString()]);

            weeklyCourses = JsonConvert.DeserializeObject<WeeklyCourses>(dataFromIsoStore);

            i = 0;
            for (; i < MyWeeklyCourses.WeeklyCoursesCollention.Count; i++)
            {
                int temp = 0;

                for (int j = 0; j < MyWeeklyCourses.WeeklyCoursesCollention[i].DailyCoursesCollection.Count; j++)
                {
                    Course course = MyWeeklyCourses.WeeklyCoursesCollention[i].DailyCoursesCollection[j];

                    if (String.IsNullOrEmpty(course.CourseName))
                    {
                        temp += course.Length;
                        continue;
                    }
                    Button button = new Button();
                    button.Opacity = 0.9;
                    button.Style = this.Resources["CourseButton"] as Style;
                    button.Background = CourseColorBrush[ColorNum];
                    //button.Background = new SolidColorBrush(Color.FromArgb((byte)50, (byte)255, (byte)255, (byte)255));
                    button.Tag = i.ToString() + j.ToString();
                    button.Click += ShowDetailofCourse_Click;
                    button.Margin = new Thickness(1, 1, 1, 1);
                    button.Content = course.Detail;

                    Grid.SetRow(button, temp);
                    Grid.SetColumn(button, i + 1);
                    Grid.SetRowSpan(button, course.Length);
                    courseGrid.Children.Add(button);
                    temp += course.Length;

                    ColorNum++;
                    if (ColorNum >= 12)
                        ColorNum = 0;
                }

            }
            IsCourseLoaded = true;
        }