A Microsoft framework for building cross-platform mobile apps using .NET and C# with native performance and user interfaces.
Hello,
Welcome to our Microsoft Q&A platform!
You can use Action instead of MessageCenter. It also can achieve ZERO code-behind.
You can have a try with following code:
NotebooksVM
SelectedNoteBookCommand = new Command(async () => {
NotesPage notesPage = new NotesPage();
await Application.Current.MainPage.Navigation.PushAsync(notesPage);
NotesVM notesViewModel = (NotesVM)notesPage.BindingContext;
notesViewModel.RecivedNoteAction(SelectedNotebook);//invoke the action
//MessagingCenter.Send(this, "data", SelectedNotebook);
SelectedNotebook = null;
});
NotesVM
CreateNewNoteCommand = new Command(async () => {
var note = await CreateNote(RecivedNotebook);
await GetNotes(RecivedNotebook);
EditorPage editorPage = new EditorPage();
await Application.Current.MainPage.Navigation.PushAsync(editorPage);
EditorVM Editor = (EditorVM)editorPage.BindingContext;
Editor.RecivedNoteAction(note);//Replace MessagingCenter.Send
});
SelectedNoteCommand = new Command(async () => {
EditorPage editorPage = new EditorPage();
await Application.Current.MainPage.Navigation.PushAsync(editorPage);
EditorVM Editor = (EditorVM)editorPage.BindingContext;
Editor.RecivedNoteAction(SelectedNote);
//MessagingCenter.Send(this, "note", SelectedNote);
SelectedNote = null;
});
// Replace MessagingCenter.Subscribe
RecivedNoteAction = async(Notebook) =>
{
RecivedNotebook = Notebook;
await GetNotes(RecivedNotebook);
};
EditorVM
public class EditorVM {
public string Text { get; set; }
public ICommand SaveContent { get; set; }
public Note RecivedNote { get; set; }
public Action<Note> RecivedNoteAction;
public EditorVM() {
SaveContent = new Command(() => { UpdateNote(); });
RecivedNoteAction = (Note) =>
{
RecivedNote = Note;
};
}
private void UpdateNote() {
// test out of action function
Console.WriteLine("test------------{0}", this.RecivedNote.Title);
}
}
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.