Search code examples
c#xamarinxamarin.formsxamarin.android

Xamarin.Forms how to get the entered data from the pop-up window?


I have a pop-up window where user need to enter some information:

<pages:PopupPage xmlns="http://xamarin.com/schemas/2014/forms"

. . .
        <StackLayout>
            <Label Text="Enter name" />
            <Entry FontSize="20"
                   Placeholder=" Some_Thing" />
        </StackLayout>
        <StackLayout>
            <Label Text="Enter price" />
            <Entry FontSize="20"
                   Placeholder="250" />
        </StackLayout>
        <Button BackgroundColor="DodgerBlue"
                FontSize="30"
                Text="Save"
                TextColor="White"
                Clicked="new_product"
                />
    </StackLayout>
</pages:PopupPage>

the pop-up window is called as follows

    private void ShowNewUserPopup(object o, EventArgs e)
    {
        PopupNavigation.Instance.PushAsync(new NewUserPopupPage());
    }

but I have a question.how do I get the entered data from the pop-up window? before that, I tried the DisplayPromptAsync option and everything worked out.But what should I assign to the "result" variable this time? But since this is my first time working with pop-ups, I am not at all good at this, so I will be very grateful for your help:

async void new_product(object sender, EventArgs e)
{
    string result = await DisplayPromptAsync("New product", "name", "add", "cancel");
    if (result != null)
    {
        if (result.Length != 0)
        {
            await App.Database_Product.Create(new Database.Product(result, 100));
            name_colec.ItemsSource = await App.Database_Product.GetAsync();
        }
        else
        {
            await App.Database_Product.Create(new Database.Product());
            name_colec.ItemsSource = await App.Database_Product.GetAsync();
        }
    }

}

Solution

  • You could pass value through the Page Constructor. For example, when you want to pass the value which you enter in popup page and show it in another page when you do the navigation, the Page Constructor is an easy way.

    Set the Name of the Entry in your popup page:

     <StackLayout>
        <StackLayout>
            <Label  Text="Enter name" />
            <Entry x:Name="popup_Name" FontSize="20"
                   Placeholder="Some_Thing" />
        </StackLayout>
        <StackLayout>
            <Label  Text="Enter price" />
            <Entry  x:Name="popup_Price" FontSize="20"
                   Placeholder="250" />
        </StackLayout>
        <Button BackgroundColor="DodgerBlue"
                FontSize="30"
                Text="Save"
                TextColor="White"
                Clicked="new_product"
                />
    </StackLayout>
    

    Set the constructor for the page which you want to navigate:

     public Page2(string name, string price)
        {
            InitializeComponent();
            Name.Text = name;//Name if the Label name of Page2
             Price.Text = price;
        }
    

    Pass the value when you leave the popup page to another page. I use ContentPage for example:

      private async void new_product(object sender, EventArgs e)
        {        
            await Navigation.PushAsync(new Page2(popup_Name.Text, popup_Price.Text));
            await PopupNavigation.Instance.PopAllAsync();
        }
    

    Update:

         private async void new_product(object sender, EventArgs e)
        {        
             if (popup_Name.Text!= null)
          {
       
            await App.Database_Product.Create(new Database.Product(popup_Name.Text, 100));
            name_colec.ItemsSource = await App.Database_Product.GetAsync();
        }
        else
        {
            await App.Database_Product.Create(new Database.Product());
            name_colec.ItemsSource = await App.Database_Product.GetAsync();
        }
    
        }