-
Notifications
You must be signed in to change notification settings - Fork 409
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
PopupService #1165
PopupService #1165
Conversation
Any updates on this? |
No I haven't been able to determine what the best option is for |
As an alternative to requiring an interface when presenting the popup for the view model to receive data we could try something like this: public Task<object?> ShowPopupAsync<TViewModel>(Action<TViewModel> onPresenting) where TViewModel : INotifyPropertyChanged
{
ArgumentNullException.ThrowIfNull(onPresenting);
var popup = GetPopup(typeof(TViewModel));
var viewModel = GetViewModel<TViewModel>();
popup.BindingContext = viewModel;
onPresenting.Invoke(viewModel);
return CurrentPage.ShowPopupAsync(popup);
} The It could then be used as follows: public class MyViewModel : INotifyPropertyChanged
{
public event PropertyChangedEventHandler? PropertyChanged;
public void LoadData(int id)
{
}
}
void Show()
{
ShowPopupAsync<MyViewModel>(onPresenting: viewModel => viewModel.LoadData(123));
} What do people think? Is it clean or a bit yucky? |
I like it, but make it Func<ViewModel, Task> |
Co-authored-by: Pedro Jesus <[email protected]>
…yToolkit/Maui into feature/sl/981-add-popupservice
…yToolkit/Maui into feature/sl/981-add-popupservice
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks Shaun!! I'm excited to see PopupService finally become a reality!!
I'm going to merge this right away so that we can squeeze it into this week's release.
Could you prioritize getting the PopupService docs done before Monday so that PopupService doesn't go too long without proper documentation?
FYI - I made a small tweak, removing PopupService's functionality that automatically assigned BindingContexts. I renamed the method to ValidateBindingContext
(below) to ensure that the user has properly assigned the Popup View's BindingContext. However, I think it's out-of-scope for the PopupService to automatically assign it for them.
For example, .NET MAUI doesn't automatically assign a ContentPage's BindingContext when a user passes it into IServiceCollection
.
I'm happy to circle back to this discussion in the future if we get requests from the community. But I agree that PopupService should be opinionated, and it should require the correct BindingContext. And us throwing an InvalidOperationException
is enough to inform the dev of the requirement, in lieu of doing it for them under the hood (which they may not expect/want).
static void ValidateBindingContext<TViewModel>(Popup popup, out TViewModel bindingContext)
{
if (popup.BindingContext is not TViewModel viewModel)
{
throw new InvalidOperationException($"Unexpected type has been assigned to the BindingContext of {popup.GetType().FullName}. Expected type {typeof(TViewModel).FullName} but was {popup.BindingContext?.GetType().FullName?? "null"}");
}
bindingContext = viewModel;
}
Description of Change
Linked Issues
PR Checklist
approved
(bug) orChampioned
(feature/proposal)main
at time of PRAdditional information