Continued: Popup V2 does not allow any way of passing data to a popup anymore if you are not using Shell #2696
-
Continuing discussions off the back of this issue #2693 since it was closed but feel like there is some confusion around the new V2 Popups, at least for me. So prior to the new V2 Popups I was previously doing something like this: popupService.ShowPopup<ProgressPopupViewModel>(onPresenting: async progressViewModel =>
{
// Sets a BindableProperty for the progress popup's label
progressViewModel.ProgressText = localizationResourceManager.GetValue("LoggingInText");
// My Long running Task here when the Progress Popup is showing
// Closes the Popup when Task is completed above
progressViewModel.CloseCommand.Execute(default);
}); Having spent time now looking at how to migrate the above to V2 Popups I don't see a way that the plugin supports it very well in the same manner as before since overload methods for That being said however I've introduced a property called public partial class ProgressPopupViewModel : ObservableObject
{
public Func<Task>? ProgressFunc;
[ObservableProperty]
private string _progressText = string.Empty;
} Follows this example: https://github.com/CommunityToolkit/Maui/blob/main/samples/CommunityToolkit.Maui.Sample/Views/Popups/UpdatingPopup.xaml.cs for how I call it. Opened += async (_, _) =>
{
if (progressPopupViewModel.ProgressFunc is not null)
{
await progressPopupViewModel.ProgressFunc().ConfigureAwait(false);
}
}; Ideally if I can set I suppose the question for me becomes, assuming I have the right approach, what will the following look like so IPopupResult popupResult = await popupService.ShowPopupAsync<ProgressPopupViewModel>().ConfigureAwait(true); Since this is not a Shell App the Would be nice to get some further guidance on these breaking changes for use cases like ours as we try navigate around these changes. Thanks! |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 13 replies
-
Because Popup inherits from View you can now set BindingContext on the Popup. e.g. using CommunityToolkit.Maui.Extensions;
using CommunityToolkit.Maui.Markup;
using CommunityToolkit.Maui.Views;
using CommunityToolkit.Mvvm.ComponentModel;
// ...
public async Task ShowPopup()
{
var popup = new Popup();
popup.BindingContext = new ProgressPopupViewModel() { ProgressText = $"LoggingInText" };
popup.BackgroundColor = Colors.Green;
popup.Content = new VerticalStackLayout
{
Children =
{
new Label { TextColor = Colors.White }.Bind(Label.TextProperty, static (ProgressPopupViewModel ctx) => ctx.ProgressText),
new Button { Text = "Close", Command = new Command(async () => { await popup.CloseAsync(); }) }
}
};
popup.Opened += Popup_Opened;
await this.ShowPopupAsync(popup);
}
private async void Popup_Opened(object? sender, EventArgs e)
{
await Task.Delay(1000); // simulate a progress delay
if (sender is Popup popup && popup.BindingContext is ProgressPopupViewModel viewModel)
{
viewModel.ProgressText = "Opened";
}
} N.B. I noticed you're still using the older MVVM syntax. Check out the blog https://devblogs.microsoft.com/dotnet/announcing-the-dotnet-community-toolkit-840/ and the corresponding new syntax (which has compiled-bindings, xmldoc advantages): [ObservableProperty]
public string ProgressText { get; set; } = string.Empty; |
Beta Was this translation helpful? Give feedback.
-
I'm stuck in the same situation. My previous implementation was working like a charm... :(
Hope there will be some new ways to handle this problem... i can't bump CommunityToolkit version anymore. |
Beta Was this translation helpful? Give feedback.
-
I'm encountering a similar issue. Without using Shell, there currently appears to be no straightforward way to pass data to a popup's ViewModel via
—but one that accepts While using Since the Community Toolkit is such a critical part of the .NET MAUI ecosystem, we would appreciate a more gradual or backward-compatible approach to breaking changes. One potential solution could be adopting a "Versioning Design Pattern" that provides continuity for legacy implementations while supporting new paradigms. Thank you for your hard work and for considering this feedback. Best regards, |
Beta Was this translation helpful? Give feedback.
There are many ways to pass data into your Popup, using PopupService + Dependency Injection, using DataBinding, using ViewModels, using Messaging Services, subscribing to events like
Popup.Opened
Popup.Closed
View.Loaded
,View.Unloaded
,View.ChildAdded
,View.PropertyChanged
etc.Popup is now just a
View
and should be thought of and implemented like any otherView
in .NET MAUI. For example, you will pass data into Popup the same way you would pass data into another ContentPage, Button, Label, etc.