MessengCenter too Slow

Eduardo Gomez 3,416 Reputation points
2022-02-01T00:40:05.537+00:00

Hello

When I tried to select a notebook in my notebook app, I Was using this code

public Notebook RecivedSelectedNotebook { get; set; }

         public Note SeletedNote { get; set; }

         public NotesPageViewModel() {

             FabAnimationCommmand = new Command<Frame>(AnimateButtonCommand);

             RecivedSelectedNotebookAccion = (SelectedObject) => {

                 RecivedSelectedNotebook = SelectedObject;
             }; // here I put a breakpoint and I get the RecivedSelectedNotebook object

             Services.ReadAsync(App.Notes, RecivedSelectedNotebook.Id); // here for  some reason RecivedSelectedNotebook is null
         }

Because It was null I change it and used the xamarin messaging center

public NotesPageViewModel() {

        Services = App.services;

        FabAnimationCommmand = new Command<Frame>(AnimateButtonCommand);

        MessagingCenter.Subscribe<MainPageViewModel, Notebook>(this, App.NotebookID, (sender, value) => {
            RecivedSelectedNotebook = value;
            Services.ReadAsync(App.Notes, RecivedSelectedNotebook.Id);

            MessagingCenter.Unsubscribe<MainPageViewModel, Notebook>(this, App.NotebookID);
        });

Now the problem is this

Video error: https://reccloud.com/u/mh5izje

project: https://github.com/eduardoagr/DuoNotes

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

Accepted answer
  1. JarvanZhang 23,936 Reputation points
    2022-02-02T02:57:21.75+00:00

    To improve the performance, try to move to the MessagingCenter.Send before navigation.

    public async virtual void SeletedItemActionAsync()
    {
        if (SelectedNotebook == null)
        {
            return;
        }
        //MessagingCenter.Send();
        await Application.Current.MainPage.Navigation.PushAsync(notesPage);
        SelectedNotebook = null;
    }
    

    If it stil doesn't work, try to store the data and then retrive the data in the

    public async virtual void SeletedItemActionAsync()
    {
        if (SelectedNotebook == null)
        {
            return;
        }
        Application.Current.Properties["id"] = App.NotebookID;//if the id is global static, you could use it directly
        Application.Current.Properties["selected_object"] = SelectedNotebook;
        await Application.Current.MainPage.Navigation.PushAsync(notesPage);
        SelectedNotebook = null;
    }
    
    public NotesPageViewModel()
    {
    
        Services = App.services;
    
        FabAnimationCommmand = new Command<Frame>(AnimateButtonCommand);
    
        if (Application.Current.Properties.ContainsKey("id"))
        {
            var selectedObject = Application.Current.Properties["selected_object"] as Notebook;
        }
        ...
    }
    
    0 comments No comments

1 additional answer

Sort by: Most helpful
  1. JarvanZhang 23,936 Reputation points
    2022-02-01T09:10:05.23+00:00

    Hi, EduardoGomez-1870. To pass the info, you could create a parameterized constructor method for 'NotesPage'. Then pass the notebook id and the selected object when navigating to the 'NotesPage' as below.

    NotesPage notesPage = new NotesPage(App.NotebookID, SelectedNotebook);
    await App.Current.MainPage.Navigation.PushAsync(notesPage);