Skip to content

[BUG] Popup not sizing to contents when onPresenting is used. #1887

@urielginsburg

Description

@urielginsburg

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

Issue

When displaying a popup using IPopupService.ShowPopup (or ...Async, doesn't matter), the popup is displayed empty (you can't see anything, but the main page becomes disabled and becomes enabled again after you click on it anywhere.
For example (based on the default MAUI program template in VS 2022):

MyPopup.xaml

<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"
               Color="Transparent"
               x:Class="MauiApp2.MyPopup">
    <VerticalStackLayout>
        <CollectionView ItemsSource="{Binding Messages}">
            <CollectionView.ItemTemplate>
                <DataTemplate>
                    <Label Text="{Binding}"/>
                </DataTemplate>
            </CollectionView.ItemTemplate>
        </CollectionView>
    </VerticalStackLayout>
</toolkit:Popup>

MyPopup.xaml.cs

public partial class MyPopup : Popup
{
	public MyPopup(MyPopupViewmodel viewmodel)
	{
		BindingContext = viewmodel;
		InitializeComponent();
	}
}

MyPopupViewmodel.cs

public class MyPopupViewmodel : INotifyPropertyChanged
{
    private ObservableCollection<string>? _messages;

    public event PropertyChangedEventHandler? PropertyChanged;

    public ObservableCollection<string>? Messages
    {
        get => _messages;
        set
        {
            _messages = value;
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(Messages)));
        }
    }
}

MainPage.xaml.cs

    private void OnButtonClicked(object sender, EventArgs e)
    {
        _popupService.ShowPopup<MyPopupViewmodel>(viewmodel => viewmodel.Messages = new System.Collections.ObjectModel.ObservableCollection<string>(new[] { "Hello, World!" }));
    }

The result is this:
image

Additional information

  1. Setting the messages in the popup's constructor and using ShowPopup without using onPresenting resolves the issue e.g.,
	public MyPopup(MyPopupViewmodel viewmodel)
	{
		BindingContext = viewmodel;
		viewmodel.Messages = new System.Collections.ObjectModel.ObservableCollection<string>
		{
			"Hello world",
		};
		InitializeComponent();
	}
  1. Alternativoodooly, playing around with MinimumWidthRequest and MinimumHeightRequest properties on the StackLayout in the popup to any value larger than 1 also resolves the issue and ShowPopup can be used with setting the content in onPresenting.

### Expected Behavior

The popup should display correctly when using `ShowPopup` with `onPresenting`.

### Steps To Reproduce

1. Create a new MAUI program using the default template in VS 2022.
2. Add the MAUI CommunityToolkit and a popup as described in this post.

### Link to public reproduction project repository

https://github.com/urielginsburg/maui-communitytoolkit-popup-issue

### Environment

```markdown
- .NET MAUI CommunityToolkit: 9.0.0
- OS: Windows 11
- .NET MAUI: 8.0.41

Anything else?

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    area/viewsIssue/Discussion/PR that has to do with ViewsbugSomething isn't workingunverified

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions