在這個Pivot Page中有兩個PivotItem,然後各自需要不同的ApplicationBar,但因為同一個頁面只能有一組的ApplicationBar,所以這邊就必須要把所有需要的ApplicationBar定義在Resources中,Xaml代碼如下
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<phone:PhoneApplicationPage | |
x:Class="AppBar.PivotPage1" | |
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" | |
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" | |
xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone" | |
xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone" | |
xmlns:controls="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone.Controls" | |
xmlns:d="http://schemas.microsoft.com/expression/blend/2008" | |
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" | |
mc:Ignorable="d" > | |
<phone:PhoneApplicationPage.Resources> | |
<shell:ApplicationBar x:Key="Appbar1" IsVisible="True" Mode="Default"> | |
<shell:ApplicationBarIconButton Text="button" IconUri="/Assets/AppBar/appbar.add.rest.png"/> | |
</shell:ApplicationBar> | |
<shell:ApplicationBar x:Key="Appbar2" IsVisible="True" Mode="Default"> | |
<shell:ApplicationBarIconButton Text="button" IconUri="/Assets/AppBar/appbar.back.rest.png"/> | |
</shell:ApplicationBar> | |
</phone:PhoneApplicationPage.Resources> | |
<Grid x:Name="LayoutRoot" Background="Transparent"> | |
<controls:Pivot Title="MY APPLICATION" Name="Testpivot"> | |
<controls:PivotItem Header="item1"> | |
<Grid/> | |
</controls:PivotItem> | |
<controls:PivotItem Header="item2"> | |
<Grid/> | |
</controls:PivotItem> | |
</controls:Pivot> | |
</Grid> | |
</phone:PhoneApplicationPage> |
而決定要使用哪組ApplicationBar是根據PivotItem而定,所以要在Pivot控制項的SelectionChanged事件中去判斷,例如如果Index是0就使用第一組的ApplicationBar,依此類推,這樣即使在同一頁面中,也可根據不同的PivotItem功能需求而更換不同的ApplicationBar了。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Windows.Controls; | |
using Microsoft.Phone.Controls; | |
using Microsoft.Phone.Shell; | |
namespace AppBar | |
{ | |
public partial class PivotPage1 : PhoneApplicationPage | |
{ | |
public PivotPage1() | |
{ | |
InitializeComponent(); | |
Testpivot.SelectionChanged += Testpivot_SelectionChanged; | |
} | |
void Testpivot_SelectionChanged(object sender, SelectionChangedEventArgs e) | |
{ | |
switch ((sender as Pivot).SelectedIndex) | |
{ | |
case 0: | |
ApplicationBar = ((ApplicationBar)this.Resources["Appbar1"]); | |
break; | |
case 1: | |
ApplicationBar = ((ApplicationBar)this.Resources["Appbar2"]); | |
break; | |
} | |
} | |
} | |
} |
可以看到在不同的PivotItem會有不同的ApplicationBar
0 意見:
張貼留言