'Xamarin expander exception

I'm trying to use Xamarin Forms Community Toolkit Expander but this exception keeps appearing to me

System.InvalidOperationException: The class, property, or method you are attempting to use ('Expander') is part of Expander; to use it, you must opt-in by calling Forms.SetFlags("Expander_Experimental") before calling Forms.Init().

It appears although I have written this line in MainActivity:

 Forms.SetFlags("Expander_Experimental"); 

Note: It's an android project.

How Can I Solve It?



Solution 1:[1]

In Xamarin Forms you put it in App.xaml.cs like this before InitializeComponent();

  public App()
    {
        Forms.SetFlags("Expander_Experimental");
        InitializeComponent();

        MainPage = new NavigationPage(new MainPage ());

    }

To enable the flags per platform, go to the class that bootstraps that platform. For iOS that would be the AppDelegate.cs file. For Android the MainActivity.cs. Then before you call the Forms.Init(); line, you will have to enable the flags. Underneath you can find sample code for iOS and Android.

 // For iOS in the AppDelegate.cs
public override bool FinishedLaunching(UIApplication uiApplication, NSDictionary launchOptions)
{
  global::Xamarin.Forms.Forms.SetFlags("Expander_Experimental");
  Forms.Init();
  
  // The rest of the code…
}

// For Android in the MainActivity.cs
protected override void OnCreate(Bundle bundle)
{
  global::Xamarin.Forms.Forms.SetFlags("Expander_Experimental");
  Forms.Init();
}

A little example of how not to use the flags, for example the Expander and Indicator.

  **// DO NOT use this code, it's faulty**
global::Xamarin.Forms.Forms.SetFlags("Expander_Experimental");
global::Xamarin.Forms.Forms.SetFlags("IndicatorView_Experimental");

Then use it like this :

Xamarin.Forms.Forms.SetFlags("Expander_Experimental", "IndicatorView_Experimental");

Solution 2:[2]

The exception was solved by adding this line to App.xaml.cs: Device.SetFlags("Expander_Experimental");

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 Yasmine Abdelsamie