0

I'm working with UWP with WinUI 2.5 prerelease and have noticed a strange behavior which doesn't happen in WPF. So i'm using a TabView control and loading a Page in it. Every time a new tab is created, a new page is loaded in it (nothing fancy).

Now, I have some OnLoaded events on my pages. so when a new tab is created and page is loaded, the OnLoad event is called (as it should) but now when i switch back to other tab and switch again to current page the OnLoaded event is called again. WHY ?

MainPage with TabView

<Page
    x:Class="TestUWPApp.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:TestUWPApp"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:controls="using:Microsoft.UI.Xaml.Controls"
    mc:Ignorable="d"
    Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">

    <Grid>
        <controls:TabView x:Name="MainTabView">
            <controls:TabView.TabItems>
                <controls:TabViewItem Header="Intelliventory"
                                      IsClosable="False" CanDrag="False">
                    <controls:TabViewItem.IconSource>
                        <controls:SymbolIconSource Symbol="Home" />
                    </controls:TabViewItem.IconSource>
                    <Frame SourcePageType="local:Page2" />
                </controls:TabViewItem>
                <controls:TabViewItem Header="Intelliventory"
                                      IsClosable="False" CanDrag="False">
                    <controls:TabViewItem.IconSource>
                        <controls:SymbolIconSource Symbol="Home" />
                    </controls:TabViewItem.IconSource>
                    <Frame SourcePageType="local:Page3" />
                </controls:TabViewItem>
            </controls:TabView.TabItems>
        </controls:TabView>
    </Grid>
</Page>  

Page3 that has an OnLoaded event

namespace TestUWPApp
{

    public sealed partial class Page3 : Page
    {
        public Page3()
        {
            this.InitializeComponent();
        }

        private void Page3_OnLoaded(object sender, RoutedEventArgs e)
        {
            //This event is called every time tab is switched
        }
    }
} 

Every time selected tab is changed to Page3 tab the onLoaded event is called. That should just be called once when first time the tab was switched and Page was loaded.

Hammas
  • 1,126
  • 1
  • 12
  • 37
  • Hello, if you are using the pre-release WinUI package, you can ask your question on [Github](https://github.com/microsoft/microsoft-ui-xaml/issues/new/choose), and the developers are actively responding to relevant questions there. – Richard Zhang Jun 30 '20 at 02:38
  • @RichardZhang-MSFT hi, I have asked there too but no response so far.(also i think same happens in non pre-release versions) – Hammas Jun 30 '20 at 07:52
  • Have you tried the ChacheModes?public Page3() { this.InitializeComponent(); this.NavigationCacheMode = Windows.UI.Xaml.Navigation.NavigationCacheMode.Enabled; } – thezapper Jun 30 '20 at 11:32
  • @thezapper yeah i just tried this and it doesn't work either. Load events are called ! Someone told me on github that it happens cuz the controls are loaded in visual tree on tab navigations again and on this the load events are called. – Hammas Jul 01 '20 at 15:21
  • and this feels not good to me for reason 1 that it doesn't happen in WPF and 2. the page was already loaded in tab so it shouldn't call load events again while switching tabs.. – Hammas Jul 01 '20 at 15:22

1 Answers1

1

The Loaded event gets raised every time the element gets added to the VisualTree, has rendered and is ready for interaction. If you select a TabViewItem, the TabViewItems page gets added to the visual tree again, gets rendered, and thus raises the Loaded event again. So this behaviors is by design.

chingucoding
  • 894
  • 7
  • 17