question

EmmanuelMorrison-3020 avatar image
0 Votes"
EmmanuelMorrison-3020 asked JarvanZhang-MSFT answered

Xamamrin.forms shell navigation with data

 namespace shell.ViewModels
 {
     [QueryProperty("Content", "Content")]
     public class ProfilePageViewModel : BindableObject
     {
         string content = "";
         public string Content
         {
    
    
             get => content;
             set
             {
                 content = Uri.UnescapeDataString(value ?? string.Empty);
                 OnPropertyChanged();
                 PerformOperation(content);
             }
         }
    
    
         private string firstname { get; set; }
         public string FirstName
         {
             get { return firstname; }
             set
             {
                 firstname = value;
                 OnPropertyChanged();
             }
         }
         private string lastname { get; set; }
         public string LastName
         {
             get { return lastname; }
             set
             {
                 lastname = value;
                 OnPropertyChanged();
             }
         }
         private string imageurl { get; set; }
         public string ImageURL
         {
             get { return imageurl; }
             set
             {
                 imageurl = value;
                 OnPropertyChanged();
             }
         }
         private string price { get; set; }
         public string Price
         {
             get { return price; }
             set
             {
                 price = value;
                 OnPropertyChanged();
             }
         }
    
         private void PerformOperation(string Profile)
         {
             var content = JsonConvert.DeserializeObject<Model>(Profile);
             firstname = content.FirstName;
             lastname = content.LastName;
            imageurl = content.ImageURL;
            price = content.Price;
         }
       
         public ProfilePageViewModel()
         {
         }
     }
 }
    
 private async void ProfileSelect(object sender, SelectionChangedEventArgs e)
         {
             var content = e.CurrentSelection.FirstOrDefault() as Model;
             if(content == null)
             {
                 return;
             }
             else
             {
                   
                 await Shell.Current.GoToAsync($"{nameof(GroomersProfilePage)}?Content={content.FirstName}" );
             }
         }

no error and nothing shows. i dont know what i am doing wrongly

dotnet-xamarin
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

JarvanZhang-MSFT avatar image
0 Votes"
JarvanZhang-MSFT answered

Hello,​

Welcome to our Microsoft Q&A platform!

If you decorate the QueryPropertyAttribute for the model class, the model class must be the the page's BindingContext. Please make sure you've set the 'ProfilePageViewModel' as the BindingContext of 'GroomersProfilePage'.

I tested a basic demo, it works as expected. Here is the sample code, you could refer to it:

public partial class Page1 : ContentPage
{
    public Page1()
    {
        InitializeComponent();
    }
    private void Button_Clicked(object sender, EventArgs e)
    {
        Shell.Current.GoToAsync($"Page2?Content={"test_name"}");
    }
}

public partial class Page2 : ContentPage
{
    public Page2()
    {
        InitializeComponent();
        BindingContext = new TestModel();
    }
}
<ContentPage ...             
    x:Class="App19F_Shell2.Page2">
    <ContentPage.Content>
        <StackLayout>
            <Label x:Name="label" Text="{Binding Content}"/>
        </StackLayout>
    </ContentPage.Content>
</ContentPage>

The model class

[QueryProperty(nameof(Content), "Content")]
public class TestModel : INotifyPropertyChanged
{
    private string content;
    public string Content
    {
        get
        {
            return content;
        }
        set
        {
            if (content != value)
            {
                //content = value;
                content = Uri.UnescapeDataString(value ?? string.Empty);
                NotifyPropertyChanged();
            }
        }
    }
    protected virtual void NotifyPropertyChanged([CallerMemberName] string propertyName = "")
    {
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
    }
    public event PropertyChangedEventHandler PropertyChanged;
}


Best Regards,

Jarvan Zhang


If the response is helpful, please click "Accept Answer" and upvote it.

Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.