[FIXED] How to call a method in xaml.cs file from BaseViewModel xamarin

Issue

I have a requirement of opening alert dialog when a event is firing in Baseviewmodel. alert dialog opening logic is written in xaml.cs file’s button click handler.

//BaseViewModel event
 void CloseDialog()
 {
    //Call xaml.cs file `OpenDiaglog` event here
 }

This is xaml.cs file’s method which I need to call

void OpenDiaglog()
  {
    lblZoomedView.IsVisible = true;
    zoomViewFrame.IsVisible = true;
    innerpersonnelGrid.IsVisible = false;
    btnNextZoomedView.IsVisible = true;
    btnNext.IsVisible = false;
  }

What I need to use MessagingCenter or Subscsribers, how can I do that ?

Solution

Solved this using MessegingCenter.

In BaseViewModel

//BaseViewModel event   
void CloseDialog()
 {     
    //Call xaml.cs file `OpenDiaglog` event here
    MessagingCenter.Send(Xamarin.Forms.Application.Current, "Hi");
 }

In xaml.cs file

protected override void OnAppearing()
 {
    base.OnAppearing();
    MessagingCenter.Subscribe<Xamarin.Forms.Application>(Xamarin.Forms.Application.Current, "Hi", (sender) =>
     {
        OpenDiaglog();
     });
 }

Answered By – R15

Answer Checked By – Dawn Plyler (Easybugfix Volunteer)

Leave a Reply

(*) Required, Your email will not be published