Solved the issue with TaskCompletionSource. I hope this solution is better than writing code-behind and using MessagingCenter.
Example:
PopupService
private async Task<object> InternalShowPopupAsync(Type viewModelType, object parameter)
{
Xamarin.CommunityToolkit.UI.Views.Popup popup = CreatePopupView(viewModelType); // or just Popup View object
App.Current.MainPage.Navigation.ShowPopup(popup);
var result = await (popup.BindingContext as BaseViewModel).InitializeWithReturnAsync(parameter);
popup.Dismiss(null);
return result;
}
Popup ViewModel
private TaskCompletionSource<object> _taskCompletion;
public override async Task<object> InitializeWithReturnAsync(object parameter = null)
{
_taskCompletion = new TaskCompletionSource<object>();
return await _taskCompletion.Task;
}
public ICommand TestCommand => new Command(() => _taskCompletion.TrySetResult("Value from popup"));