MVVM Xamarin Forms Passing data form ViewModel to Modal

Buzzdown 46 Reputation points

How I Could pass data from one ViewModel to Modal, thank you community!! Cheers.

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

Accepted answer
  1. Leon Lu (Shanghai Wicresoft Co,.Ltd.) 70,966 Reputation points Microsoft Vendor


    I Have searched many ways to pass the data from one View to another, but the solutions that are suggested is wrong because you break the MVVM pattern (e.g pass data by constructorse e.t.c)

    You can create Init(data) in the BaseViewModel which is extended by other viewmodels

       public abstract class BaseViewModel  
               public abstract void initData(string data);  

    Then when you bind the viewmodel, you can set the data by initData method.

       public MainPage(string senddata)  
                   MyViewModel myViewModel=  new MyViewModel();  
                   BindingContext = myViewModel;  

    Or you can use Constructor injection as well.

    Best Regards,

    Leon Lu

    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    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.

    1 person found this answer helpful.

0 additional answers

Sort by: Most helpful