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?
- I have read the “Reporting a bug” section on Contributing file: https://github.com/CommunityToolkit/Maui/blob/main/CONTRIBUTING.md#reporting-a-bug
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
- Open and run sample project (android)
- 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)
@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
MyPopup.xaml
MyPopupViewmodel.cs
MyPage.xamls.cs
@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 😃