Xamarin - TabbedPage with childreen content page MVVM

Davis Cornejo 1 Reputation point
2021-06-08T00:24:48.187+00:00

Hi everybody!

I have a tabbed page with its respective viewmodel where a rest service is called from that viewmodel. The moment that rest service is called and returns a response, I need to pass that response to the viewmodels of the children of the tabbed. What is the best way to do this?

Xamarin
Xamarin
A Microsoft open-source app platform for building Android and iOS apps with .NET and C#.
5,337 questions
{count} votes

1 answer

Sort by: Most helpful
  1. JarvanZhang 23,951 Reputation points
    2021-06-23T08:38:36.753+00:00

    Hello,​

    Welcome to our Microsoft Q&A platform!

    You could also use Delegate to achieve the function. Create the delegate in the TabbedPage's ViewModel class and then subscribe the event in child page's ViewModel class.

       //the viewModel class of the tabbedPage  
       public delegate void TestEventHandler();  
       namespace TestApplication  
       {  
           public class ViewModel_TabbedPge  
           {  
               public event TestEventHandler TestEvent;  
               public async Task TestMethod()  
               {  
                   var response = await getResponseMethod();  
                   if (TestEvent != null)  
                   {  
                       TestEvent();  
                   }  
               }  
           }  
       }  
             
       //the child page  
       public class ViewModel_Page1  
       {  
           ViewModel_Page1 viewModel;  
           ViewModel_TabbedPge viewModel_TabbedPge;  
         
           public ViewModel_Page1(ViewModel_TabbedPge viewModel_TabbedPge)  
           {  
               InitializeComponent();  
         
               viewModel = new ViewModel_Page1();  
               BindingContext = viewModel;  
         
               viewModel_TabbedPge = App.viewModel_TabbedPge;//create a global static instance of the ViewModel_TabbedPge class in the App.cs  
               viewModel_TabbedPge.TestEvent += TheTestEvent;  
           }  
         
           private void TheTestEvent()  
           {  
               //perform the work  
           }  
       }  
    

    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.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.