private void SaveLists(object sender, SuspendingEventArgs e) { var deferral = e.SuspendingOperation.GetDeferral(); if (IsMyBackgroundTaskRunning) { // Stop handling player events immediately RemoveMediaPlayerEventHandlers(); // Tell the background task the foreground is suspended MessageService.SendMessageToBackground(new AppStateChangedMessage(AppState.Suspended)); } // Persist that the foreground app is suspended ApplicationSettingsHelper.SaveSettingsValue(ApplicationSettingsConstants.AppState, AppState.Suspended.ToString()); ShuffleList shuffleList = new ShuffleList(Albums.albumList.ToList()); shuffleList.SaveShuffleList(shuffleList.GenerateNewList(ShuffleList.FAV_LIST_CAPACITY)); ShuffleList.SaveFavouriteList(shuffleList.GenerateFavouriteList()); deferral.Complete(); }
private async void WaitingBar_Loaded(object sender, RoutedEventArgs e) { if (IsInitialed) { var str = ApplicationSettingsHelper.ReadResetSettingsValue("NewCreated"); if (str == null) { HideBar(WaitingBar); str = ApplicationSettingsHelper.ReadResetSettingsValue("NewAdded"); if (str != null) { MessageService.SendMessageToBackground(new RefreshFilesMessage()); Albums.notifyrefresh += Albums_notifyrefresh; await Albums.Refresh(); SetOtherPages(); if (Albums.albumList.Count > 0) { MessageService.SendMessageToBackground(new UpdateFilesMessage(Albums.albumList.ToSongModelList())); } } str = ApplicationSettingsHelper.ReadResetSettingsValue("NewDeleted"); if (str != null) { Albums.RestoreAlbums(); } return; } } RefreshState v = RefreshState.Normal; v = Albums.RestoreAlbums(); Albums.CopytoAlbumList(); Albums.progresschanged += Albums_progresschanged; switch (v) { case RefreshState.NeedCreate: await Albums.FirstCreate(); break; case RefreshState.NeedRefresh: await Albums.Refresh(); break; case RefreshState.Normal: break; } HideBar(WaitingBar); ((Window.Current.Content as Frame).Content as MainPage).FinishCreate(); IsInitialed = true; foreach (var task in BackgroundTaskRegistration.AllTasks) { if (task.Value.Name == nameof(BackgroundAudio)) { isMyBackgroundTaskRunning = true; break; } } StartBackgroundAudioTask(); Application.Current.Suspending += SaveLists; TimeSpan period = TimeSpan.FromSeconds(5); #pragma warning disable CS4014 // 由于此调用不会等待,因此在调用完成前将继续执行当前方法 SetOtherPages(); #pragma warning restore CS4014 // 由于此调用不会等待,因此在调用完成前将继续执行当前方法 PeriodicTimer = ThreadPoolTimer.CreateTimer((source) => { if (!IsShuffleListInitialed) { ShuffleList shuffleList = new ShuffleList(Albums.albumList.ToList()); var list = shuffleList.GenerateNewList(ShuffleList.FAV_LIST_CAPACITY); list = ShuffleList.ShuffleIt(list); #pragma warning disable CS4014 // 由于此调用不会等待,因此在调用完成前将继续执行当前方法 Dispatcher.RunAsync(CoreDispatcherPriority.High, async () => { ShuffleListResources.Source = list; await ShowShuffleArtwork(list); HideBar(FavWaitingBar); }); #pragma warning restore CS4014 // 由于此调用不会等待,因此在调用完成前将继续执行当前方法 } }, period); }