簡體   English   中英

.Net MAUI WinUI 推送通知打開新實例

[英].Net MAUI WinUI push notification opening new instance

每次點擊收到的推送通知都會打開應用程序的一個新實例。

我發現的文檔和許多其他示例使用以下代碼:

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 class 是從 MauiWinUIApplication 繼承的,我認為這就是重點。 也許這個 MauiWinUIApplication 有不同的方法來處理這個問題,但我找不到:

public partial class App : MauiWinUIApplication

每次單擊推送通知時如何防止打開新實例?

我正在使用:Visual Studio Community 2022 17.4.0,Maui.Net 6

謝謝!

.Net MauiWinUI的默認行為是允許您的應用程序的多個實例運行,這將由於各種原因給我們帶來很多問題,例如 SQLite 數據庫或深度鏈接等。這是known issue ,正在跟蹤簡化並使單實例 WinUI 應用程序的過程更加健壯

作為替代解決方法,您可以參考使應用程序成為單實例,了解如何使應用成為single-instanced ,這將防止每次單擊推送通知時都打開新實例。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM