I'm trying to navigate to a new page upon a click of button inside the popup. Here is what I have:
<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>
and here is my code behind:
private async void Button_Clicked(object sender, EventArgs e)
{
await Navigation.PushAsync(new RegisterNumberEntryPage());
}
As @Jason mentionned you should return a value from your Popup to the page that opened it, and based on the return value, you handle whether or no to navigate to another page in your calling page code.
Below is a sample of a popup returning a boolean, when true pushing RegisterNumberEntryPage
to the navigation stack.
<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>
public partial class MyPopup
{
public MyPopup() => InitializeComponent();
void Button_Clicked(object? sender, System.EventArgs e) => Dismiss(true);
}
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());
}
}
Note:
that you can return any type, you need to specify it in MyPopup.xaml
x:TypeArguments
parameter, you might also want to set IsLightDismissEnabled
according to your requirements.
https://learn.microsoft.com/en-us/xamarin/community-toolkit/views/popup