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.