English 中文(简体)
UWP App Crashes on Resuming after being Idle for a while
原标题:UWP App Crashes on Resuming After Being Idle for a While
  • 时间:2023-10-01 12:59:24
  •  标签:
  • uwp

我正在培养我的乐团音乐家多年。 我曾遇到一个问题,即“光线”在它被闲置后恢复,时间不确定。 然而,如果音乐家玩.,照相机的运行完全是罚款的。 坠机发生时, app子在闲置后又重新成为焦点。 我不能稳定地照搬坠机,但这次坠机确实是时候了。 我不知道需要多长时间。

我曾试图通过附上一小小gger来诊断这一问题,但坠毁的拖.是在 attached后发生的。 在<条码>级应用:应用程序上,我添加了<条码>Resuming、<代码>Suspending和 UnhandledException方法,但还是用斜线标示坠毁原因。

我对如何进一步解决这一问题感到悲痛。 任何关于下一步应采取哪些步骤或如何适当利用diagnose和避免这种坠毁的建议都将受到高度赞赏。

这是我音乐家守则的一部分。

public class MusicPlayer : IMusicEventListener
{
    public static MediaPlaybackState PlaybackState => Player.PlaybackSession.PlaybackState;
    public static bool IsPlaying => PlaybackState == MediaPlaybackState.Playing;
    public static Playlist NowPlaying => new Playlist(MenuFlyoutHelper.NowPlaying, CurrentPlaylist);
    private static MediaPlaybackList PlaybackList = new MediaPlaybackList() { MaxPlayedItemsToKeepOpen = 1 };
    public static MediaPlayer Player = new MediaPlayer() { Source = PlaybackList };
}

其部分内容是我的应用等级法典。

sealed partial class App : Application
{
    public static bool Inited { get; private set; } = false;
    public static List<Action> LoadedListeners = new List<Action>();
    public App()
    {
        this.InitializeComponent();
        this.Suspending += OnSuspending;
        this.Resuming += App_Resuming;
        this.UnhandledException += App_UnhandledException;
    }

    private void App_Resuming(object sender, object e)
    {
        Debug.WriteLine($"sender {sender.GetType()} e {e}");
    }

    protected override void OnLaunched(LaunchActivatedEventArgs e)
    {
        OnLaunched(e, null);
    }

    private void OnSuspending(object sender, SuspendingEventArgs e)
    {
        var deferral = e.SuspendingOperation.GetDeferral();
        try
        {
            TileHelper.ResumeTile();
            ToastHelper.HideToast();
            Save();
            SQLHelper.ClearInactive();
        }
        catch (Exception ex)
        {
            Log.Warn($"OnSuspending exception {ex}");
        }
        deferral.Complete();
    }

    public static async void Save()
    {
        SettingsHelper.Save();
        MusicPlayer.Save();
        await Helper.ClearBackups(10);
    }

    protected override void OnBackgroundActivated(BackgroundActivatedEventArgs args)
    {
        var deferral = args.TaskInstance.GetDeferral();
        switch (args.TaskInstance.Task.Name)
        {
            case ToastHelper.ToastTaskName:
                if (args.TaskInstance.TriggerDetails is Windows.UI.Notifications.ToastNotificationActionTriggerDetail details)
                {
                    // Perform tasks
                    if (Enum.TryParse(typeof(ToastButtonEnum), details.Argument, out object toastButton))
                    {
                        try
                        {
                            HandleBackgroundActivation((ToastButtonEnum)toastButton);
                        }
                        catch (Exception e)
                        {
                            Log.Warn($"HandleBackgroundActivation failed {e}");
                        }
                    }
                }
                break;
        }
        deferral.Complete();
    }

    private async void HandleBackgroundActivation(ToastButtonEnum toastButton)
    {
        switch (toastButton)
        {
            case ToastButtonEnum.Next:
                MusicPlayer.MoveNext();
                if (MusicPlayer.PlayMode == PlayMode.RepeatOne)
                {
                    ToastHelper.HideToast();
                    await ToastHelper.ShowToast(MusicPlayer.CurrentMusic, MusicPlayer.PlaybackState);
                }
                break;
            case ToastButtonEnum.Pause:
                MusicPlayer.Pause();
                break;
            case ToastButtonEnum.Play:
                MusicPlayer.Play();
                break;
            case ToastButtonEnum.SwitchLyricsSourceToMusic:
                await ToastHelper.SwitchLyricsSource(LyricsSource.Music);
                break;
            case ToastButtonEnum.SwitchLyricsSourceToLrcFile:
                await ToastHelper.SwitchLyricsSource(LyricsSource.LrcFile);
                break;
            case ToastButtonEnum.SwitchLyricsSourceToInternet:
                await ToastHelper.SwitchLyricsSource(LyricsSource.Internet);
                break;
        }
    }

    protected override async void OnFileActivated(FileActivatedEventArgs args)
    {
        base.OnFileActivated(args);
        Music music = await Music.LoadFromPathAsync(args.Files[0].Path);
        Log.Info("activate file " + args.Files[0].Path);
        if (music == null)
        {
            Helper.ShowButtonedNotification("CannotReadLocalMusicFile", "Authorize", async (n) =>
            {
                await StorageHelper.AuthorizeFolder();
            });
            return;
        }
        if (args.PreviousExecutionState == ApplicationExecutionState.Running)
        {
            MusicPlayer.AddNextAndPlay(music);
        }
        else
        {
            OnLaunched(null, music);
        }
    }

    private void App_UnhandledException(object sender, Windows.UI.Xaml.UnhandledExceptionEventArgs e)
    {
        Log.Error("App_UnhandledException {0}", e.Exception);
    }

}
最佳回答

<代码>Save()功能中有一个例外,该功能为async void notasync 任务。 因此,它的例外被逐年地推倒,没有被排入“待决”。 因此,解决办法只会在<代码>Save(功能>中处理这一问题。

private void OnSuspending(object sender, SuspendingEventArgs e)
{
    var deferral = e.SuspendingOperation.GetDeferral();
    try
    {
        TileHelper.ResumeTile();
        ToastHelper.HideToast();
        Save();
        SQLHelper.ClearInactive();
    }
    catch (Exception ex)
    {
        Log.Warn($"OnSuspending exception {ex}");
    }
    deferral.Complete();
}

public static async void Save()
{
    SettingsHelper.Save();
    MusicPlayer.Save();
    await Helper.ClearBackups(10);
}
问题回答

暂无回答




相关问题
Adjusting the height of the webview inside the listview

I have a listview that contains a webview that contains strings taken from data binding. I want if the webview contains an image, then the height of the webview is adjusted to the height of the image. ...

Python - How to launch uri with selection I want?

my brain is on fire and cant find for sure the most simple answer after hour of research. What I want to do is basicly: I want to start a uri and open it with "only this time" with the same ...

How to customize flyout page top bar in xamarin forms

I want to customize top bar of Xamarin forms like following i want to add some of menus search-bar and profile icon with drop down I am trying to achieve this with flyoutpage control. Please help me ...

热门标签