question

EduardoGomez-1870 avatar image
0 Votes"
EduardoGomez-1870 asked WenyanZhang-MSFT commented

View Model to view model comunication

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






dotnet-xamarin
· 3
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

I'm afraid I can't understand what you mean. Do you want to communicate NotesVM with EditoVM? Would you mind sharing more code about NotesVM, EditoVM and MessagingCenter?

0 Votes 0 ·

Ok What I want to do, is to navigate to the EditorPage, when the user selects a node, or when the user creates a note, in both cases I need the ID of the note.

The only way I can think of is to navigate when the user selects a note, but that is poor UX, what I want is for the user to create a note, and immediately navigate to the EditorPage

this is my project

https://github.com/eduardoagr/safe.Xamarin

0 Votes 0 ·

I have got the code, I will test it.

0 Votes 0 ·

1 Answer

WenyanZhang-MSFT avatar image
0 Votes"
WenyanZhang-MSFT answered WenyanZhang-MSFT commented

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][1] to enable e-mail notifications if you want to receive the related email notification for this thread.
[1]: https://docs.microsoft.com/en-us/answers/articles/67444/email-notifications.html

· 8
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Yes this would work, but I do not want to pass through the code behind, I want ZERO CODE BEHIND

0 Votes 0 ·

Maybe you can use MessagingCenter when you create or select a note.

0 Votes 0 ·

How? can I do to send in both cases, and subscribe to the receive note or created note

0 Votes 0 ·
Show more comments