Do some tasks after OnAppearing() in MAUI

214 Views Asked by At

I have a maui community toolkit expander control in my page. On Tapping the header the content is shown/hidden. I got a Maui map in the expander's content. It display properly if the expander is in 'Expanded' state. But if the page is loaded with 'Collapsed' State the map won't be displayed properly.

So I would like to keep the expander expanded once loading and after the OnAppearing I would like to keep it closed.

Is there any way I can call a method to change the state (IsExpanded to false) after the OnAppearing

<Page
  …
  xmlns:toolkit="http://schemas.microsoft.com/dotnet/2022/maui/toolkit"
  xmlns:maps="clr-namespace:Microsoft.Maui.Controls.Maps;assembly=Microsoft.Maui.Controls.Maps">

    <toolkit:Expander x:Name="expander" 
                      IsExpanded="True" 
                      ExpandedChanged="Expander_ExpandedChanged">
        <toolkit:Expander.Header>
            <Label Text="Show/Hide "
                   FontAttributes="Bold"
                   FontSize="Medium" />
        </toolkit:Expander.Header>
        <toolkit:Expander.Content>
            <StackLayout Margin="0"
                         Orientation="Horizontal">
                <maps:Map x:Name="map" 
                          HeightRequest="300" 
                          IsShowingUser="True" 
                          MapType="Satellite"/>
            </StackLayout>
        </toolkit:Expander.Content>
    </toolkit:Expander>
1

There are 1 best solutions below

0
Guangyu Bai - MSFT On

Is there any way I can call a method to change the state (IsExpanded to false) after the OnAppearing?

Lifecycle functions that are triggered after a page is fully loaded are not yet included in MAUI.

But you can use the VisualElement.Loaded Event, it occurs when a VisualElement has been constructed and added to the object tree. This event may occur before the VisualElement has been measured so should not be relied on for size information.

YouControl.Loaded += (s, e) => {        };

Here is the information about VisualElement.Loaded Event.