【问题标题】:.Net MAUI WinUI push notification opening new instance.Net MAUI WinUI 推送通知打开新实例
【发布时间】:2022-11-17 14:05:57
【问题描述】:

每次点击收到的推送通知都会打开应用程序的一个新实例。

我发现的文档和许多其他示例使用以下代码:

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();
    }

但这对我不起作用。以下行导致 NullReferenceException窗口.当前.内容.

Frame rootFrame = Window.Current.Content as Frame;

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

public partial class App : MauiWinUIApplication

每次单击推送通知时如何防止打开新实例?

我在用着: Visual Studio 社区 2022 17.4.0, 毛伊岛.Net 6

谢谢!

【问题讨论】:

  • 我对这个话题一无所知,但你确定那是 WinUI 3 代码,而不是旧的 WinUI / UWP 代码吗?无论如何,我找到了可能相关的this WindowsAppSDK-Sample。
  • 注意:在您的代码中,单击MauiWinUIApplication,然后按 F12 转到其声明。你会看到public abstract class MauiWinUIApplication : Application, IPlatformApplication。 Application 是 Microsoft.UI.Xaml.Application,所以据我所知,它将与任何其他 WinUI 3 应用程序(Windows 桌面应用程序)一样工作。

标签: c# windows push-notification maui


【解决方案1】:

.Net Maui中WinUI的默认行为是允许多个实例运行您的应用程序,这会给我们带来很多问题,例如 SQLite 数据库或深度链接等各种原因。这是 known issue,正在 this thread 中进行跟踪。

作为替代解决方法,您可以参考this link 了解如何制作应用程序single-instanced 这将阻止每次单击推送通知时打开新实例。

【讨论】:

    猜你喜欢
    • 2022-08-03
    • 1970-01-01
    • 1970-01-01
    • 2016-12-11
    • 2013-09-04
    • 1970-01-01
    • 1970-01-01
    • 2014-05-25
    • 1970-01-01
    相关资源
    最近更新 更多