2013年9月2日 星期一

[WP] 在一個頁面中使用多組ApplicationBar

在Windows Phone的Pivot或Panorama頁面中,有時可能會因為PivotItem或PanoramaItem都有各自功能上的需求,所以出現必須要在同一個頁面中使用到不同的ApplicationBar情況,這種情況其實很容易就可以去完成,下面簡單用個Pivot Page來示範。


在這個Pivot Page中有兩個PivotItem,然後各自需要不同的ApplicationBar,但因為同一個頁面只能有一組的ApplicationBar,所以這邊就必須要把所有需要的ApplicationBar定義在Resources中,Xaml代碼如下
<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了。
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
imgimg2

0 意見:

張貼留言