Maui Shell Flyout IsPresentedChanged event

161 Views Asked by At

Xamarin forms to Maui migration question...

The FlyoutPage in XF has the IsPresentedChanged event that fires when the Flyout menu is displayed/hidden.

Is there an equivalent for Maui Shell Flyout? I can't find anything in the docs.

As a supplement I have a ContentView set as the Shell.FlyoutContent and I can accomplish my requirement (amend a property on the ContentView when the Flyout menu becomes visible) via an OnAppearing event on the ContentView.

Thanks in advance.

2

There are 2 best solutions below

1
On BEST ANSWER

The Shell doesn't have a such event. But you can detect the Shell.FlyoutIsPresented Property changed by your custom event.

  public AppShell()
  {
      InitializeComponent();
      PropertyChanged += Changed;
  }   

  private void Changed(object? sender, System.ComponentModel.PropertyChangedEventArgs e)
  {
      if(e.PropertyName == "FlyoutIsPresented")
      {
          if(FlyoutIsPresented)
          {

          }else
          {
          }
      }
  }
2
On

If you're using the MVVM Community Toolkit then you can rely on two-way binding and the source generated OnXXXChanged / OnXXXChanging methods.

In your ViewModel:

[ObservableProperty]
bool _isFlyoutPresented;

In your View (XAML):

<Shell FlyoutIsPresented="{x:Binding IsFlyoutPresented, Mode=TwoWay}" />

Now you can react to the changes in your ViewModel:

partial void OnIsFlyoutPresentedChanging(bool oldValue, bool newValue)
{
    // ...
}

Setting the IsFlyoutPresented property will now trigger the flyout. You can tell if it's open by checking the value in IsFlyoutPresented.