windows .Net MAUI WinUI推送通知打开新示例

3htmauhk  于 2022-11-18  发布在  Windows
关注(0)|答案(1)|浏览(362)

每次单击收到的推送通知都会打开该应用程序的一个新示例。
我找到的文档和许多其他示例使用以下代码:

protected override void OnLaunched(LaunchActivatedEventArgs e)
    {
        Frame rootFrame = Window.Current.Content as Frame;

        // Do not repeat app initialization when the Window already has content,
        // just ensure that the window is active.
        if (rootFrame == null)
        {
            // Create a Frame to act as the navigation context and navigate to the first page.
            rootFrame = new Frame();

            rootFrame.NavigationFailed += OnNavigationFailed;

            if (e.PreviousExecutionState == ApplicationExecutionState.Terminated)
            {
                //TODO: Load state from previously suspended application.
            }

            // Place the frame in the current Window.
            Window.Current.Content = rootFrame;
        }

        if (rootFrame.Content == null)
        {
            // When the navigation stack isn't restored navigate to the first page,
            // configuring the new page by passing required information as a navigation
            // parameter.
            rootFrame.Navigate(typeof(MainPage), e.Arguments);
        }

        // Ensure the current window is active.
        Window.Current.Activate();
    }

但这对我不起作用。下面的代码行导致Window.Current.Content上出现NullReferenceException。

Frame rootFrame = Window.Current.Content as Frame;

我也注意到我的App类是从MauiWinUIApplication继承的,我想这就是重点,也许这个MauiWinUIApplication有不同的方法来处理这个问题,但是我找不到:

public partial class App : MauiWinUIApplication

如何防止每次单击推送通知时打开新示例?
我正在使用:Visual Studio社区2022 17.4.0,毛伊岛.Net 6
谢谢你!

j0pj023g

j0pj023g1#

.Net MauiWinUI的默认行为是允许多个应用示例运行,这会因SQLite数据库或深度链接等各种原因给我们带来很多问题。这是this thread中正在跟踪的known issue
作为替代解决方案,您可以参考this link,了解如何使应用程序single-instanced在每次单击推送通知时都不会打开新示例。

相关问题