I have a Page with only a pivot in it. This page is always cached. Now whenever I navigate to this page, I want its contents and selections to be loaded from cached but I want to have the first PivotItem
in view.
XAML :
<Pivot x:Name="FilterPivot"
IsHeaderItemsCarouselEnabled="True"
SelectedIndex="0">
<PivotItem Header="Author" >
<ListBox ItemsSource="{x:Bind AuthorFacets, Mode=OneWay}"
Name="AuthorListBox"
SelectionMode="Multiple"
SelectionChanged="AuthorListBox_SelectionChanged">
<ListBox.ItemContainerStyle>
<Style TargetType="ListBoxItem">
<Setter Property="HorizontalContentAlignment" Value="Stretch" />
</Style>
</ListBox.ItemContainerStyle>
<ListBox.ItemTemplate>
<DataTemplate x:DataType="local:IFacet">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"></ColumnDefinition>
<ColumnDefinition Width="Auto"></ColumnDefinition>
</Grid.ColumnDefinitions>
<TextBlock Grid.Column="0"
Text="{x:Bind read}"
TextWrapping="Wrap"
HorizontalAlignment="Left"
VerticalAlignment="Center"/>
<Border Grid.Column="1"
Background="Gray"
MinWidth="25"
CornerRadius="8">
<TextBlock Text="{x:Bind num}"
HorizontalAlignment="Center"
VerticalAlignment="Center"
Padding="2"/>
</Border>
</Grid>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</PivotItem>
<PivotItem Header="Language">
....
....
</PivotItem>
<PivotItem Header="Learning Resource Type">
....
....
</PivotItem>
<PivotItem Header="Subject">
....
....
</PivotItem>
<PivotItem Header="Type">
....
....
</PivotItem>
<PivotItem Header="Education Level">
....
....
</PivotItem>
<PivotItem Header="Source">
....
....
</PivotItem>
</Pivot>
This is my code behind:
public FilterPage()
{
this.InitializeComponent();
this.NavigationCacheMode = NavigationCacheMode.Required;
}
protected override void OnNavigatedTo(NavigationEventArgs e)
{
base.OnNavigatedTo(e);
FilterPivot.SelectedIndex = 0;
}
This doesn't select the 0th index instead, it shows the cached PivotItem
that was selected when the user navigated away from the view.
The OnNavigatedTo
will be invoked before the Pivot
control is even loaded, so setting the SelectedIndex
won't affect its selection.
The easy workaround is to subscribe to the Loaded
event instead and set it from there.
Just do the following within your MainPage
's constructor -
FilterPivot.Loaded += (s, e) => FilterPivot.SelectedIndex = 0;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With