如何从弹出消息导航到新页面?我正在使用 Xamarin 社区工具包弹出窗口

How do I navigate to a new page from a Popup message? I'm using the Xamarin Community Toolkit Popup

我正在尝试通过单击弹出窗口中的按钮导航到新页面。这是我拥有的:

     <StackLayout>
        <Label FontSize="15" Margin="20" Text="An account with this phone number doesn't exist. Do you want to create new account?" VerticalOptions="CenterAndExpand" HorizontalOptions="CenterAndExpand"/>
        <StackLayout Orientation="Horizontal">
            <Button BackgroundColor="{StaticResource SystemGreen}" CornerRadius="20" Margin="20" Text="Enter again"/>
            <Button BackgroundColor="{StaticResource SystemGreen}" CornerRadius="20" Margin="20" Text="Yes" Clicked="Button_Clicked"/>
        </StackLayout>
    </StackLayout>

下面是我的代码:

            private async void Button_Clicked(object sender, EventArgs e)
            {
                await Navigation.PushAsync(new RegisterNumberEntryPage());
            }

正如@Jason 提到的那样,您应该 return 从 Popup 到打开它的页面的值,并根据 return 值,您处理是否导航到另一个页面您的呼叫页面代码。

下面是 return 一个布尔值的弹出示例,当为真时将 RegisterNumberEntryPage 推送到导航堆栈。

MyPopup.xaml

<xct:Popup x:Class="AppTest.Popups.MyPopup"
           xmlns="http://xamarin.com/schemas/2014/forms"
           xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
           xmlns:xct="clr-namespace:Xamarin.CommunityToolkit.UI.Views;assembly=Xamarin.CommunityToolkit"
           x:TypeArguments="x:Boolean"
           IsLightDismissEnabled="False">

    <StackLayout>
        <Label Margin="20"
               FontSize="15"
               Text="An account with this phone number doesn't exist. Do you want to create new account?"
               VerticalOptions="CenterAndExpand"
               HorizontalOptions="CenterAndExpand"/>
        <StackLayout Orientation="Horizontal">
            <Button Margin="20"
                    CornerRadius="20"
                    Text="Enter again"/>
            <Button Margin="20"
                    CornerRadius="20"
                    Text="Yes"
                    Clicked="Button_Clicked"/>
        </StackLayout>
    </StackLayout>
</xct:Popup>

MyPopup.xaml.cs

 public partial class MyPopup
    {
        public MyPopup() => InitializeComponent();

        void Button_Clicked(object? sender, System.EventArgs e) => Dismiss(true);
    }

PopupPage.xaml.cs

 public partial class PopupPage : ContentPage
    {
        public PopupPage() => InitializeComponent();

        async void OpenPopup(object sender, EventArgs e)
        {
            var result = await App.Current.MainPage.Navigation.ShowPopupAsync(new MyPopup());

            if (result)
                await Navigation.PushAsync(new RegisterNumberEntryPage());
        }
    }

注意: 你可以 return 任何类型,你需要在 MyPopup.xaml x:TypeArguments 参数中指定它,你可能还想根据你的要求设置 IsLightDismissEnabled

官方文档

https://docs.microsoft.com/en-us/xamarin/community-toolkit/views/popup