弹出对话框的代码如下:
MainPage.xaml代码:
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="HtDevice.MainPage">
<ScrollView>
<VerticalStackLayout
Spacing="25"
Padding="30,0"
VerticalOptions="Center">
<Label
Text="Hello, Hovertree!"
SemanticProperties.HeadingLevel="Level1"
FontSize="32"
HorizontalOptions="Center" />
<Label
Text="Welcome to Hovertree MAUI"
SemanticProperties.HeadingLevel="Level2"
SemanticProperties.Description="Welcome to Hovertree MAUI "
FontSize="18"
HorizontalOptions="Center" />
<Button
x:Name="HtBtn"
Text="Click me"
SemanticProperties.Hint="Show tips"
Clicked="HtBtn_Clicked"
HorizontalOptions="Center" />
</VerticalStackLayout>
</ScrollView>
</ContentPage>
MainPage.xaml.cx代码:
private async void HtBtn_Clicked(object sender, EventArgs e)
{
await DisplayAlert("Tips", "Welcome to hovertree.com", "OK");
}
效果如下图(安卓系统上运行):

本图背景为黑色,是由于Android系统设置为深色主题。
在Windows系统上运行的效果如下图:

点击按钮效果图:

MAUI的ContentPage类,有三种方法用于通过弹出窗口与用户交互,分别是: DisplayAlert、 DisplayActionSheet和 DisplayPromptAsync。
本示例使用了DisplayAlert
public System.Threading.Tasks.Task
DisplayAlert (string title, string message, string accept, string cancel, Microsoft.Maui.FlowDirection flowDirection);
参数
title
String
警报对话框的标题。
message
String
警报对话框的正文文本。
accept
String
要显示在“接受”按钮上的文本。
cancel
String
要显示在“取消”按钮上的文本。
重载:
DisplayAlert(String, String, String, String, FlowDirection)
使用接受和取消按钮向应用程序用户显示警报对话框。
DisplayAlert(String, String, String, String)
使用接受和取消按钮向应用程序用户显示警报对话框。
DisplayAlert(String, String, String)
使用单个取消按钮向应用程序用户显示警报对话框。
DisplayAlert(String, String, String, FlowDirection)
使用单个取消按钮向应用程序用户显示警报对话框。