使用本书 .net maui 为 C# 开发人员尝试让 Snackbar 在 Windows 中工作

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

当我在 .net maui 中为 C# 开发人员尝试示例时出现此错误。

Message=Package.appxmanifest 文件中需要进行其他设置才能在 Windows 上启用 Snackbar。

此外,必须调用

UseMauiCommunityToolkit(options => options.SetShouldEnableSnackbarOnWindows(true);
才能在 Windows 上启用 Snackbar。有关详细信息,请参阅 Snackbar 文档的平台特定初始化部分:https://learn.microsoft.com/dotnet/communitytoolkit/maui/alerts/snackbar

我访问了该网站,并从 https://learn.microsoft.com/dotnet/communitytoolkit/maui/alerts/snackbar

进行了如下更改

这是来自 mauiProgram.cs 的代码

public static MauiApp CreateMauiApp()
{
    var builder = MauiApp.CreateBuilder().UseMauiCommunityToolkit(options => options.SetShouldEnableSnackbarOnWindows(true));
    builder
      .UseMauiApp<App>()
      .UseMauiCommunityToolkit()
      .UseMauiCommunityToolkitMarkup()
      .ConfigureFonts(fonts =>
      {
          fonts.AddFont("OpenSans-Regular.ttf", "OpenSansRegular");
          fonts.AddFont("OpenSans-Semibold.ttf", "OpenSansSemibold");
      });

package.appxmanifes 文件中所做的更改

 xmlns:com="http://schemas.microsoft.com/appx/manifest/com/windows10"
xmlns:desktop="http://schemas.microsoft.com/appx/manifest/desktop/windows10"

  IgnorableNamespaces="uap rescap desktop">

程序启动然后获取未处理异常的消息。我不知道该去哪里看。试图在网上找到简单的样本。请注意,toast 在网站上有效。

c# maui
1个回答
0
投票

我可以重现您的问题,并通过添加 Jason 所说的步骤来修复它:

在 Package.appxmanifest 中的每个标签内,添加以下扩展:


<Extensions>

   <!-- Specify which CLSID to activate when notification is clicked -->
   <desktop:Extension Category="windows.toastNotificationActivation">
       <desktop:ToastNotificationActivation ToastActivatorCLSID="6e919706-2634-4d97-a93c-2213b2acc334" /> 
   </desktop:Extension>

   <!-- Register COM CLSID -->
   <com:Extension Category="windows.comServer">
       <com:ComServer>
           <com:ExeServer Executable="YOUR-PATH-TO-EXECUTABLE" DisplayName="$targetnametoken$" Arguments="----AppNotificationActivated:"> <!-- Example path to executable: CommunityToolkit.Maui.Sample\CommunityToolkit.Maui.Sample.exe -->
               <com:Class Id="6e919706-2634-4d97-a93c-2213b2acc334" />
           </com:ExeServer>
       </com:ComServer>
   </com:Extension>

</Extensions>

我检查了 github repo 中的示例。您的项目的

Executable="YOUR-PATH-TO-EXECUTABLE"
应为
Executable="Your prject name\Your project name.exe"
。在我的项目中,它是
Executable="MauiAppTest\MauiAppTest.exe"
并且小吃栏可以正确显示。

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