How do I return a value from a popup with VS 2026 in a maui app
15:51 05 Jun 2026

I'm trying to return a string from my popup. The code-behind I have looks like this:

public partial class SelectDb : PopupPage
{
    SelViewModel Sv;
    TaskCompletionSource taskCompSrc;
    public Task PopupDismissed => taskCompSrc.Task;
    public string RetVal { get; set; } = string.Empty;
}

    protected async override void OnAppearing()
    {
        base.OnAppearing();
        taskCompSrc = new TaskCompletionSource();
    }

    protected override void OnDisappearing()
    {
        base.OnDisappearing();
        taskCompSrc.SetResult(RetVal);
        }

And when I start my program and try to open the popup I get an exception throw that looks like this:

System.NullReferenceException: 'Object reference not set to an instance of an object.'

taskCompSrc was null.

So what is going wrong and how can it be fixed?

c# popup maui code-behind