dotnet / maui

.NET MAUI is the .NET Multi-platform App UI, a framework for building native device applications spanning mobile, tablet, and desktop.
https://dot.net/maui
MIT License
22.02k stars 1.73k forks source link

ContentTemplate pages are not executing functions. #20777

Open kelputoo123 opened 7 months ago

kelputoo123 commented 7 months ago

Description

Hi, im a begginer, and im not really sure if this is a bug or not. I noticed that this works sometimes in windows machine and sometimes don't, it's really weird bug. So, i have Flyout and Tabbar in my AppShell.xaml, my code looks like this:

<Shell
    x:Class="DFAPP.AppShell"
    xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
    xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
    xmlns:local="clr-namespace:DFAPP"
    Shell.FlyoutBehavior="Flyout">

    <TabBar x:Name="Tabbar_Items">
        <ShellContent x:Name="TabItem_Administracao" Title="Administração" Route="home" ContentTemplate="{DataTemplate local:Pages.Decimal.HomePage}" Icon="dashboardicon"/>
        <ShellContent x:Name="TabItem_MenuBombeiro" Title="Menu Bombeiro" ContentTemplate="{DataTemplate local:Pages.MenuBombeiro.Home_MENUBOMB}" Icon="fireicon"/>
        <ShellContent x:Name="TabItem_IPMA" Title="IPMA" ContentTemplate="{DataTemplate local:Pages.Decimal.IPMA}" Icon="ipmaicon"/>
        <ShellContent x:Name="TabItem_Configuracoes" Title="Configurações" ContentTemplate="{DataTemplate local:Pages.Decimal.Configuracoes}" Icon="settingsicon"/>
    </TabBar>

    <!--<MenuFlyoutItem x:Name="Menu_HOME" Text="Início" Clicked="Navigate" IconImageSource="homeicon"></MenuFlyoutItem>-->
    <MenuFlyoutItem x:Name="Menu_QO" Text="Quartel Online" Clicked="Navigate" IconImageSource="quartelonlineicon"></MenuFlyoutItem>
    <MenuFlyoutItem x:Name="Menu_OCORRENCIAS" Text="Ocorrências" Clicked="Navigate" IconImageSource="ocorrenciasicon"></MenuFlyoutItem>
    <MenuFlyoutItem x:Name="Menu_TRANSPORTADOS" Text="Transportados" Clicked="Navigate" IconImageSource="transportadosicon"></MenuFlyoutItem>
    <MenuFlyoutItem x:Name="Menu_DIRECAO" Text="Direção" Clicked="Navigate" IconImageSource="direcaoicon"></MenuFlyoutItem>

</Shell>

By far, all ok. But once the app opens it goet do my Pages.Decimal.HomePage, and the function in there are not executed when the app loads. I tried with OnAppearing(), but it only works if I navigate to it, it doesnt execute when the app loads. I don't know if im explaining the best way. This is my HomePage.xaml.cs code:

public HomePage()
        {
            InitializeComponent();
            OnAppearing();
            BindingContext = this;
        }

        protected override void OnAppearing()
        {
            base.OnAppearing();
            x();
        }

        void x()
        {
            DisplayAlert("Ola", "teste", "teste");
        }

This function "x()" is not being executed when it shows this page, does someone knows why this happens?

Steps to Reproduce

No response

Link to public reproduction project repository

No response

Version with bug

8.0.6 SR1

Is this a regression from previous behavior?

Not sure, did not test other versions

Last version that worked well

Unknown/Other

Affected platforms

iOS, Android

Affected platform versions

No response

Did you find any workaround?

No.

Relevant log output

No response

PureWeen commented 7 months ago

@kelputoo123 if you subscribe to Loaded and call x() from there does that work? Also, I wouldn't recommend calling Appearing from the constructor.

Also, it'd probably be best to away the DisplayAlert call.

ghost commented 7 months ago

Hi @kelputoo123. We have added the "s/needs-repro" label to this issue, which indicates that we require steps and sample code to reproduce the issue before we can take further action. Please try to create a minimal sample project/solution or code samples which reproduce the issue, ideally as a GitHub repo that we can clone. See more details about creating repros here: https://github.com/dotnet/maui/blob/main/.github/repro.md

This issue will be closed automatically in 7 days if we do not hear back from you by then - please feel free to re-open it if you come back to this issue after that time.

ghost commented 7 months ago

We've added this issue to our backlog, and we will work to address it as time and resources allow. If you have any additional information or questions about this issue, please leave a comment. For additional info about issue management, please read our Triage Process.

kelputoo123 commented 7 months ago

I've fixed this by added the Loaded event, however, i think this still a bug, it's was not supposed to add the "Loaded" event.

ninachen03 commented 5 days ago

Could you provide us with a sample project so we can investigate it further? Looking forward to your reply!