//加载数据 private async Task LoadCourseData(int weekOfTerm, int index) { //清空页面数据 _sv0.Content = null; _sv1.Content = null; _sv2.Content = null; //周数显示 Title = $"第 {weekOfTerm} 周"; int daySpan = (DateTime.Today - App.AppSettings.TermlyDate).Days; int currentWeek = daySpan < 0 ? -1 : daySpan / 7; ShowCurrentWeek = weekOfTerm - 1 == currentWeek ? Visibility.Visible : Visibility.Collapsed; //初始化月份及日期 DateTime weeklyDate = App.AppSettings.TermlyDate.AddDays((weekOfTerm - 1) * 7); _tbkMonth.Text = $"{weeklyDate.Month}\n月"; for (int i = 0; i < 7; i++) { _listDateDetail[i].Text = $"{weeklyDate.AddDays(i).Day.ToString()}\n周{_dayOfWeek[i]}"; _listDateDetail[i].Opacity = weeklyDate.AddDays(i) == DateTime.Today ? 1 : 0.7; } //清空课程面板的元素 _courseGrid.Children.Clear(); //初始化课程序号 for (int j = 0; j < 12; j++) { TextBlock tbkCourseRank = new TextBlock(); tbkCourseRank.VerticalAlignment = VerticalAlignment.Center; tbkCourseRank.TextAlignment = TextAlignment.Center; tbkCourseRank.Foreground = new SolidColorBrush(Colors.White); tbkCourseRank.Text = (j + 1).ToString(); Grid.SetColumn(tbkCourseRank, 0); Grid.SetRow(tbkCourseRank, j); _courseGrid.Children.Add(tbkCourseRank); } //从本地加载当前周课表信息 weeklyList = new List <List <Course> >(); weeklyList = await CourseDataService.LoadWeeklyDataFromIsoStoreAsync(weeklyList, weekOfTerm); if (weeklyList != null) { for (int i = 0; i < weeklyList.Count; i++) { for (int j = 0; j < weeklyList[i].Count; j++) { Style btnCourseStyle = App.Current.Resources["CourseButtonStyle"] as Style; Course course = weeklyList[i][j]; Button btn = new Button(); btn.Style = btnCourseStyle; btn.Content = course.OverView; btn.FontSize = 9.5; btn.Margin = new Thickness(1); btn.Background = bgdCourse[colorIndex]; btn.Tag = $"[{i}][{j}]"; //用于标记点击时button位置 Grid.SetColumn(btn, i + 1); //第一列是课程序号 Grid.SetRow(btn, course.StartMark); Grid.SetRowSpan(btn, course.CourseSpan); //btn.Click += BtnCourse_Click; btn.Tapped += btnCourse_Tapped; btn.Holding += btnCourse_Holding; btn.RightTapped += btnCourse_RightTapped; _courseGrid.Children.Add(btn); colorIndex = colorIndex < 11 ? ++colorIndex : 0; } } } else { await new MessageDialog("获取课表内容失败,请重启应用!!").ShowAsync(); } switch (index) { case 0: _sv0.Content = _courseGrid; break; case 1: _sv1.Content = _courseGrid; break; case 2: _sv2.Content = _courseGrid; break; } }
public async static Task Update() { StorageSettings settings = new StorageSettings(); if (settings.UserSignedIn) { //计算当前周数以及当天是一周中的哪一天 int daySpan = (DateTime.Today - settings.TermlyDate).Days; if (daySpan < 0 || daySpan / 7 > settings.WeekNum) { return; } int weekOfTerm = daySpan / 7; int temp = (int)DateTime.Today.DayOfWeek;//0表示一周的开始(周日),所以需要根据自身需要做个修订 int dayOfWeek = temp == 0 ? 6 : --temp; //获取所需课程数据 (上课20分钟内不更新) var weeklyList = await CourseDataService.LoadWeeklyDataFromIsoStoreAsync(weekOfTerm + 1); //当前周数据 var dailyList = weeklyList[dayOfWeek]; //当天数据 var result = from c in dailyList where !string.IsNullOrEmpty(c.FullName) && DateTime.Now < Convert.ToDateTime(c.StartTime).AddMinutes(20) select c; Debug.WriteLine(result.Count()); //更新磁贴 if (result.Count() > 0) { var c = result.First(); XmlDocument wideTileXML = TileUpdateManager.GetTemplateContent(TileTemplateType.TileWide310x150IconWithBadgeAndText); XmlNodeList wideTileTestAttributes = wideTileXML.GetElementsByTagName("text"); wideTileTestAttributes[0].AppendChild(wideTileXML.CreateTextNode(c.FullName)); wideTileTestAttributes[1].AppendChild(wideTileXML.CreateTextNode($"时间: {c.StartTime} - {c.EndTime}")); wideTileTestAttributes[2].AppendChild(wideTileXML.CreateTextNode("地点: " + c.Classroom)); XmlNodeList wideTileImageAttributes = wideTileXML.GetElementsByTagName("image"); ((XmlElement)wideTileImageAttributes[0]).SetAttribute("src", "ms-appx:///Assets/Toast.png"); TileNotification notification = new TileNotification(wideTileXML); //notification.ExpirationTime = DateTime TileUpdateManager.CreateTileUpdaterForApplication().Update(notification); //TileUpdateManager.CreateTileUpdaterForSecondaryTile("SecondaryTile")?.Update(notification); XmlDocument badgeXml = BadgeUpdateManager.GetTemplateContent(BadgeTemplateType.BadgeNumber); XmlElement badgeElement = (XmlElement)badgeXml.SelectSingleNode("/badge"); badgeElement.SetAttribute("value", result.Count().ToString()); BadgeNotification badge = new BadgeNotification(badgeXml); BadgeUpdateManager.CreateBadgeUpdaterForApplication().Update(badge); //TileUpdateManager.CreateTileUpdaterForSecondaryTile("SecondaryTile")?.Update(notification); //未开放二级磁贴选项 //if (SecondaryTile.Exists("SecondaryTile")) //{ // notification = new TileNotification(wideTileXML); // badge = new BadgeNotification(badgeXml); // TileUpdateManager.CreateTileUpdaterForSecondaryTile("SecondaryTile").Update(notification); // BadgeUpdateManager.CreateBadgeUpdaterForSecondaryTile("SecondaryTile").Update(badge); //} } else { //清空磁贴 TileUpdateManager.CreateTileUpdaterForApplication().Clear(); BadgeUpdateManager.CreateBadgeUpdaterForApplication().Clear(); //if (SecondaryTile.Exists("SecondaryTile"))//应用中未设置二级磁贴 //{ // TileUpdateManager.CreateTileUpdaterForSecondaryTile("SecondaryTile").Clear(); // BadgeUpdateManager.CreateBadgeUpdaterForSecondaryTile("SecondaryTile").Clear(); //} if (settings.PushNotification && DateTime.Now.Hour >= 21 && settings.NotificationDate != DateTime.Today)//每天晚上9点钟以后推送一次 { //检查次日是否有课 if (++dayOfWeek == 7) { weekOfTerm++; dayOfWeek = 0; } weeklyList = await CourseDataService.LoadWeeklyDataFromIsoStoreAsync(weekOfTerm); dailyList = weeklyList[dayOfWeek]; result = from c in dailyList where !string.IsNullOrEmpty(c.FullName) select c; Debug.WriteLine(result.Count()); if (result.Count() != 0) { settings.NotificationDate = DateTime.Today; XmlDocument toastXML = ToastNotificationManager.GetTemplateContent(ToastTemplateType.ToastText01); var toastTextElements = toastXML.GetElementsByTagName("text"); toastTextElements[0].AppendChild(toastXML.CreateTextNode(String.Format($"亲,你明天共有{result.Count()}节课,不要迟到哟!(●ˇ∀ˇ●)"))); ToastNotification notification = new ToastNotification(toastXML); ToastNotificationManager.CreateToastNotifier().Show(notification); } } } } }