public async void Run(IBackgroundTaskInstance taskInstance) { bool isLoad = false; PasswordCredential credential = null; var vault = new PasswordVault(); var credentialList = vault.FindAllByResource("ZSCY"); if (credentialList.Count == 1) { credentialList[0].RetrievePassword(); credential = credentialList[0]; } try { var list = DatabaseMethod.ToModel(); BackgroundTaskDeferral deferral1 = taskInstance.GetDeferral(); if (list.Count > 0) { //上传上次未上传的 foreach (var item in list) { if (item.Id == null) { string content = ""; isLoad = true; MyRemind tempRemind = new MyRemind(); tempRemind = JsonConvert.DeserializeObject <MyRemind>(item.json); tempRemind.IdNum = credential.Password; tempRemind.StuNum = credential.UserName; content = await NetWork.httpRequest(@"http://hongyan.cqupt.edu.cn/cyxbsMobile/index.php/Home/Person/addTransaction", NetWork.addRemind(tempRemind)); Debug.WriteLine(content); AddRemindReturn tempReturn = JsonConvert.DeserializeObject <AddRemindReturn>(content); if (tempReturn.Status == 200) { string id = tempReturn.Id; tempRemind.Id = id; tempRemind.IdNum = null; tempRemind.StuNum = null; DatabaseMethod.EditDatabase(item.Num, id, JsonConvert.SerializeObject(tempRemind), item.Id_system); } } } } deferral1.Complete(); BackgroundTaskDeferral deferral2 = taskInstance.GetDeferral(); if (isLoad) { } else { RemindHelp.SyncRemind(); } deferral2.Complete(); } catch (Exception) { throw; } }
public static async void SyncRemind() { List <KeyValuePair <string, string> > paramList = new List <KeyValuePair <string, string> >(); PasswordCredential user = null; var vault = new PasswordVault(); var credentialList = vault.FindAllByResource("ZSCY"); if (credentialList.Count == 1) { credentialList[0].RetrievePassword(); user = credentialList[0]; } paramList.Add(new KeyValuePair <string, string>("stuNum", user.UserName)); paramList.Add(new KeyValuePair <string, string>("idNum", user.Password)); string content = ""; try { content = await NetWork.httpRequest(@"http://hongyan.cqupt.edu.cn/cyxbsMobile/index.php/Home/Person/getTransaction", paramList); } catch (Exception x) { Debug.WriteLine(x.Message); } //相当于MyRemind GetRemindModel getRemid = new GetRemindModel(); try { getRemid = JsonConvert.DeserializeObject <GetRemindModel>(content); } catch (Exception ex) { await new MessageDialog(ex.Message).ShowAsync(); } List <string> getRemindList_json = new List <string>(); List <MyRemind> remindList = new List <MyRemind>(); #region 返回的json格式和添加的风格不一样,转换 foreach (var item in getRemid.DataItems) { //getRemindList_json.Add(getRemid.DataItems[0].Id.ToString()); MyRemind mr = new MyRemind(); List <DateItemModel> dim = new List <DateItemModel>(); //每个MyRemind的date foreach (var itemData in item.DateItems) { DateItemModel dateitme = new DateItemModel(); string week = ""; foreach (var itemWeek in itemData.WeekItems) { week += itemWeek + ","; } week = week.Remove(week.Length - 1); dateitme.Class = itemData.Class.ToString(); dateitme.Day = itemData.Day.ToString(); dateitme.Week = week; dim.Add(dateitme); } mr.Title = item.Title; mr.Content = item.Content; mr.DateItems = dim; mr.Time = item.Time; mr.Id = item.Id.ToString(); remindList.Add(mr); } #endregion List <string> RemindTagList = new List <string>(); RemindTagList = DatabaseMethod.ClearRemindItem() as List <string>; var notifier = ToastNotificationManager.CreateToastNotifier(); if (RemindTagList != null) { for (int i = 0; i < RemindTagList.Count(); i++) { var scheduledNotifs = notifier.GetScheduledToastNotifications() .Where(n => n.Tag.Equals(RemindTagList[i])); // Remove all of those from the schedule foreach (var n in scheduledNotifs) { notifier.RemoveFromSchedule(n); } } } foreach (var remindItem in remindList) { string id = await SyncAllRemind(remindItem); DatabaseMethod.ToDatabase(remindItem.Id, JsonConvert.SerializeObject(remindItem), id); } }