Beryl
Beryl

Reputation: 1

MAUI Shell Navigation with parameter not binding

I am new to Stackoverflow, and embracing MAUI currently.

My problem is, I am navigating to a new page using Shell navigation, and passing a Text parameter, however I cannot seem to retrieve the passed string on the other end.

Code which does the navigation

await Shell.Current.GoToAsync($"{nameof(ReportingPage)}?Text=Testing");

My ViewModel for the target page (ReportingPage)

[QueryProperty("Text", "Text")]
    public partial class ReportingViewModel : ObservableObject
    {
        [ObservableProperty]
        string text;  
    }

My Page (ReportingPage) Neither of these labels bind to the 'Text'.

public class ReportingPage : ContentPage
{
    public ReportingPage(ReportingViewModel viewModel)
    {
        BindingContext = viewModel;

        Label label = new Label();
        label.SetBinding(Label.TextProperty, "Name");

        Label label2 = new Label();
        label2.Text = viewModel.Text;

        Content = new Grid()
        {
            label,
            label2
        };
   }
}

Tried following Microsoft guidance: https://learn.microsoft.com/en-us/dotnet/maui/fundamentals/shell/navigation

But unable to get any further.

Also tried passing a dictionary but for testing purposes just doing simple Shell navigation with a text value.

Upvotes: 0

Views: 611

Answers (1)

Jianwei Sun - MSFT
Jianwei Sun - MSFT

Reputation: 4332

As Jason said that you should bind Text instead of Name.

You can refer to the official doc about BindableObjectExtensions.SetBinding Method:

public class PersonViewModel
{
    public string Name { get; set; }
    public string Company { get; set; }
}

// ...

var vm = new PersonViewModel {
    Name = "John Doe", 
    Company = "Xamarin"
}

var label = new Label ();
label.SetBinding (Label.TextProperty, "Name"); // "Name" is the property on the view model
label.BindingContext = vm;

Debug.WriteLine (label.Text); // prints "John Doe"

Upvotes: 0

Related Questions