Show Pop Up for entering Data

Ronald Rex 1,666 Reputation points
2023-03-28T20:06:05.7666667+00:00

Hi Friends. I want to show a Modal Pop Up to get data from the user. Is there a base class for a Pop Up View and Can I use a RelayCommand to display the Pop up? Thanks !!!!

.NET MAUI
.NET MAUI
A Microsoft open-source framework for building native device applications spanning mobile, tablet, and desktop.
3,419 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,855 questions
XAML
XAML
A language based on Extensible Markup Language (XML) that enables developers to specify a hierarchy of objects with a set of properties and logic.
805 questions
0 comments No comments
{count} votes

Accepted answer
  1. Leon Lu (Shanghai Wicresoft Co,.Ltd.) 74,641 Reputation points Microsoft Vendor
    2023-03-29T02:01:35.56+00:00

    Hello,

    You can achieve it by Popup - .NET MAUI Community Toolkit - .NET Community Toolkit | Microsoft Learn.

    Firstly, please refer to this document to set it up

    Then create a contentpage like following code. I add Entry to get data from the user.

    <?xml version="1.0" encoding="utf-8" ?>
    <toolkit:Popup xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
                 xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
                   xmlns:toolkit="http://schemas.microsoft.com/dotnet/2022/maui/toolkit"
                 x:Class="MauiPopWindow.SimplePopup"
                 >
        <VerticalStackLayout>
            <Label
                Text="Welcome to .NET MAUI!"
                VerticalOptions="Center"
                HorizontalOptions="Center" />
            <Entry x:Name="myEntry"></Entry>
            <Button Text="OK"
                    Clicked="OnOKButtonClicked" />
        </VerticalStackLayout>
    </toolkit:Popup>
    

    Here is popup's background code. When you click the Ok button. this popup will be closed and transfer data to previous page.

    using CommunityToolkit.Maui.Views;
    namespace MauiPopWindow;
    
    public partial class SimplePopup : Popup
    {
        public SimplePopup()
        {
            InitializeComponent();
        }
        void OnOKButtonClicked(object? sender, EventArgs e) => Close(myEntry.Text);
    }
    

    If I popup up in the mainPage's button click event, then I can get the value from the popup window by var result = await this.ShowPopupAsync(popup);

    private async void OnCounterClicked(object sender, EventArgs e)
        {
            var popup = new SimplePopup();
    
           var result = await this.ShowPopupAsync(popup);
        }
    

    By the way, if you have only one entry to get data, you can use string result = await DisplayPromptAsync("Question 1", "What's your name?"); directly by display a prompt

    Best Regards,

    Leon Lu


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.