Home > Software design >  Problem with navigating to another view xamarin community toolkit
Problem with navigating to another view xamarin community toolkit

Time:12-26

I have a problem while trying to navigate to another view from community toolkit popup. It simply wont navigate to new page, instead after clicking button nothing happens.

Code:

private async void Btn_Clicked_UpdateAlbum(object sender, EventArgs e)
    {

        string newalbumname = albumname.Text;

        Preferences.Set("NewAlbumName", newalbumname);

        await Navigation.PushModalAsync(new ChosePhotosAlbum());


    }

I'm using

packages\xamarin.communitytoolkit\2.0.5\

CodePudding user response:

You can return a value from your PopUp to the page that opened it, and based on the return value, you can handle whether or not to navigate to another page in your calling page code.

Below is a sample of a popup returning a bool, when true navigate to page ChoosePhotosAlbum.

MainPage.xaml.cs

<?xml version="1.0" encoding="utf-8" ?> 
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:xct="http://xamarin.com/schemas/2020/toolkit"
             x:Class="PopApp1226.MainPage">

    <StackLayout>
        <Frame BackgroundColor="#2196F3" Padding="24" CornerRadius="0">
            <Label Text="Welcome to Main Page" HorizontalTextAlignment="Center" TextColor="White" FontSize="36"/>
        </Frame>

        <Button Text="popup" Clicked="Button_Clicked" />

    </StackLayout>
</ContentPage>

MainPage.xaml.cs

public partial class MainPage : ContentPage 
{
    public MainPage()
    {
        InitializeComponent();
    }

    private async  void Button_Clicked(object sender, EventArgs e)
    {

        var result = await App.Current.MainPage.Navigation.ShowPopupAsync(new SimplePopup());

        bool returnvalue = false;

        if (result is bool)
            returnvalue = (bool)result;

        if (returnvalue) {
            await Navigation.PushModalAsync(new ChoosePhotosAlbum());
        }
    }
}

SimplePopup.xaml.cs

[XamlCompilation(XamlCompilationOptions.Compile)] 
public partial class SimplePopup : Popup
{
    public SimplePopup()
    {
        InitializeComponent();
    }

    private void Button_Clicked(object sender, EventArgs e)
    {
        Dismiss(true);
    }
}

SimplePopup.xaml

<?xml version="1.0" encoding="utf-8" ?> 
<xct:Popup 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:Class="PopApp1226.SimplePopup">
    <StackLayout>
        <Label Margin="20"
               FontSize="15"
               Text="SimplePopup"
               VerticalOptions="CenterAndExpand"
               HorizontalOptions="CenterAndExpand"/>
        <StackLayout Orientation="Horizontal">
            <Button Margin="20"
                    CornerRadius="20"
                    Text="test"/>
            <Button Margin="20"
                    CornerRadius="20"
                    Text="Yes"
                    Clicked="Button_Clicked"/>
        </StackLayout>
    </StackLayout>
</xct:Popup>
  • Related