toast
本文内容
Toast
是屏幕底部显示的计时警报。 它会在可配置的持续时间后被自动消除。
它以小警报的方式向用户提供有关操作的简单反馈。
要访问 Toast
功能,需执行以下特定于平台的设置。
使用 Snackbar
时,必须执行以下两个步骤:
1.使用 MauiAppBuilder 启用 snackbar 使用情况
使用 UseMauiCommunityToolkit
利用 options
参数在 Windows 上启用 snackbar 使用情况,如下所示:
var builder = MauiApp.CreateBuilder()
.UseMauiCommunityToolkit(options =>
{
options.SetShouldEnableSnackbarOnWindows(true);
})
上述操作将通过配置生命周期事件(OnLaunched
和 OnClosed
)来自动注册所需的处理程序。
2.在 Package.appxmanifest 文件中包括 ToastNotification 注册
若要处理 snackbar 操作,需要按如下所示修改 Platform\Windows\Package.appxmanifest
文件:
在 Package.appxmanifest 中,在打开的 <Package>
标记中添加以下 XML 命名空间:
xmlns:com="http://schemas.microsoft.com/appx/manifest/com/windows10"
xmlns:desktop="http://schemas.microsoft.com/appx/manifest/desktop/windows10"
在 Package.appxmanifest 中,在打开的 <Package>
标记中,更新 IgnorableNamespaces
以包含 uap
rescap
com
和 desktop
:
IgnorableNamespaces="uap rescap com desktop"
例如:已完成 <Package>
标记
下面是已完成的打开的 <Package>
标记的示例,该标记添加了对 Snackbar
的支持:
<Package
xmlns="http://schemas.microsoft.com/appx/manifest/foundation/windows10"
xmlns:uap="http://schemas.microsoft.com/appx/manifest/uap/windows10"
xmlns:rescap="http://schemas.microsoft.com/appx/manifest/foundation/windows10/restrictedcapabilities"
xmlns:com="http://schemas.microsoft.com/appx/manifest/com/windows10"
xmlns:desktop="http://schemas.microsoft.com/appx/manifest/desktop/windows10"
IgnorableNamespaces="uap rescap com desktop">
在 Package.appxmanifest 中,在每个 <Application>
标记内,添加以下扩展:
<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>
例如:已完成 <Applications>
标记
下面是已完成 <Applications>
标记的示例,该标记现在添加了对 Snackbar
的支持:
<Applications>
<Application Id="App" Executable="$targetnametoken$.exe" EntryPoint="$targetentrypoint$">
<uap:VisualElements
DisplayName="$placeholder$"
Description="$placeholder$"
Square150x150Logo="$placeholder$.png"
Square44x44Logo="$placeholder$.png"
BackgroundColor="transparent">
<uap:DefaultTile Square71x71Logo="$placeholder$.png" Wide310x150Logo="$placeholder$.png" Square310x310Logo="$placeholder$.png" />
<uap:SplashScreen Image="$placeholder$.png" />
</uap:VisualElements>
<Extensions>
<desktop:Extension Category="windows.toastNotificationActivation">
<desktop:ToastNotificationActivation ToastActivatorCLSID="6e919706-2634-4d97-a93c-2213b2acc334" />
</desktop:Extension>
<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>
</Application>
</Applications>
示例:更新 Package.appxmanifest
的文件以支持 Snackbar
下面是已更新以支持 Windows 上的 Snackbar
的 Package.appxmanifest
文件示例:
<?xml version="1.0" encoding="utf-8"?>
<Package
xmlns="http://schemas.microsoft.com/appx/manifest/foundation/windows10"
xmlns:uap="http://schemas.microsoft.com/appx/manifest/uap/windows10"
xmlns:rescap="http://schemas.microsoft.com/appx/manifest/foundation/windows10/restrictedcapabilities"
xmlns:com="http://schemas.microsoft.com/appx/manifest/com/windows10"
xmlns:desktop="http://schemas.microsoft.com/appx/manifest/desktop/windows10"
IgnorableNamespaces="uap rescap com desktop">
<Identity Name="maui-package-name-placeholder" Publisher="CN=Microsoft" Version="0.0.0.0" />
<Properties>
<DisplayName>$placeholder$</DisplayName>
<PublisherDisplayName>Microsoft</PublisherDisplayName>
<Logo>$placeholder$.png</Logo>
</Properties>
<Dependencies>
<TargetDeviceFamily Name="Windows.Universal" MinVersion="10.0.17763.0" MaxVersionTested="10.0.19041.0" />
<TargetDeviceFamily Name="Windows.Desktop" MinVersion="10.0.17763.0" MaxVersionTested="10.0.19041.0" />
</Dependencies>
<Resources>
<Resource Language="x-generate" />
</Resources>
<Applications>
<Application Id="App" Executable="$targetnametoken$.exe" EntryPoint="$targetentrypoint$">
<uap:VisualElements
DisplayName="$placeholder$"
Description="$placeholder$"
Square150x150Logo="$placeholder$.png"
Square44x44Logo="$placeholder$.png"
BackgroundColor="transparent">
<uap:DefaultTile Square71x71Logo="$placeholder$.png" Wide310x150Logo="$placeholder$.png" Square310x310Logo="$placeholder$.png" />
<uap:SplashScreen Image="$placeholder$.png" />
</uap:VisualElements>
<Extensions>
<desktop:Extension Category="windows.toastNotificationActivation">
<desktop:ToastNotificationActivation ToastActivatorCLSID="6e919706-2634-4d97-a93c-2213b2acc334" />
</desktop:Extension>
<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>
</Application>
</Applications>
<Capabilities>
<rescap:Capability Name="runFullTrust" />
</Capabilities>
</Package>
有关处理激活的详细信息:从 C# 应用发送本地 toast 通知
语法
C#
若要显示 Toast
,请先使用静态方法 Toast.Make()
创建它,然后使用其方法 Show()
显示它。
using CommunityToolkit.Maui.Alerts;
CancellationTokenSource cancellationTokenSource = new CancellationTokenSource();
string text = "This is a Toast";
ToastDuration duration = ToastDuration.Short;
double fontSize = 14;
var toast = Toast.Make(text, duration, fontSize);
await toast.Show(cancellationTokenSource.Token);
调用 Toast.Make()
时,需要其参数 string text
。 其他所有参数都是可选参数。 其可选参数 ToastDuration duration
使用默认持续时间 ToastDuration.Short
。 其可选参数 double fontSize
使用默认值 14.0
。
以下屏幕截图显示生成的 Toast:
属性
属性
类型
说明
默认值
文本
string
显示在 Toast
中的文本。
必需
持续时间
ToastDuration
显示的持续时间 Toast
。
ToastDuration.Short
TextSize
double
文本字号。
14.0
ToastDuration
ToastDuration
枚举定义以下成员:
Short
- 显示 Toast
2 秒
Long
- 显示 Toast
3.5 秒
这些值遵循 android.widget.Toast
API 中定义的常数。
方法
方法
说明
显示
显示请求的 Toast
。 如果当前显示 Toast
,则会在显示请求的 Toast
之前自动将其消除。
取消
消除当前 toast。
注意
一次只能显示一个 Toast
。 如果第二次调用 Show
方法,则将自动消除第一个 Toast
。
示例
可以在 .NET MAUI 社区工具包示例应用程序 中找到此功能的示例。
API
可以在 .NET MAUI 社区工具包 GitHub 存储库 查看Toast
的源代码
API 通过实现 IToast
接口,支持使用你自己的实现替代现有方法或创建你自己的 Toast。
Toast 在由 Google 创建的 Android 上实现。 其他平台使用自定义实现的容器(UIView
用于 iOS 和 MacCatalyst,ToastNotification
用于 Windows)。
Tizen 上的 Toast 不能使用其 Duration
和 TextSize
属性进行自定义。