在.NET MAUI中,实现弹出框(Popup)有两种不同的场景。以下是这两种方式的详细说明:
方式一:直接调用弹出框,通过代码自动生成窗体内容
这种方式通常使用DisplayAlert
、DisplayActionSheet
或DisplayPromptAsync
等方法,它们都是同步的,并且直接在代码中生成弹出框的内容。
DisplayAlert
用于显示一个简单的警告对话框,包含标题、消息和一个关闭按钮。
bool wasConfirmed = await DisplayAlert("Title", "Message", "Accept", "Cancel");
if (wasConfirmed)
{
// 用户点击了"Accept"
}
else
{
// 用户点击了"Cancel"
}
DisplayActionSheet
用于显示一个带有多个选项的动作表,允许用户选择一个操作。
string action = await DisplayActionSheet("Title", "Cancel", null, "Option 1", "Option 2");
DisplayPromptAsync
用于显示一个带有文本输入框的对话框,允许用户输入文本。
(string result, bool userConfirmed) = await DisplayPromptAsync("Enter your name", "Name", "OK", "Cancel");
if (userConfirmed && !string.IsNullOrWhiteSpace(result))
{
// 用户输入了文本并点击了"OK"
}
方式二:采用Pupop方式生成或调用弹出窗体页面
这种方式涉及到创建一个专门的弹出窗体页面(例如继承自ContentPage
的类),然后通过导航服务来显示这个页面作为弹出框。
创建弹出窗体页面
首先,创建一个弹出窗体页面:
public partial class DeviceModePopup : PopupBase, ITransientDependency
{
public string Mode = "";
public string SetResult = "";
public DeviceModePopup()
{
InitializeComponent();
//Picker picker = new Picker { Title = "Select a monkey" };
//picker.SetBinding(Picker.ItemsSourceProperty, "Monkeys");
//picker.ItemDisplayBinding = new Binding("Name");
//var maxNum = 60;
for(int num=0;num< 60;num++)
{
var str = num.ToString("D2");
TimeMinute.Items.Add(str);
TimeSecond.Items.Add(str);
}
TimeMinute.SelectedIndex = 0;
TimeSecond.SelectedIndex = 0;
PickerAccount.IsVisible = true;
SkipMode.IsVisible = true;
TimeJump.IsVisible = false;
NumberJump.IsVisible = false;
}
显示弹出窗体页面
然后,使用导航服务显示这个页面:
// 处理按钮点击事件
using (var objWrapper = iocResolver.ResolveAsDisposable<DeviceModePopup>())
{
deviceModePopup = objWrapper.Object;
await navigationService.ShowPopupAsync(deviceModePopup);
// 关闭弹出页面后获取返回值
var setResult = deviceModePopup.SetResult;
var setMode = deviceModePopup.Mode;
SkipMode = string.Format("{0} {1}", setMode, setResult);
}
Console.WriteLine("Button clicked!");
这种方式的好处是可以自定义弹出窗体页面的布局和行为,适用于需要更复杂交互的场景。
另外还可以直接分创建页面的内容
// 创建Popup实例
listPopup = new Popup();
var list = new List<SelectItem>();
foreach (var lis in TaskService.listConnPool)
{
list.Add(new SelectItem() { Key = lis.Uuid.ToString(), Value = lis.Name });
}
ListView listView = new ListView
{
HorizontalOptions = LayoutOptions.Center,
VerticalOptions = LayoutOptions.Center,
ItemsSource = list,
ItemTemplate = new DataTemplate(() =>
{
var textCell = new TextCell();
textCell.SetBinding(TextCell.TextProperty, "Value");
textCell.SetBinding(TextCell.DetailProperty, "Key");
textCell.SetBinding(TextCell.CommandProperty, "ItemSelected");
//textCell.SetValue(Label.FontAttributesProperty, FontAttributes.Bold);
//textCell.Tapped += (s, args) => OnListViewItemSelected(s, args);
return textCell;
}),
};
// 设置ItemSelected事件处理程序
listView.ItemSelected += OnListViewItemSelected;
listView.SetValue(PaddingProperty, (10, 5, 10, 5));
listView.Margin = 5;
listView.RowHeight = 40;
listView.Header = new Label
{
Margin = 5,
Text = "设备列表",
FontSize = 12,
HorizontalOptions = LayoutOptions.Center
};
listPopup = new Popup
{
//Anchor = ImageButton,
Size = new Size(300, 300),
Content = listView // 将ListView设置为Popup的内容
};
this.ShowPopup(listPopup);