关于为Pivot不同的PivotItem定制ApplicationBa,原理其实很简单,首先定义多套ApplicationBar资源
<phone:PhoneApplicationPage.Resources>
<shell:ApplicationBar x:Key="appbar1" IsVisible="True">
<shell:ApplicationBarIconButton IconUri="Images/appbar.add.rest.png" Text="Button1_1"/>
<shell:ApplicationBarIconButton IconUri="Images/appbar.back.rest.png" Text="Button1_2"/>
</shell:ApplicationBar>
<shell:ApplicationBar x:Key="appbar2" IsVisible="True">
<shell:ApplicationBarIconButton IconUri="Images/appbar.delete.rest.png" Text="Button2_1"/>
<shell:ApplicationBarIconButton IconUri="Images/appbar.next.rest.png" Text="Button2_2"/>
</shell:ApplicationBar>
</phone:PhoneApplicationPage.Resources>
<shell:ApplicationBar x:Key="appbar1" IsVisible="True">
<shell:ApplicationBarIconButton IconUri="Images/appbar.add.rest.png" Text="Button1_1"/>
<shell:ApplicationBarIconButton IconUri="Images/appbar.back.rest.png" Text="Button1_2"/>
</shell:ApplicationBar>
<shell:ApplicationBar x:Key="appbar2" IsVisible="True">
<shell:ApplicationBarIconButton IconUri="Images/appbar.delete.rest.png" Text="Button2_1"/>
<shell:ApplicationBarIconButton IconUri="Images/appbar.next.rest.png" Text="Button2_2"/>
</shell:ApplicationBar>
</phone:PhoneApplicationPage.Resources>
还可以为不同的Button设置响应事件,这里就略去了。
其次,让Pivot
control响应SelectionChanged事件
<controls:Pivot Title="PIVOT DEMONSTRATION" Name="MyPivotControl" SelectionChanged="MyPivot_SelectionChanged">
事件处理函数中为不同的PivotItem设置不同的ApplicationBar
private void MyPivot_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
if (MyPivotControl.SelectedIndex == 0)
ApplicationBar = (Microsoft.Phone.Shell.ApplicationBar)Resources["appbar1"];
else if (MyPivotControl.SelectedIndex == 1)
ApplicationBar = (Microsoft.Phone.Shell.ApplicationBar)Resources["appbar2"];
}
{
if (MyPivotControl.SelectedIndex == 0)
ApplicationBar = (Microsoft.Phone.Shell.ApplicationBar)Resources["appbar1"];
else if (MyPivotControl.SelectedIndex == 1)
ApplicationBar = (Microsoft.Phone.Shell.ApplicationBar)Resources["appbar2"];
}