当前位置:编程学习 > C#/ASP.NET >>

Behaviors扩展----根据Pivot的item自动切换AppBar

 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
    

补充:软件开发 , C# ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,