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.
OnLoaded is called whenever the content is added to the Visual tree, navigating to a tab causes its visuals to be loaded into the tree, and since the page is a part of the tab that is causing the page to get loaded. I believe this is by design.
If you have more questions feel free to ask here even thought he issue is closed :)
OnLoaded is called whenever the content is added to the Visual tree, navigating to a tab causes its visuals to be loaded into the tree, and since the page is a part of the tab that is causing the page to get loaded. I believe this is by design.
Ok ! but this doesn't happen in WPF
that's strange that Load
event is called every time even though page was already loaded in tab. Load event should be called just once when first time in tab the page was loaded. not every-time when we switch tabs .
@StephenLPeters am i not right ? Load events should be called once when first time the page was loaded in tab. On tab switching it shouldn't again call load events ...
I think there is a little confusion about what loaded means. In UWP it does not indicate that the content was loaded into memory, it indicates that the item was added to the visual tree. The UWP TabView adds the contents associated with the selected TabViewItem to the visual tree when the selection changes so we do expect the loaded event to fire.