处理通知单击事件以在.NET MAUI Blazor应用程序中打开特定页面(仅适用于Windows平台)

问题描述 投票:0回答:1

我正在开发 .NET MAUI Blazor 应用程序,并使用 Microsoft.Toolkit.Uwp.NotificationsToastContentBuilder 实现了通知服务。 (仅适用于 Windows 平台)我想处理通知上的单击事件,以便当用户单击它时,MAUI Blazor 应用程序会在由通知中传递的参数确定的特定页面(Blazor 页面)上打开。

我关注了一篇关于处理激活的文章(用户点击通知), 处理激活 这建议重写 App.xaml.cs 文件中的 OnActivated() 方法。但是,当我尝试重写此方法时,遇到错误,指出该方法不适合重写。

这是我收到的错误:

方法不适合重写

是否有人在打开特定页面的 .NET MAUI Blazor 应用程序中成功实现了通知单击事件处理程序?我怎样才能正确地重写 OnActivated() 方法,或者我应该采取另一种方法?

这是我的NotficationService 类

#if WINDOWS
using MAUIDemo.Services;
using Microsoft.Toolkit.Uwp.Notifications;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MAUIDemo.PlatformServices.Windows
{
    public class NotificationService : INotificationService
    {
        public void ShowNotification(string title, string body)
        {
            new ToastContentBuilder()
           .AddToastActivationInfo(null, ToastActivationType.Foreground)
           .AddAppLogoOverride(new Uri("ms-appx:///Assets/dotnet_bot.png"))
           .AddText(title, hintStyle: AdaptiveTextStyle.Header)
           .AddText(body, hintStyle: AdaptiveTextStyle.Body)
           .AddButton(new ToastButton()
               .SetContent("See more details")
               .AddArgument("action", "viewDetails"))

            .AddButton(new ToastButton()
                .SetContent("Remind me later")
                .AddArgument("action", "remindLater"))
           .Show();
        }


    }
}
#endif
.net push-notification notifications maui maui-blazor
1个回答
0
投票

使用 .NET MAUI 构建的 Windows 应用程序使用 Windows UI3(WinUI 3) 库来创建面向 Windows 桌面的本机应用程序。但你使用的方法是针对UWP的。

为了处理 .NET MAUI 中的激活,您可以进行一些更改。

首先,编辑位于Platforms/Windows文件夹下的Package.appxmanifest文件。您可以参考WinUI细节中的2.使用外部NuGet包

<Package
 ...
  xmlns:desktop="http://schemas.microsoft.com/appx/manifest/desktop/windows10"
  xmlns:com="http://schemas.microsoft.com/appx/manifest/com/windows10"
  IgnorableNamespaces="uap rescap com desktop">

...
<Applications>
<Application Id="App" Executable="$targetnametoken$.exe" EntryPoint="$targetentrypoint$">
  <uap:VisualElements
  ...  
  </uap:VisualElements>

  <Extensions>

        <!-- Specify which CLSID to activate when toast clicked -->
        <desktop:Extension Category="windows.toastNotificationActivation">
            <desktop:ToastNotificationActivation ToastActivatorCLSID="8D2A1FF2-A8EE-412A-86AF-9EB20DE932DD" />
        </desktop:Extension>

        <!--Register COM CLSID LocalServer32 registry key-->
        <com:Extension Category="windows.comServer">
            <com:ComServer>
                <com:ExeServer Executable="MauiApp8.exe" Arguments="-ToastActivated" DisplayName="Toast activator">
                    <com:Class Id="8D2A1FF2-A8EE-412A-86AF-9EB20DE932DD" DisplayName="Toast activator"/>
                </com:ExeServer>
            </com:ComServer>
        </com:Extension>

    </Extensions>
</Application>

其次,通过在位于

Platforms/Windows
文件夹下的 App.xaml.cs 中添加以下行来订阅 OnActivated

public partial class App : MauiWinUIApplication
{
     
    public App()
    {
        this.InitializeComponent();
    }
    ...
    //add the following lines
    protected override void OnLaunched(LaunchActivatedEventArgs args)
    {
        ToastNotificationManagerCompat.OnActivated += ToastNotificationManagerCompat_OnActivated;
        base.OnLaunched(args);
    }

    void ToastNotificationManagerCompat_OnActivated(ToastNotificationActivatedEventArgsCompat e)
    {
        // Handle ToastNotificationEvent.
        Console.WriteLine(e.ToString());
    }
    ...

}

希望有帮助!

© www.soinside.com 2019 - 2024. All rights reserved.