Pivot是Windows Phone中的常用控件,我們經常需要根據PivotItem的切換使用不同的AppBar,在此我提供一個Behaviors
來自動管理AppBar,省去手動切換的麻煩。
看代碼:
[csharp]
[ContentProperty("AppBars")]
public class PivotAppBarBehavior : Behavior<Pivot>
{
PhoneApplicationPage _page;
public PhoneApplicationPage ParentPage
{
get
{
if (_page == null && this.AssociatedObject!=null)
_page = this.AssociatedObject.GetParentPhonePage() as PhoneApplicationPage;
return _page;
}
}
public static readonly DependencyProperty AppBarsProperty = DependencyProperty.Register("AppBars", typeof(List<IApplicationBar>), typeof(PivotAppBarBehavior), null);
public List<IApplicationBar> AppBars
{
get
{
var appBars = base.GetValue(AppBarsProperty) as List<IApplicationBar>;
if (appBars == null)
{
appBars = new List<IApplicationBar>();
base.SetValue(PivotAppBarBehavior.AppBarsProperty, appBars);
}
return appBars;
}
set
{
base.SetValue(AppBarsProperty, value);
}
}
void pivot_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
if (ParentPage == null)
return;
if (AssociatedObject != null && AppBars != null && AppBars.Count > AssociatedObject.SelectedIndex)
{
IApplicationBar appbar = AppBars[AssociatedObject.SelectedIndex];
if (appbar is AppBar)
{
AppBar bar = appbar as AppBar;
if (bar != null)
ParentPage.ApplicationBar = bar.ApplicationBar;
}
else
{
ParentPage.ApplicationBar = AppBars[AssociatedObject.SelectedIndex];
}
}
else
{
ParentPage.ApplicationBar = null;
}
}
protected override void OnAttached()
{
base.OnAttached();
Pivot pivot = this.AssociatedObject as Pivot;
if (pivot != null)
pivot.SelectionChanged += pivot_SelectionChanged;
}
protected override void OnDetaching()
{
base.OnDetaching();
Pivot pivot = this.AssociatedObject as Pivot;
if (pivot != null)
pivot.SelectionChanged -= pivot_SelectionChanged;
}
[ContentProperty("AppBars")]
public class PivotAppBarBehavior : Behavior<Pivot>
{
PhoneApplicationPage _page;
public PhoneApplicationPage ParentPage
{
get
{
if (_page == null && this.AssociatedObject!=null)
_page = this.AssociatedObject.GetParentPhonePage() as PhoneApplicationPage;
return _page;
}
}
public static readonly DependencyProperty AppBarsProperty = DependencyProperty.Register("AppBars", typeof(List<IApplicationBar>), typeof(PivotAppBarBehavior), null);
public List<IApplicationBar> AppBars
{
get
{
var appBars = base.GetValue(AppBarsProperty) as List<IApplicationBar>;
if (appBars == null)
{
appBars = new List<IApplicationBar>();
base.SetValue(PivotAppBarBehavior.AppBarsProperty, appBars);
}
return appBars;
}
set
{
base.SetValue(AppBarsProperty, value);
}
}
void pivot_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
if (ParentPage == null)
return;
if (AssociatedObject != null && AppBars != null && AppBars.Count > AssociatedObject.SelectedIndex)
{
IApplicationBar appbar = AppBars[AssociatedObject.SelectedIndex];
if (appbar is AppBar)
{
AppBar bar = appbar as AppBar;
if (bar != null)
ParentPage.ApplicationBar = bar.ApplicationBar;
}
else
{
ParentPage.ApplicationBar = AppBars[AssociatedObject.SelectedIndex];
}
}
else
{
ParentPage.ApplicationBar = null;
}
}
protected override void OnAttached()
{
base.OnAttached();
Pivot pivot = this.AssociatedObject as Pivot;
if (pivot != null)
pivot.SelectionChanged += pivot_SelectionChanged;
}
protected override void OnDetaching()
{
base.OnDetaching();
Pivot pivot = this.AssociatedObject as Pivot;
if (pivot != null)
pivot.SelectionChanged -= pivot_SelectionChanged;
}
這個Behavior中保存一個AppBar的列表,並且會監聽Pivot的Item切換事件,根據Item顯示對應的AppBar,另外還支持
我自定義的一個AppBar,這個AppBar可以使用命令綁定,使用時會很方便,關於這個AppBar點擊這裡:http://www.devdiv.com/home.php?mod=space&uid=55433&do=blog&quickforward=1&id=50584。
如何使用:
[html] <controls:Pivot>
<i:Interaction.Behaviors>
<local:PivotAppBarBehavior>
<local:AppBar Width="0" Height="0" >
<local:AppBarIconButton Text="添加" IconUri="/appbar.new.rest.png" Command="{Binding AddItemCommand,Source={StaticResource viewmodel}}" CommandParameter="Add"/>
<local:AppBarIconButton Text="減少" IconUri="/appbar.cancel.rest.png" Command="{Binding DecreaseItemCommand,Source={StaticResource viewmodel}}"/>
</local:AppBar>
<local:AppBar Width="0" Height="0" >
<local:AppBarIconButton Text="添加" IconUri="/appbar.new.rest.png" Command="{Binding AddItemCommand,Source={StaticResource viewmodel}}" CommandParameter="Add"/>
<local:AppBar.Menus>
<local:AppBarMenuItem Text="減少" Command="{Binding DecreaseItemCommand,Source={StaticResource viewmodel}}"/>
</local:AppBar.Menus>
</local:AppBar>
<local:AppBar Width="0" Height="0" >
<local:AppBarIconButton Text="減少" IconUri="/appbar.cancel.rest.png" Command="{Binding DecreaseItemCommand,Source={StaticResource viewmodel}}"/>
<local:AppBar.Menus>
<local:AppBarMenuItem Text="添加" Command="{Binding AddItemCommand,Source={StaticResource viewmodel}}" CommandParameter="PivotItem3Add"/>
</local:AppBar.Menus>
</local:AppBar>
</local:PivotAppBarBehavior>
</i:Interaction.Behaviors>
<controls:Pivot>
<i:Interaction.Behaviors>
<local:PivotAppBarBehavior>
<local:AppBar Width="0" Height="0" >
<local:AppBarIconButton Text="添加" IconUri="/appbar.new.rest.png" Command="{Binding AddItemCommand,Source={StaticResource viewmodel}}" CommandParameter="Add"/>
<local:AppBarIconButton Text="減少" IconUri="/appbar.cancel.rest.png" Command="{Binding DecreaseItemCommand,Source={StaticResource viewmodel}}"/>
</local:AppBar>
<local:AppBar Width="0" Height="0" >
<local:AppBarIconButton Text="添加" IconUri="/appbar.new.rest.png" Command="{Binding AddItemCommand,Source={StaticResource viewmodel}}" CommandParameter="Add"/>
<local:AppBar.Menus>
<local:AppBarMenuItem Text="減少" Command="{Binding DecreaseItemCommand,Source={StaticResource viewmodel}}"/>
</local:AppBar.Menus>
</local:AppBar>
<local:AppBar Width="0" Height="0" >
<local:AppBarIconButton Text="減少" IconUri="/appbar.cancel.rest.png" Command="{Binding DecreaseItemCommand,Source={StaticResource viewmodel}}"/>
<local:AppBar.Menus>
<local:AppBarMenuItem Text="添加" Command="{Binding AddItemCommand,Source={StaticResource viewmodel}}" CommandParameter="PivotItem3Add"/>
</local:AppBar.Menus>
</local:AppBar>
</local:PivotAppBarBehavior>
</i:Interaction.Behaviors> 使用與正常的Behavior是一樣的,注意第一個AppBar對應第一個PivotItem,第二個AppBar對應第二個PivotItem,以此類推。
看效果: