1

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

     <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());
            }
4

1 回答 1

3

正如@Jason 提到的,您应该从 Popup 返回一个值到打开它的页面,并根据返回值处理是否导航到调用页面代码中的另一个页面。

下面是一个返回布尔值的弹出窗口示例,当真正推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());
        }
    }

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

官方文档

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

于 2022-01-16T17:58:13.627 回答