Forum Xamarin.Forms
We are excited to announce that the Xamarin Forums are moving to the new Microsoft Q&A experience. Q&A is the home for technical questions and answers at across all products at Microsoft now including Xamarin!

We encourage you to head over to Microsoft Q&A for .NET for posting new questions and get involved today.

how to output data from one page to another from the collection of the selected item?

kingston5555kingston5555 Member ✭✭✭

I have two XAML pages and one of their common ViewModel page.I want to output data from one page to another from the collection of the selected item. It must be Labels Text. I have a problem If I bind Labels Text to a variable. I can see data only on the current page. But if I go to another page and place the same label there, the information is not displayed. I do not understand why so because on the next page the same variable which already contains data

https://stackoverflow.com/questions/63623080/how-to-output-data-from-one-page-to-another-from-the-collection-of-the-selected?noredirect=1#comment112507636_63623080

I posted my question there. Please help. There you can see code and problem description.First problem I solved.Please help me to output data from one page to another from the collection of the selected item

Best Answer

  • jezhjezh Member, Xamarin Team Xamurai
    Accepted Answer

    If you want to navigation to another in VM , you need to pass the current navigation from current page.
    You can refer to the following code:

    In TryPageCS.cs

        INavigation Navigation { get; set; }
    
        public Command navigateCommand { get; set; }
    
      public TryPageCS(INavigation navigation)
        {
            this.Navigation = navigation;
            this.navigateCommand = new Command(async () => await GotoPage2());
          }
    
           public async Task GotoPage2()
        {
           await Navigation.PushModalAsync(new MainPage());
        }
    

    TryPage.xaml.cs

        public partial class TryPage : ContentPage
        {
            public TryPage()
            {
                InitializeComponent();
    
                BindingContext = new TryPageCS(this.Navigation);
            }
        }
    

    A simple usage in xaml

      <Button  Text="Navigate" Command="{Binding navigateCommand }"></Button>
    

Answers

  • LardeCristoLardeCristo Member ✭✭

    Hey, If you use the same VM (cs) both XAML, it´s possible create a variable Static.
    For example:

    private static string Name {get; set;}

  • jezhjezh Member, Xamarin Team Xamurai
    Accepted Answer

    If you want to navigation to another in VM , you need to pass the current navigation from current page.
    You can refer to the following code:

    In TryPageCS.cs

        INavigation Navigation { get; set; }
    
        public Command navigateCommand { get; set; }
    
      public TryPageCS(INavigation navigation)
        {
            this.Navigation = navigation;
            this.navigateCommand = new Command(async () => await GotoPage2());
          }
    
           public async Task GotoPage2()
        {
           await Navigation.PushModalAsync(new MainPage());
        }
    

    TryPage.xaml.cs

        public partial class TryPage : ContentPage
        {
            public TryPage()
            {
                InitializeComponent();
    
                BindingContext = new TryPageCS(this.Navigation);
            }
        }
    

    A simple usage in xaml

      <Button  Text="Navigate" Command="{Binding navigateCommand }"></Button>
    
  • jezhjezh Member, Xamarin Team Xamurai

    Hi @kingston5555, have you resolved your question?

Sign In or Register to comment.