本篇文章是一篇关于切换添加的帖子
Pivot是Windows Phone中的用常控件,我们经常要需根据PivotItem的切换应用不同的AppBar,在此我供给一个Behaviors
来主动理管AppBar,省去手动切换的烦麻。
看码代:
[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。
如何应用:
<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,以此类推。
看效果:
文章结束给大家分享下程序员的一些笑话语录: 乔布斯:怎么样还是咱安全吧!黑客:你的浏览器支持国内网银吗?苹果可以玩国内的网游吗乔布斯:......不可以黑客:那我研究你的漏洞干嘛,我也需要买奶粉!