Home > Net >  How to call a method in xaml.cs file from BaseViewModel xamarin
How to call a method in xaml.cs file from BaseViewModel xamarin

Time:07-23

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 ?

CodePudding user response:

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();
     });
 }
  • Related