MVVM Xamarin Forms Passing data form ViewModel to Modal

Buzzdown 46 Reputation points
2022-11-03T15:09:27.127+00:00

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

Xamarin
Xamarin
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
    2022-11-04T07:31:41.967+00:00

    Hello,

    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)  
               {  
                   InitializeComponent();  
                   MyViewModel myViewModel=  new MyViewModel();  
                   myViewModel.initData(senddata);  
                   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