View Model to view model comunication

Eduardo Gomez 3,426 Reputation points
2021-07-04T19:37:49.687+00:00

Hello

I am making a note-taking application I already have all my notes, and notebook listed, But now I am facing this problem.

I am using the MVVM pattern, and to communicate with the other VM, I am using the messaging center, so I have zero code-behind.

Look at this VM

   public NotesVM() {

            CreateNewNoteCommand = new Command(async () => {
                var note = await CreateNote(RecivedNotebook);
                await GetNotes(RecivedNotebook);
            });

            SelectedNoteCommand = new Command(async () => {
                await Application.Current.MainPage.Navigation.PushAsync(new EditorPage());
                MessagingCenter.Send(this, "note", SelectedNote);
            });

I want to navigate to the EditorPage, when the user creates a new note or when the user selects a note, the big problem is that in both scenarios I need information from the note such as the ID

I tried to subscribe to both scenarios in my EditoVM, which works, but is very messy, and have to duplicate my subscribing for both scenarios, and when debugging I get duplicates messages.

Everybody tells me to use an MVVM framework but if xamarin is MVVM out of the box, I don't see the point

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

1 answer

Sort by: Most helpful
  1. Wenyan Zhang (Shanghai Wicresoft Co,.Ltd.) 29,456 Reputation points Microsoft Vendor
    2021-07-07T06:53:53.96+00:00

    Hello,
    Welcome to our Microsoft Q&A platform!

    I noticed that you display a dialog to create a new note and give it a title. If you want to navigate to the EditorPage when you create or select a note, you can pass value(ID,model) like following code:

    UpDate

    NotesVM

     CreateNewNoteCommand = new Command(async () => {  
    
                    //you can use action to pass Id             
                    EditorPage editorPage = new EditorPage();  
                    await Application.Current.MainPage.Navigation.PushAsync(editorPage);  
                    EditorVM Editor = (EditorVM)editorPage.BindingContext;  
                    Editor.reciviceBookIdAction(RecivedNotebook.Id);  
                    /*  
                    //you can also use MessagingCenter to pass Id  
                    await Application.Current.MainPage.Navigation.PushAsync(new EditorPage());  
                    MessagingCenter.Send(this, "RecivedNotebookId", RecivedNotebook.Id);  
                    */  
                });  
                SelectedNoteCommand = new Command(async () => {  
                    //you can use action to pass SelectedNote  
                    EditorPage editorPage = new EditorPage();  
                    await Application.Current.MainPage.Navigation.PushAsync(editorPage);  
                    EditorVM Editor = (EditorVM)editorPage.BindingContext;  
                    Editor.RecivedNoteAction(SelectedNote);  
                    /*  
                    //you can also use MessagingCenter to pass SelectedNote  
                    await Application.Current.MainPage.Navigation.PushAsync(new EditorPage());  
                    MessagingCenter.Send(this, "SelectedNote", SelectedNote);  
                    */  
                });  
    

    EditorPage(none)

        /*  
    public EditorPage(String NotebookId)  
        {  
        InitializeComponent();  
        EditorVM Editor = (EditorVM)BindingContext;  
        Editor.CreateNote(NotebookId);  
        Console.WriteLine("{0}", NotebookId);  
        }  
        public EditorPage(Model.Note note)  
        {  
        InitializeComponent();  
        EditorVM Editor = (EditorVM)BindingContext;  
        Editor.RecivedNote = note;  
        Console.WriteLine("{0}", note.Id);  
        }  
    */  
    

    EditorVM

            public Action<string> reciviceBookIdAction; //use action  
            public Action<Note> RecivedNoteAction;   
            public EditorVM() {  
    
                SaveContent = new Command(() => { UpdateNote(); });  
    
                //use action(reciviceBookId)  
    
                reciviceBookIdAction = (reciviceBookId) =>  
                {  
                    Console.WriteLine("reciviceBookId-----{0}", reciviceBookId);  
                    //CreateNote(reciviceBookId);  
                };  
    
                /*  
                //use MessagingCenter(reciviceBookId)  
                MessagingCenter.Subscribe<NotesVM, string>(this, "RecivedNotebookId", (obj, item) => {  
                    Console.WriteLine("reciviceBookId-----{0}", item);  
                });  
                */  
    
                //use action(note)  
                RecivedNoteAction = (note) =>  
                {  
                    RecivedNote = note;  
                    Title = RecivedNote.Title;  
                    Console.WriteLine("RecivedNote.id-----{0}", RecivedNote.Id);  
                };  
                /*  
                //use MessagingCenter(note)  
                MessagingCenter.Subscribe<NotesVM, Note>(this, "SelectedNote", (obj, item) => {  
                    RecivedNote = item;  
                    Title = RecivedNote.Title;  
                    Console.WriteLine("RecivedNote.id-----{0}", item.Id);  
                });  
                */  
            }  
    

    Best Regards,
    Wenyan 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.