-
Notifications
You must be signed in to change notification settings - Fork 416
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[BUG] PopupService ignores the specified ViewModel instance #1733
Comments
We've decided that this method was a bit of a mistake and will likely be removed from future versions. What you can do is follow the pattern from this example: So it is up to you as a developer to set the BindingContext. |
@bijington Not sure to understand why it has to be removed. |
You can achieve this through the popuoService.Show<MessageViewModel>(viewModel => viewModel.Message = "This is how you do it"); |
@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 |
@urielginsburg the documentation that you linked to doesn't refer to the method that will be removed. What it does do is refer to our example application that shows how to achieve your scenario. I understand that your solution works but it isn't very DI friendly, an alternative approach (based on our sample) would be to do the following): MauiProgram.csbuilder.UseMauiCommunityToolkit()
builder.Services.AddTransientPopup<MyPopup, MyPopupViewmodel>();
builder.Services.AddTransient<MainPage>(); 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"
x:DataType="local:MyPopupViewModel"> <!-- use this for better validation of your bindings and a slight performance boost -->
<VerticalStackLayout >
<Label Text="{Binding Message}"/>
</VerticalStackLayout>
</toolkit:Popup> MyPopup.xaml.cspublic partial class MyPopup : Popup
{
public MyPopup(MyPopupViewModel viewModel) // Let DI create the view model instance for you
{
InitializeComponent();
BindingContext = viewModel;
}
private async void OnButtonClicked(object sender, EventArgs e)
{
var res = await MyPopupViewmodel.ShowAsync("message", CancellationToken.None);
}
} MyPopupViewModelpublic 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)));
}
}
} MainPage.xaml.cspublic partial class MainPage : ContentPage
{
readonly IPopupService _popupService;
public MainPage(IPopupService popupService)
{
InitializeComponent();
_popupService = popupService;
}
private async void OnButtonClicked(object sender, EventArgs e)
{
var res = await _popupService.ShowAsync<MyPopupViewModel>(viewModel => viewModel.Message = "message");
}
} |
@bijington I apologize for misunderstanding... what method is going to be removed? |
It's quite subtle but the OPs issue was with |
@bijington There should be a way to pass in a preconfigured instance of the Popup viewmodel from the pages viewmodel so all the popup data can be set at display time instead of the popup having an empty Popup viewmodel. |
That was my original intention but that functionality was removed before it merged. Does the |
@bijington It does not help if you need properties set inside the Popup constructor before it appears since the viewmodel injected is empty. I guess this is only a problem when opening from page viewmodel. But that is really where I open all my Popups not from inside the page code behind. The |
@Orgbrat Sorry I don't follow why you need to use the Opened event, can't you use bindings to the view model? I'm keen to discuss so we make sure the toolkit offers enough |
@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 Thanks for the conversation and wake-up of why you doing that.... |
Thank you for the discussion also. I'm glad you resolved the issue. As for the why I wonder if it is caused by the same issue as #1887 which appears to be a timing issue on Windows |
@bijington I know we talked about adding this popup service when it was just a thought from you. But I am in the process of migrating my custom popup DialogService to the CommunityToolkit.Maui PopupService so I will let you know if all the issues are handled and I hope this windows timing issue does not rear it's ugly head. Charge... |
Best of luck! |
Is there an existing issue for this?
Did you read the "Reporting a bug" section on Contributing file?
Current Behavior
When using the method
ShowPopupAsync(viewmodel)
fromPopupService
, PopupService ignores the specified ViewModel and raises anInvalidOperationException
duringValidateBindingContext()
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
Link to public reproduction project repository
https://github.com/elparasite/mauipopupserviceissue
Environment
Anything else?
No response
The text was updated successfully, but these errors were encountered: