2017-06-14 46 views
4

我有一個只有一個透視的頁面。此頁面始終緩存。現在,無論何時導航到此頁面,我都希望從緩存中加載其內容和選擇,但我希望能夠看到第一個PivotItem。 XAML:在UWP中使用Pivot的SelectedIndex屬性

<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> 

這背後是我的代碼:

public FilterPage() 
{ 
    this.InitializeComponent(); 
    this.NavigationCacheMode = NavigationCacheMode.Required; 
} 

protected override void OnNavigatedTo(NavigationEventArgs e) 
{ 
    base.OnNavigatedTo(e); 
    FilterPivot.SelectedIndex = 0; 
} 

這不選擇零指數,相反,它顯示了緩存PivotItem當用戶導航遠離視圖是被選中。

回答

4

OnNavigatedTo將被調用Pivot控制甚至裝,所以設置SelectedIndex不會影響其選擇。

簡單的解決方法是訂閱Loaded事件,然後從那裏設置它。

只是做內以下的MainPage的構造 -

FilterPivot.Loaded += (s, e) => FilterPivot.SelectedIndex = 0; 
相關問題