Maui: [BUG] PopupService ignores the specified ViewModel instance

Is there an existing issue for this?

  • I have searched the existing issues

Did you read the “Reporting a bug” section on Contributing file?

Current Behavior

When using the method ShowPopupAsync(viewmodel) from PopupService, PopupService ignores the specified ViewModel and raises an InvalidOperationException during ValidateBindingContext() if the constructor of the PopUp doesn’t accept injection of the viewmodel the ViewModel.

Even if the constructor sets the view model, the specified view model won’t be used.

Expected Behavior

The method ShowPopupAsync(viewmodel) should set the specified ViewModel before Validating the binding context.

Steps To Reproduce

  1. Open and run sample project (android)
  2. Click on “Open Popup”

Link to public reproduction project repository

https://github.com/elparasite/mauipopupserviceissue

Environment

- .NET MAUI CommunityToolkit: 7.0.1
- OS: Android API 26
- .NET MAUI: 8.0.7

Anything else?

No response

About this issue

  • Original URL
  • State: closed
  • Created 3 years ago
  • Comments: 15 (8 by maintainers)

Most upvoted comments

@bijington Well I guess I was making it harder than I should have. Removed the Opened event and added a binding to the Popup Xaml top definition Size="{Binding PopupSize}" and it solved my issue and it also solved the discussion I opened “Windows Popup Location When Opened From ViewModel #1919”. Although I am not sure why 😃

Thanks for the conversation and wake-up of why you doing that…

@dartasen I also ran into this and indeed the community toolkit code does not assign a viewmodel instance to the bindingcontext of the popup. I also agree this pattern seems convenient and would be nice to have it working. It can be made to work in this way:

MauiProgram.cs

builder.UseMauiCommunityToolkit()
builder.Services.AddTransientPopup<MyPopup, MyPopupViewmodel>();

MyPopup.xaml

<?xml version="1.0" encoding="utf-8" ?>
<toolkit:Popup xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
               xmlns:toolkit="http://schemas.microsoft.com/dotnet/2022/maui/toolkit"
               xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
               xmlns:local="clr-namespace:MauiApp2"
               x:Class="MauiApp.MyPopup">
    <!-- Instantiate the viewmodel -->
    <toolkit:Popup.BindingContext>
        <local:MyPopupViewmodel/>
    </toolkit:Popup.BindingContext>
    <VerticalStackLayout >
        <Label Text="{Binding Message}"/>
    </VerticalStackLayout>
</toolkit:Popup>

MyPopupViewmodel.cs

public class MyPopupViewmodel : INotifyPropertyChanged
{
    private string _message = string.Empty;

    public event PropertyChangedEventHandler? PropertyChanged;

    public string Message
    {
        get => _message;
        set
        {
            _message = value;
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(Message)));   
        }
    }

    public static async Task<object?> ShowAsync(string message, CancellationToken cancellationToken)
    {
        // This is for Windows. For other platforms, you're going to need different code e.g., for MacCatalyst
        // it's going to be IPlatformApplication.Current.Services , etc.
        var popupService = MauiWinUIApplication.Current.Services.GetRequiredService<IPopupService>();
        return await popupService.ShowPopupAsync<MyPopupViewmodel>(viewModel => viewModel.Message = message, cancellationToken);
    }
}

MyPage.xamls.cs

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

    private async void OnButtonClicked(object sender, EventArgs e)
    {
        var res = await MyPopupViewmodel.ShowAsync("message", CancellationToken.None);
    }
}

@bijington If this is going to be taken out of the framework, it may be a good idea to take it out of the documentation, or at least add a big warning about it there. That could probably save a lot of people a lot of time 😃