【发布时间】: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