我正试图在我的应用程序主页面完全加载并显示后,获得一个MAUI社区工具包弹出页面。我想展示一个第一次设置交易,或提示。
我试着在protected override void OnAppearing()
下调用它,但没有成功。当我遍历代码时,它在调用var result = await this.ShowPopupAsync(popup);
后立即退出DisplayPopup()
方法,并且不执行if(result is bool boolResult)
语句。
我确信我已经把所有的东西都安排好了。我只是想了解一下文档。
弹出页面的XAML:
<toolkit:Popup xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:toolkit="http://schemas.microsoft.com/dotnet/2022/maui/toolkit"
x:Class="MyApp.View.StartupPopup">
<VerticalStackLayout>
<Label Text="This is a very important message! Do you agree?" />
<Button Text="Yes"
Clicked="OnYesButtonClicked" />
<Button Text="No"
Clicked="OnNoButtonClicked" />
</VerticalStackLayout>
</toolkit:Popup>
主页代码:
using CommunityToolkit.Maui.Views;
namespace MyApp.View;
public partial class MyViewPage : ContentPage
{
public MyViewPage()
{
InitializeComponent();
}
protected override void OnAppearing()
{
DisplayPopup();
}
public async Task DisplayPopup()
{
var popup = new StartupPopup();
//After calling this, it just exits the method
var result = await this.ShowPopupAsync(popup);
if(result is bool boolResult)
{
if(boolResult)
{
// Yes was tapped
}
else
{
// No was tapped
}
}
}
弹出代码:
using CommunityToolkit.Maui.Views;
namespace MyApp.View;
public partial class StartupPopup : Popup
{
public StartupPopup()
{
InitializeComponent();
bool FirstTime = false;
bool ShowTips = true;
if(FirstTime)
{
//Show initial setup
}
else if(ShowTips)
{
//Show some tips
}
}
void OnYesButtonClicked(object? sender, EventArgs e) => Close(true);
void OnNoButtonClicked(object? sender, EventArgs e) => Close(false);
}
有什么想法吗?当我浏览代码时,主页实际上并没有完全显示出来。它仍然显示启动屏幕。我想知道这是否与此有关。
然而,当我离开主页,然后返回主页时,这确实有效。
您可以订阅ContentPage的Loaded事件,并从事件处理程序中显示弹出窗口。
protected override void OnNavigatedTo(NavigatedToEventArgs args)
{
this.Loaded += MainPage_Loaded;
}
protected override void OnNavigatedFrom(NavigatedFromEventArgs args)
{
this.Loaded -= MainPage_Loaded;
}
private async void MainPage_Loaded(object sender, EventArgs e)
{
StartupPopup popup = new StartupPopup();
var result= await this.ShowPopupAsync(popup);
}
更新
根据原始海报下方的评论,可以通过覆盖OnNavigatedTo
方法来显示弹出窗口。无需订阅Loaded事件。
protected override void OnNavigatedTo(NavigatedToEventArgs args)
{
StartupPopup popup = new StartupPopup();
var result= await this.ShowPopupAsync(popup);
}