0

I have a Maui app where I use MVVM pattern with MAUI Toolkik and also trying with Mopup plugin but I haven't found how to pass objects to Popup pages combined with MVVM. At the moment, I have a page, which I use to navigate to the PopupPage successfully and also I am able to connect the PopupPage with its viewmodel. What I am unable to do is to pass any kind of object to the PopupPage.

I have tried to set the PopupPage constructor with parameters but the methods to navigate to the PopupPage only recognize parameters setted on the code behind.

Here is my code:

Popup

<mopup:PopupPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
         xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
         x:Class="NewScholarApp.Views.MessagePopup"
         xmlns:toolkit="http://schemas.microsoft.com/dotnet/2022/maui/toolkit"
           xmlns:mopup="clr-namespace:Mopups.Pages;assembly=Mopups"
           xmlns:viewmodels="clr-namespace:NewScholarApp.ViewModels"
            x:DataType="viewmodels:MessagePopupViewModel">
<mopup:PopupPage.BindingContext>
    <viewmodels:MessagePopupViewModel/>
</mopup:PopupPage.BindingContext>

<VerticalStackLayout BackgroundColor="White" HorizontalOptions="Center" VerticalOptions="Center" HeightRequest="100" WidthRequest="100">
    <Label 
        Text="{Binding Message}"
        VerticalOptions="Center" 
        HorizontalOptions="Center" />
</VerticalStackLayout>
</mopup:PopupPage>

I use this to navigate from my page viewmodel

await _popupNavigation.PushAsync(new MessagePopup(string text = "tex"));

If I try to set a parameter, shows this error, even that in my PopupPage constructor I have setted a parameter

"MessagePopup does not contain a a constructor that contains 1 argument"

**MessagePopupViewModel **

public partial class MessagePopupViewModel : ObservableObject
{
    #region AnP
    [ObservableProperty]
    private string message;

    private readonly IApiService _apiService;
    #endregion

    public MessagePopupViewModel(string tex)
    {
        Message = tex;
    }
}
  • 1
    have you added a constructor to `MessagePopup` that accepts one parameter? The code you posted shows the constructor for the VM, not the page – Jason Jan 05 '23 at 20:34
  • Add to question the code where `MessagePopup` creates and/or refers to a `MessagePopupViewModel`. You'll need to do something like `public MessagePopup(string tex) { ... new MessagePopupViewModel(tex); ... }`. To get the exact details you need, show what you do now. – ToolmakerSteve Jan 05 '23 at 23:20
  • @Jason I didn't add the code behind of the page because I don´t want that, I need to receive the parameter in the viewmodel not in the code behind of the page. – Luis Guzman Jan 06 '23 at 14:29
  • you're navigating to the **page**, not the VM, so you can only pass a parameter to the page. The page can then pass the parameter to the VM. – Jason Jan 06 '23 at 14:31
  • @ToolmakerSteve Actually, there is the code, the first section of code that I posted that is the XAML of the PopupPage is where I connect the page to the viewmodel. Also, the last section of code that is the constructor of the PopupViewmodel is where I have the constructor with parameter: public MessagePopupViewModel(string tex). In my navigation: await _popupNavigation.PushAsync(new MessagePopup()); I didn´t put the parameter because it shows an error is what I explained in this: "If I try to set a parameter, shows this error, even that in my PopupPage constructor I have setted a parameter" – Luis Guzman Jan 06 '23 at 14:31
  • please **show that code**. What you are describing is a very basic C# error and should be trivial to fix, but we need you to show the code that is generating it. And [edit] your question to include relevant code, do not stuff it into a comment – Jason Jan 06 '23 at 14:41
  • @Jason Maybe I am not explaining well, all my code is already posted in the question, I don´t have anything in the code behind of the PopupPage, please tell me what do you want me to add. The code that is generating the error is this: await _popupNavigation.PushAsync(new MessagePopup()); what is already added in the origial question. – Luis Guzman Jan 06 '23 at 14:58
  • Ok, I didn't notice the `BindingContext` xaml. Since you need to pass `tex` from `MessagePopup` to `MessagePopupViewModel, you can't do that in xaml. Jason's answer shows what to do instead. – ToolmakerSteve Jan 06 '23 at 22:54

1 Answers1

1

you are navigating using this code

await _popupNavigation.PushAsync(new MessagePopup(string text = "tex"));

so MessagePopup MUST have a constructor that accepts a parameter

public MessagePopup(string somevalue)

if you also want to pass that value to your VM, then you can add

BindingContext = new MessagePopupViewModel(somevalue);

if you do this, then you should remove the BindingContext property from the XAML

Jason
  • 86,222
  • 15
  • 131
  • 146