IPopupService: Show popup overload taking view model does not use it. No support for Popup instances? #1878
Replies: 4 comments 4 replies
-
I originally added that method to enable the support you desire but decisions went against that approach and I understand the reasons why. I do have a desire to implement the ability to just show a Popup without an associated view model. Would this solve your issue? |
Beta Was this translation helpful? Give feedback.
-
I don't understand why you want prevent an instance of Popup being created external to the service. The current implementation I went with works ok and won't be detrimental to the project, its just not as good as it could have been. I will add some more details for what I was thinking. I have a IDialogService that is injected into page viewmodels. This is for showing alert and confirmation dialogs. So for the alert dialog all it needs is three strings: a title, mesage and button text. Currently working implementation: public Task ShowAlertDialogAsync(string title, string message)
{
return popupService.ShowPopupAsync<DialogAlertModel>(onPresenting: model =>
{
model.Title = title;
model.Message = message;
model.ButtonText= _okButtonCaption;
});
}
public sealed partial class DialogAlertModel : ObservableObject
{
[ObservableProperty] private string _title;
[ObservableProperty] private string _message;
[ObservableProperty] private string _buttonText;
} I wanted to do something like the following.
public sealed class DialogAlertModel(string title, string message, string buttonText)
{
public string Title { get; } = title;
public string Message { get; } = message;
public string ButtonText { get; } = buttonText;
} Your overload public Task ShowAlertDialogAsync(string title, string message)
{
return popupService.ShowPopupAsync(
new DialogAlertModel(title, message, _okButtonCaption)
);
} Maybe the caller could do this instead public Task ShowAlertDialogAsync(string title, string message)
{
return popupService.ShowPopupAsync(new DialogAlertPopup(
new DialogAlertModel(title, message, _okButtonCaption)
));
} |
Beta Was this translation helpful? Give feedback.
-
Is this discussion stale? can it be closed? |
Beta Was this translation helpful? Give feedback.
-
I have the exact same usecase and wondered, that my allready created ViewModel is not used for the Popup. In my opinion, the method public Task<object?> ShowPopupAsync<TViewModel>(CancellationToken token = default (CancellationToken))` should create a new instance of TViewModel via dependency injection, but the following: public Task<object?> ShowPopupAsync<TViewModel>(TViewModel viewModel, CancellationToken token = default (CancellationToken))` must use my instance of TViewModel. So I was a bit surprised, when I looked at the current implementation. If we don't want a given instance (and want to create one via DI) , we can create an instance via ActivatorUtilities, which uses DependencyInjection via IServiceProvider and fill the missing Constructor Arguments with the given params: public static object CreateInstance (IServiceProvider provider, Type instanceType, params object[] parameters); This would result in something like this: public Task<object?> ShowPopupAsync<TViewModel>(params object[] constructorParameters, CancellationToken token = default (CancellationToken)) where TViewModel : INotifyPropertyChanged
{
Popup popup = this.GetPopup(typeof (TViewModel), constructorParameters);
PopupService.ValidateBindingContext<TViewModel>(popup, out TViewModel _);
return PopupService.ShowPopupAsync(popup, token);
}
private Popup GetPopup(Type viewModelType, params object[] constructorParameters)
{
if ( ActivatorUtilities.GetService((this.serviceProvider, PopupService.viewModelToViewMappings[viewModelType], constructorParameters) is Popup service)
return service;
[..]
} |
Beta Was this translation helpful? Give feedback.
-
I was trying to open a popup with this overload, passing an app-created (non resolved) view model instance.
Task<object?> ShowPopupAsync<TViewModel>(TViewModel viewModel, CancellationToken token)
This is useful because the viewmodel can be immutable, and therefore its ctor can be in charge of enforcing its state, its properties can have OneTime bindings, and no need for property changed.
All that seems to happen with this overload is an arg null check. What is the point of it then?
So then I was forced make a fully mutable view model and use the
Action<TViewModel> onPresenting
to set the model up.When I saw the model instance overload did not use the instance, I thought about just newing up the Popup and passing my view model into its ctor, but IPopupService doesn't support showing a raw Popup instance and expects to resolve the popup and viewmodel pair.
It seems all that is needed to support that is make the following public:
private static Task<object?> ShowPopupAsync(Popup popup, CancellationToken token)
Beta Was this translation helpful? Give feedback.
All reactions