1

I tried to follow the Microsoft tutorial on passing data from the calling ViewModel to the Popup ViewModel on the following website: https://learn.microsoft.com/en-us/dotnet/communitytoolkit/maui/views/popup

Nonetheless, I don't understand exactly how I can pass data. My question: Does anybody know, how I may pass e.g. string or integer data? Do I need to use ApplyQueryAttributes?

I thought I could pass data like similar when using NavigateToAsync with QueryAttributes, e.g. NavigationService.NavigateToAsync($"DestinationView?userId={User.ID}"); but it does not seem that trivial.

How may I perform that?

2
  • When you call the CloseAsync method you can pass your data in it, as the first argument, it accepts objects and will pretty much accept anything Commented May 23, 2024 at 15:02
  • 1
    Yes, the CloseAsync method seems to be useful for passing data from the PopUpViewModel to the calling ViewModel, but I also wanted to pass data from the calling ViewModel to the PopUpViewModel. Commented May 24, 2024 at 10:37

2 Answers 2

0

this is covered in the docs

this.popupService.ShowPopup<MyPopupViewModel>(onPresenting: viewModel => viewModel.CallSomeMethod(10));

or

this.popupService.ShowPopup<MyPopupViewModel>(onPresenting: viewModel => viewModel.SomeProperty = 10);
Sign up to request clarification or add additional context in comments.

Comments

0

Here

Passing data to a Popup view model

We do indeed need to implement IQueryAttributable on our popup's view model:

public class NamePopupViewModel : ObservableObject, IQueryAttributable
{
    [ObservableProperty]
    public partial string Name { get; set; } = "";

    public void ApplyQueryAttributes(IDictionary<string, object> query)
    {
        Name = (string)query[nameof(NamePopupViewModel.Name)];
    }
}

And in our calling view model:

public class MyViewModel : INotifyPropertyChanged
{
    private readonly IPopupService popupService;

    public MyViewModel(IPopupService popupService)
    {
        this.popupService = popupService;
    }

    public async Task DisplayPopup()
    {
        var queryAttributes = new Dictionary<string, object>
        {
            [nameof(NamePopupViewModel.Name)] = "Shaun"
        };

        await this.popupService.ShowPopupAsync<NamePopupViewModel>(
            Shell.Current,
            options: PopupOptions.Empty,
            shellParameters: queryAttributes);
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.