【WP開發】實現Pivot控件單個PivotItem的全屏化

在新浪微博WP7版中有一個蠻酷的特效,就是滑動list的時候直接全屏化界面。
        一直想實現這個特效,最終還是類似的實現了這個特效。

7cc05e2a-a2db-3872-a3ab-71bf0f1e9000.jpg

2012-6-30 22:12:51 上傳
下載附件 (65.41 KB)


對於ApplicationBar還是很好解決的,直接對IsVisible屬性設置爲false就能實現,而對上面的Header進行進行隱藏就可以了,經過一番實驗貌似用Margin設爲負數就能夠解決。

好接下去看代碼:
首先是佈局文件。


  1. <controls:Pivot x:Name="FeaturePivot" Title="{Binding Title}" Foreground="{StaticResource PhoneAccentBrush}">
  2.             <controls:PivotItem x:Name="StoryPivotItem">
  3.                 <controls:PivotItem.Header>
  4.                     <TextBlock Text="{Binding Storys.Title}" FontSize="64" VerticalAlignment="Center" HorizontalAlignment="Center" Margin="0,10,0,0" Foreground="{StaticResource PhoneAccentBrush}"></TextBlock>
  5.                 </controls:PivotItem.Header>
  6.                 <lw:StoryList x:Name="HomeTimeLine" DataContext="{Binding Storys}" />
  7.             </controls:PivotItem>
  8. </controls:Pivot>
複製代碼


這裏的lw:StoryList是一個UserControl,裏面是一個List和ListItem的模版文本,這裏不用去管它。但後面它困擾了我很久。
好,由於這是擴展的交互方式,我們可以不用嚴格的把BackCode寫在ViewModel裏,我們就直接寫在xaml.cs裏就行了。


  1.         void FullScreenAction()
  2.         {
  3.             if (this.ApplicationBar.IsVisible == false)
  4.                 return;
  5.             this.FeaturePivot.Margin = new Thickness(0, -158, 0, 0);
  6.             this.GlobalProgressBar.Margin = new Thickness(0);
  7.             this.ApplicationBar.IsVisible = false;
  8.             this.BackKeyPress += OnFullScreenBackKeyPress;
  9.         }
  10.         void StopFullScreenAction()
  11.         {
  12.             if (this.ApplicationBar.IsVisible == true)
  13.                 return;
  14.             this.GlobalProgressBar.Margin = new Thickness(80, 32, 120, 0);
  15.             this.ApplicationBar.IsVisible = true;
  16.             this.FeaturePivot.Margin = new Thickness(0);
  17.             this.BackKeyPress -= OnFullScreenBackKeyPress;
  18.         }
  19.         private void OnFullScreenBackKeyPress(object sender, System.ComponentModel.CancelEventArgs e)
  20.         {
  21.             StopFullScreenAction();
  22.             e.Cancel = true;
  23.         }
複製代碼


這是最挫的實現方式,直接this.FeaturePivot.Margin = new Thickness(0, -158, 0, 0);設置Margin爲負值,能達到全屏的效果,但直接隱藏header用戶體驗很不好。並且這個時候還是能夠左右滑動
PS:這裏需要注意按後退鍵是能夠退出全屏的,並在退出時清空BackKeyPress這個Event,防止阻礙用戶正常退出App。

事情一件一件來,先解決左右滑動的問題。toolkit中有一個控件叫lockablePivot,在這裏它就可以用了。

將上面的xml代碼中的controls:Pivot替換成toolkit:LockablePivot,其他不用變,在backcode中再放入一些代碼:


  1. void FullScreenAction()
  2.         {
  3.             if (this.ApplicationBar.IsVisible == false)
  4.                 return;
  5.             this.FeaturePivot.Margin = new Thickness(0, -158, 0, 0);
  6.             this.GlobalProgressBar.Margin = new Thickness(0);
  7.             this.ApplicationBar.IsVisible = false;
  8.             this.FeaturePivot.IsLocked = true; //added code
  9.             this.BackKeyPress += OnFullScreenBackKeyPress;
  10.         }
  11.         void StopFullScreenAction()
  12.         {
  13.             if (this.ApplicationBar.IsVisible == true)
  14.                 return;
  15.             this.GlobalProgressBar.Margin = new Thickness(80, 32, 120, 0);
  16.             this.ApplicationBar.IsVisible = true;
  17.             this.FeaturePivot.Margin = new Thickness(0);
  18.             this.FeaturePivot.IsLocked = false; //added code
  19.             this.BackKeyPress -= OnFullScreenBackKeyPress;
  20.         }
  21.         private void OnFullScreenBackKeyPress(object sender, System.ComponentModel.CancelEventArgs e)
  22.         {
  23.             StopFullScreenAction();
  24.             e.Cancel = true;
  25.         }
複製代碼

只是增加this.FeaturePivot.IsLocked的定值操作就能完美的解決全屏時能滑動的現象。


接下來,貌似得加點動畫才感覺有較好的用戶體驗,好,繼續,我們會對上面的代碼繼續優化。

到加動畫的時候又犯難了,尼瑪查文檔發現Storyboard根本不支持Margin屬性的DoubleAnimation類型的動畫,因爲Margin在SL中被定義成一個Object,而DoubleAnimation只能對數值類進行補間動畫。
不過還是有方案的,就是用RenderTransform下的CompositeTransform的Translate系列屬性。

直接看代碼。


  1. <phone:PhoneApplicationPage.Resources>
  2.         <Storyboard x:Name="TitleDispear">
  3.             <DoubleAnimation Duration="0:0:1" To="-158" Storyboard.TargetProperty="(UIElement.RenderTransform).(CompositeTransform.TranslateY)"
  4.             Storyboard.TargetName="FeaturePivot" d:IsOptimized="True">
  5.                 <DoubleAnimation.EasingFunction>
  6.                     <CubicEase EasingMode="EaseOut" />
  7.                 </DoubleAnimation.EasingFunction>
  8.             </DoubleAnimation>
  9.         </Storyboard>
  10.         <Storyboard x:Name="TitleAppear">
  11.             <DoubleAnimation Duration="0:0:1" To="0" Storyboard.TargetProperty="(UIElement.RenderTransform).(CompositeTransform.TranslateY)"
  12.             Storyboard.TargetName="FeaturePivot" d:IsOptimized="True">
  13.                 <DoubleAnimation.EasingFunction>
  14.                     <CubicEase EasingMode="EaseOut" />
  15.                 </DoubleAnimation.EasingFunction>
  16.             </DoubleAnimation>
  17.         </Storyboard>
  18. </phone:PhoneApplicationPage.Resources>
  19. <!---------------code---------------->
  20. <toolkit:LockablePivot x:Name="FeaturePivot" Title="{Binding Title}" Foreground="{StaticResource PhoneAccentBrush}">
  21.             <toolkit:LockablePivot.RenderTransform>
  22.                 <CompositeTransform />
  23.             </toolkit:LockablePivot.RenderTransform>
  24.             <i:Interaction.Triggers>
  25.                 <i:EventTrigger EventName="SelectionChanged">
  26.                     <cmd:EventToCommand Command="{Binding ChangeSelectionCommand}"  CommandParameter="{Binding ElementName=FeaturePivot}" />
  27.                 </i:EventTrigger>
  28.             </i:Interaction.Triggers>
  29.             <controls:PivotItem x:Name="StoryPivotItem">
  30.                 <controls:PivotItem.RenderTransform>
  31.                     <CompositeTransform />
  32.                 </controls:PivotItem.RenderTransform>
  33.                 <controls:PivotItem.Header>
  34.                     <TextBlock Text="{Binding Storys.Title}" FontSize="64" VerticalAlignment="Center" HorizontalAlignment="Center" Margin="0,10,0,0" Foreground="{StaticResource PhoneAccentBrush}"></TextBlock>
  35.                 </controls:PivotItem.Header>
  36.                 <lw:StoryList x:Name="HomeTimeLine" DataContext="{Binding Storys}" />
  37.             </controls:PivotItem>
  38. <!------------code--------------->
  39. </toolkit:LockablePivot>
複製代碼

在上面的XML文件中定義了兩個Storyboard動畫資源,全屏化時讓FeaturePivot的TranslateY由0->-158,向上溢出,這樣就是實現了header的隱藏,所以TranslateY和margin都是可以隱藏header的,但有一點很大的不同,

TranslateY不會改變UIElement的大小,而Margin會改變,負數等於是拉伸大小。於是這裏問題就產生了,如果光設置TranslateY會導致整個Pivot下方有一部分空白區域。。。所以要着手解決這個問題,也就是這個問題困然了我很久。。。。

看backcode代碼:

  1.         void FullScreenAction()
  2.         {
  3.             if (this.ApplicationBar.IsVisible == false)
  4.                 return;
  5.             this.FeaturePivot.Margin = new Thickness(0, 0, 0, -158);
  6.             this.GlobalProgressBar.Margin = new Thickness(0);
  7.             TitleDispear.Begin();
  8.             this.ApplicationBar.IsVisible = false;
  9.             this.FeaturePivot.IsLocked = true;
  10.             this.BackKeyPress += OnFullScreenBackKeyPress;
  11.         }
  12.         void StopFullScreenAction()
  13.         {
  14.             if (this.ApplicationBar.IsVisible == true)
  15.                 return;
  16.             this.GlobalProgressBar.Margin = new Thickness(80, 32, 120, 0);
  17.             this.ApplicationBar.IsVisible = true;
  18.             TitleAppear.Begin();
  19.             this.FeaturePivot.Margin = new Thickness(0);
  20.             this.FeaturePivot.IsLocked = false;
  21.             this.BackKeyPress -= OnFullScreenBackKeyPress;
  22.         } //code
複製代碼



這裏我們再start時先對Pivot的Margin的bottom設置爲-158,用戶這時候其實是感知不到pivot的大小變化的,因爲溢出的看不到,接下來在進行動畫的begin操作。就能完美的實現全屏模式了。

慢着,如何觸發全屏這個動作呢,新浪是這麼做的:用戶快速下滑List的時候會開啓,而上拉時關閉。接下來得加上手勢。

我們要用到toolkit中的GestureService行爲了。

前端代碼:


  1.           <controls:PivotItem x:Name="StoryPivotItem">
  2.                 <controls:PivotItem.RenderTransform>
  3.                     <CompositeTransform />
  4.                 </controls:PivotItem.RenderTransform>
  5.                 <controls:PivotItem.Header>
  6.                     <TextBlock Text="{Binding Storys.Title}" FontSize="64" VerticalAlignment="Center" HorizontalAlignment="Center" Margin="0,10,0,0" Foreground="{StaticResource PhoneAccentBrush}"></TextBlock>
  7.                 </controls:PivotItem.Header>
  8. <!-- 增加下面的語句 -->
  9.                 <toolkit:GestureService.GestureListener>
  10.                     <toolkit:GestureListener Flick="OnStoryGestureListenerFlick"/>
  11.                 </toolkit:GestureService.GestureListener>
  12. <!-- END -->
  13.                 <lw:StoryList x:Name="HomeTimeLine" DataContext="{Binding Storys}" />
  14.             </controls:PivotItem>
複製代碼



BackCode:


  1.         private void OnStoryGestureListenerFlick(object sender, FlickGestureEventArgs e)
  2.         {
  3.             if (SettingsHelper.GetFullScreenMode() == "0")
  4.                 return;
  5.             if (e.Direction == System.Windows.Controls.Orientation.Horizontal) //判斷手勢方向
  6.                 return;
  7.             //if (e.Angle < 260 || e.Angle > 280) //角度判斷,可以不用
  8.             //    return;
  9.             if (e.VerticalVelocity < 0) //垂直速度判斷
  10.             {
  11.                 FullScreenAction();
  12.             }
  13.             else
  14.             {
  15.                 StopFullScreenAction();
  16.             }
  17.         }
複製代碼


在前端中加入上面的語句來聲明一個手勢bind,在backcode中進行ActionBind,SettingsHelper.GetFullScreenMode()是設置信息,默認是開啓的。
其他的看註釋。

好,這樣就差不多實現了新浪微博類似的全屏模式。。。出個效果圖(順便宣傳一下來往~~,哇咔咔)

普通界面:

aaa8278a-adcc-3f03-9d88-80f8e777cc10.png
2012-6-30 22:12:51 上傳
下載附件 (122.54 KB)



換膚加全屏界面:

70b57882-a02e-3260-8502-7dc169356853.png
2012-6-30 22:12:51 上傳
下載附件 (138.75 KB)




Hummm,尼瑪還是全屏神馬的才能看得更多嘛!!

 

http://www.itboat.net/thread-38914-1-1.html


 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章