WPF如何实现类似iPhone界面切换的效果(转载)

摘要:
渲染1:渲染2:设计思想将这些多个界面放入方向为水平的StackPanel中。单击“下一步”时,StackPanel中的所有控件都会执行TransformTransform动画。

WPF如何实现类似iPhone界面切换的效果 (version .1)

转自:http://blog.csdn.net/fallincloud/article/details/6968764

在论坛上见到有人提出了这个问题(WPF实现点击横向切换界面

我简单地做了个Sample。

效果图1:

效果图1

效果图2:

效果图2

设计思路

将这多个界面放入一个Orientation为Horizontal的StackPanel当中,点击Next时,里面所有控件执行TranslteTransform动画。

实现

xaml

  1. <Windowx:Class="WPFNavigation.MainWindow"
  2. xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  3. xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  4. Title="MainWindow"Height="350"Width="400">
  5. <Grid>
  6. <Grid.RowDefinitions>
  7. <RowDefinitionHeight="*"></RowDefinition>
  8. <RowDefinitionHeight="Auto"></RowDefinition>
  9. </Grid.RowDefinitions>
  10. <StackPanelOrientation="Horizontal"
  11. x:Name="NavigationPanel"
  12. Height="300"
  13. HorizontalAlignment="Left"
  14. VerticalAlignment="Top">
  15. <GridWidth="{BindingRelativeSource={RelativeSourceMode=FindAncestor,AncestorLevel=1,AncestorType={x:TypeWindow}},Path=ActualWidth}"
  16. Height="{BindingRelativeSource={RelativeSourceMode=FindAncestor,AncestorLevel=1,AncestorType={x:TypeStackPanel}},Path=ActualHeight}"
  17. Background="Blue">
  18. <TextBlockFontSize="36">Page1</TextBlock>
  19. </Grid>
  20. <GridWidth="{BindingRelativeSource={RelativeSourceMode=FindAncestor,AncestorLevel=1,AncestorType={x:TypeWindow}},Path=ActualWidth}"
  21. Height="{BindingRelativeSource={RelativeSourceMode=FindAncestor,AncestorLevel=1,AncestorType={x:TypeStackPanel}},Path=ActualHeight}"
  22. Background="Violet">
  23. <TextBlockFontSize="36">Page2</TextBlock>
  24. </Grid>
  25. <GridWidth="{BindingRelativeSource={RelativeSourceMode=FindAncestor,AncestorLevel=1,AncestorType={x:TypeWindow}},Path=ActualWidth}"
  26. Height="{BindingRelativeSource={RelativeSourceMode=FindAncestor,AncestorLevel=1,AncestorType={x:TypeStackPanel}},Path=ActualHeight}"
  27. Background="Purple">
  28. <TextBlockFontSize="36">Page3</TextBlock>
  29. </Grid>
  30. </StackPanel>
  31. <StackPanelGrid.Row="1"Orientation="Horizontal">
  32. <ButtonContent="Previous"x:Name="ButtonPreviousPage"Click="ButtonPreviousPage_Click"></Button>
  33. <ButtonContent="Next"x:Name="ButtonNextPage"Click="ButtonNextPage_Click"></Button>
  34. </StackPanel>
  35. </Grid>
  36. </Window>

cs中

    1. ///<summary>
    2. ///InteractionlogicforMainWindow.xaml
    3. ///</summary>
    4. publicpartialclassMainWindow:Window
    5. {
    6. privatestaticreadonlydoubleCOUNT_PAGE=3;
    7. privateTranslateTransformNavigationPanelTranslateTransform;
    8. publicMainWindow()
    9. {
    10. InitializeComponent();
    11. NavigationPanelTranslateTransform=newTranslateTransform();
    12. this.Loaded+=newRoutedEventHandler(MainWindow_Loaded);
    13. }
    14. voidMainWindow_Loaded(objectsender,RoutedEventArgse)
    15. {
    16. foreach(FrameworkElementfeinNavigationPanel.Children)
    17. {
    18. fe.RenderTransform=NavigationPanelTranslateTransform;
    19. }
    20. DeterminButtonStates();
    21. }
    22. privatevoidDeterminButtonStates()
    23. {
    24. doublecurrentTranX=NavigationPanelTranslateTransform.X;
    25. if(currentTranX>=0)
    26. {
    27. ButtonPreviousPage.IsEnabled=false;
    28. }
    29. elseif(currentTranX<=-(COUNT_PAGE-1)*this.ActualWidth)
    30. {
    31. ButtonNextPage.IsEnabled=false;
    32. }
    33. else
    34. {
    35. ButtonPreviousPage.IsEnabled=true;
    36. ButtonNextPage.IsEnabled=true;
    37. }
    38. }
    39. privatevoidButtonPreviousPage_Click(objectsender,RoutedEventArgse)
    40. {
    41. doublecurrentTranX=NavigationPanelTranslateTransform.X;
    42. DoubleAnimationda=newDoubleAnimation(currentTranX,currentTranX+this.ActualWidth,TimeSpan.FromMilliseconds(250));
    43. da.Completed+=(o1,e1)=>
    44. {
    45. DeterminButtonStates();
    46. };
    47. NavigationPanelTranslateTransform.BeginAnimation(TranslateTransform.XProperty,da);
    48. }
    49. privatevoidButtonNextPage_Click(objectsender,RoutedEventArgse)
    50. {
    51. doublecurrentTranX=NavigationPanelTranslateTransform.X;
    52. DoubleAnimationda=newDoubleAnimation(currentTranX,currentTranX-this.ActualWidth,TimeSpan.FromMilliseconds(250));
    53. da.Completed+=(o1,e1)=>
    54. {
    55. DeterminButtonStates();
    56. };
    57. NavigationPanelTranslateTransform.BeginAnimation(TranslateTransform.XProperty,da);
    58. }
    59. }

WPF如何实现类似iPhone界面切换的效果 (version .2)

转自:http://blog.csdn.net/fallincloud/article/details/6969329

前面写了篇WPF如何实现类似iPhone界面切换的效果 (version .1)

现在又花了点时间重构了下,将动画的效果和Previous和Next这两个按钮的状态控制都封装了起来,效果依然和前面一样

不过重用性高了许多。使用方法如下:

XAML:

  1. <Windowx:Class="WPFNavigation.MainWindow"
  2. xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  3. xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  4. xmlns:local="clr-namespace:WPFNavigation"
  5. Title="MainWindow"Height="350"Width="400">
  6. <Grid>
  7. <Grid.RowDefinitions>
  8. <RowDefinitionHeight="*"></RowDefinition>
  9. <RowDefinitionHeight="Auto"></RowDefinition>
  10. </Grid.RowDefinitions>
  11. <local:NavigationPanelOrientation="Horizontal"
  12. x:Name="NavigationPanel"
  13. Height="300"
  14. HorizontalAlignment="Left"
  15. VerticalAlignment="Top">
  16. <GridWidth="{BindingRelativeSource={RelativeSourceMode=FindAncestor,AncestorLevel=1,AncestorType={x:TypeWindow}},Path=ActualWidth}"
  17. Height="{BindingRelativeSource={RelativeSourceMode=FindAncestor,AncestorLevel=1,AncestorType={x:Typelocal:NavigationPanel}},Path=ActualHeight}"
  18. Background="Blue">
  19. <TextBlockFontSize="36">Page1</TextBlock>
  20. </Grid>
  21. <GridWidth="{BindingRelativeSource={RelativeSourceMode=FindAncestor,AncestorLevel=1,AncestorType={x:TypeWindow}},Path=ActualWidth}"
  22. Height="{BindingRelativeSource={RelativeSourceMode=FindAncestor,AncestorLevel=1,AncestorType={x:Typelocal:NavigationPanel}},Path=ActualHeight}"
  23. Background="Violet">
  24. <TextBlockFontSize="36">Page2</TextBlock>
  25. </Grid>
  26. <GridWidth="{BindingRelativeSource={RelativeSourceMode=FindAncestor,AncestorLevel=1,AncestorType={x:TypeWindow}},Path=ActualWidth}"
  27. Height="{BindingRelativeSource={RelativeSourceMode=FindAncestor,AncestorLevel=1,AncestorType={x:Typelocal:NavigationPanel}},Path=ActualHeight}"
  28. Background="Purple">
  29. <TextBlockFontSize="36">Page3</TextBlock>
  30. </Grid>
  31. </local:NavigationPanel>
  32. <StackPanelGrid.Row="1"Orientation="Horizontal">
  33. <ButtonContent="Previous"x:Name="ButtonPreviousPage"
  34. IsEnabled="{BindingElementName=NavigationPanel,Path=PreviousIsValid,Mode=OneWay}"
  35. Click="ButtonPreviousPage_Click"></Button>
  36. <ButtonContent="Next"x:Name="ButtonNextPage"Click="ButtonNextPage_Click"
  37. IsEnabled="{BindingElementName=NavigationPanel,Path=NextIsValid,Mode=OneWay}"></Button>
  38. </StackPanel>
  39. </Grid>
  40. </Window>

C#:

  1. ///<summary>
  2. ///InteractionlogicforMainWindow.xaml
  3. ///</summary>
  4. publicpartialclassMainWindow:Window
  5. {
  6. publicMainWindow()
  7. {
  8. InitializeComponent();
  9. }
  10. privatevoidButtonPreviousPage_Click(objectsender,RoutedEventArgse)
  11. {
  12. NavigationPanel.Previous();
  13. }
  14. privatevoidButtonNextPage_Click(objectsender,RoutedEventArgse)
  15. {
  16. NavigationPanel.Next();
  17. }
  18. }

以上是用法,封装的NavigationPanel设计如下:

NavigationPanel设计图

具体实现如下:

  1. usingSystem;
  2. usingSystem.Collections.Generic;
  3. usingSystem.Linq;
  4. usingSystem.Text;
  5. usingSystem.Windows;
  6. usingSystem.Windows.Controls;
  7. usingSystem.Windows.Data;
  8. usingSystem.Windows.Documents;
  9. usingSystem.Windows.Input;
  10. usingSystem.Windows.Media;
  11. usingSystem.Windows.Media.Imaging;
  12. usingSystem.Windows.Navigation;
  13. usingSystem.Windows.Shapes;
  14. usingSystem.Windows.Media.Animation;
  15. namespaceWPFNavigation
  16. {
  17. publicclassNavigationPanel:StackPanel
  18. {
  19. publiceventEventHandlerAnimationComplte;
  20. privatestaticdoubleCOUNT_OF_PAGE;
  21. privateTranslateTransformNavigationPanelTranslateTransform;
  22. privatestaticreadonlyTimeSpanDURATION=TimeSpan.FromMilliseconds(250);
  23. publicNavigationPanel():base()
  24. {
  25. this.Orientation=Orientation.Horizontal;
  26. NavigationPanelTranslateTransform=newTranslateTransform();
  27. this.Loaded+=newRoutedEventHandler(NavigationPanel_Loaded);
  28. }
  29. voidNavigationPanel_Loaded(objectsender,RoutedEventArgse)
  30. {
  31. COUNT_OF_PAGE=this.Children.Count;
  32. CurrentIndex=0;
  33. foreach(FrameworkElementfeinthis.Children)
  34. {
  35. fe.RenderTransform=NavigationPanelTranslateTransform;
  36. }
  37. }
  38. publicvoidNext()
  39. {
  40. AnimationChildren(true);
  41. }
  42. publicvoidPrevious()
  43. {
  44. AnimationChildren(false);
  45. }
  46. privateboolValidateNext()
  47. {
  48. returnCurrentIndex<(COUNT_OF_PAGE-1)&&CurrentIndex>=0;
  49. }
  50. privateboolValidatePrevious()
  51. {
  52. returnCurrentIndex<=(COUNT_OF_PAGE-1)&&CurrentIndex>0;
  53. }
  54. privateboolValidateCurrentIndex()
  55. {
  56. if(CurrentIndex>-1&&CurrentIndex<this.Children.Count)
  57. {
  58. returntrue;
  59. }
  60. returnfalse;
  61. }
  62. privatevoidAnimationChildren(boolforward)
  63. {
  64. doublecurrentTranX=NavigationPanelTranslateTransform.X;
  65. doublenextTranX=currentTranX;
  66. if(forward)
  67. {
  68. if(ValidateCurrentIndex())
  69. {
  70. nextTranX-=(this.Children[CurrentIndex]asFrameworkElement).ActualWidth;
  71. }
  72. }
  73. else
  74. {
  75. if(ValidateCurrentIndex())
  76. {
  77. nextTranX+=(this.Children[CurrentIndex]asFrameworkElement).ActualWidth;
  78. }
  79. }
  80. DoubleAnimationda=newDoubleAnimation(currentTranX,nextTranX,DURATION);
  81. da.Completed+=(o1,e1)=>
  82. {
  83. CurrentIndex+=forward?1:-1;
  84. if(AnimationComplte!=null)
  85. {
  86. AnimationComplte(this,e1);
  87. }
  88. };
  89. NavigationPanelTranslateTransform.BeginAnimation(TranslateTransform.XProperty,da);
  90. }
  91. #regionCurrentIndex
  92. ///<summary>
  93. ///The<seecref="CurrentIndex"/>dependencyproperty'sname.
  94. ///</summary>
  95. publicconststringCurrentIndexPropertyName="CurrentIndex";
  96. ///<summary>
  97. ///Getsorsetsthevalueofthe<seecref="CurrentIndex"/>
  98. ///property.Thisisadependencyproperty.
  99. ///</summary>
  100. publicintCurrentIndex
  101. {
  102. get
  103. {
  104. return(int)GetValue(CurrentIndexProperty);
  105. }
  106. set
  107. {
  108. SetValue(CurrentIndexProperty,value);
  109. }
  110. }
  111. ///<summary>
  112. ///Identifiesthe<seecref="CurrentIndex"/>dependencyproperty.
  113. ///</summary>
  114. publicstaticreadonlyDependencyPropertyCurrentIndexProperty=DependencyProperty.Register(
  115. CurrentIndexPropertyName,
  116. typeof(int),
  117. typeof(NavigationPanel),
  118. newUIPropertyMetadata(-1,OnCurrentIndexChanged));
  119. privatestaticvoidOnCurrentIndexChanged(DependencyObjectd,DependencyPropertyChangedEventArgse)
  120. {
  121. (dasNavigationPanel).OnCurrentIndexChanged((int)e.OldValue,(int)e.NewValue);
  122. }
  123. protectedvirtualvoidOnCurrentIndexChanged(intoldIndex,intnewIndex)
  124. {
  125. NextIsValid=ValidateNext();
  126. PreviousIsValid=ValidatePrevious();
  127. }
  128. #endregion//CurrentIndex
  129. #regionNextIsValid
  130. ///<summary>
  131. ///The<seecref="NextIsValid"/>dependencyproperty'sname.
  132. ///</summary>
  133. publicconststringNextIsValidPropertyName="NextIsValid";
  134. ///<summary>
  135. ///Getsorsetsthevalueofthe<seecref="NextIsValid"/>
  136. ///property.Thisisadependencyproperty.
  137. ///</summary>
  138. publicboolNextIsValid
  139. {
  140. get
  141. {
  142. return(bool)GetValue(NextIsValidProperty);
  143. }
  144. set
  145. {
  146. SetValue(NextIsValidProperty,value);
  147. }
  148. }
  149. ///<summary>
  150. ///Identifiesthe<seecref="NextIsValid"/>dependencyproperty.
  151. ///</summary>
  152. publicstaticreadonlyDependencyPropertyNextIsValidProperty=DependencyProperty.Register(
  153. NextIsValidPropertyName,
  154. typeof(bool),
  155. typeof(NavigationPanel),
  156. newUIPropertyMetadata(false));
  157. #endregion//NextIsValid
  158. #regionPreviousIsValid
  159. ///<summary>
  160. ///The<seecref="PreviousIsValid"/>dependencyproperty'sname.
  161. ///</summary>
  162. publicconststringPreviousIsValidPropertyName="PreviousIsValid";
  163. ///<summary>
  164. ///Getsorsetsthevalueofthe<seecref="PreviousIsValid"/>
  165. ///property.Thisisadependencyproperty.
  166. ///</summary>
  167. publicboolPreviousIsValid
  168. {
  169. get
  170. {
  171. return(bool)GetValue(PreviousIsValidProperty);
  172. }
  173. set
  174. {
  175. SetValue(PreviousIsValidProperty,value);
  176. }
  177. }
  178. ///<summary>
  179. ///Identifiesthe<seecref="PreviousIsValid"/>dependencyproperty.
  180. ///</summary>
  181. publicstaticreadonlyDependencyPropertyPreviousIsValidProperty=DependencyProperty.Register(
  182. PreviousIsValidPropertyName,
  183. typeof(bool),
  184. typeof(NavigationPanel),
  185. newUIPropertyMetadata(false));
  186. #endregion//PreviousIsValid
  187. }
  188. }

免责声明:文章转载自《WPF如何实现类似iPhone界面切换的效果(转载)》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇【代码更新】IIC协议建模——读写EEPROMABP框架 UnitOfWorkAttribute下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

[转]WPF焦点概述

WPF 中,有两个与焦点有关的主要概念:键盘焦点和逻辑焦点。 键盘焦点指接收键盘输入的元素,而逻辑焦点指焦点范围中具有焦点的元素。 本概述将详细介绍这些概念。 理解这些概念之间的区别对于创建具有可以获取焦点的多个区域的复杂应用程序是非常重要的。 参与焦点管理的主要类有 Keyboard 类、FocusManager 类以及基元素类(如 UIElement...

8天入门wpf(转)

8天入门wpf—— 第一天 基础概念介绍8天入门wpf—— 第二天 xaml详解8天入门wpf—— 第三天 样式8天入门wpf—— 第四天 模板8天入门wpf—— 第五天 数据绑定8天入门wpf—— 第六天 细说控件8天入门wpf—— 第七天 画刷8天入门wpf—— 第八天 最后的补充WPF 一个MVVM的简单例子 MVVM设计模式...

原生App切图的那些事儿

如何切图?  了解iphone界面的尺寸 最小的分辨率是320x480,我们把这个尺寸定为基准界面尺寸(baseline),基准尺寸所用的图标定为1倍图(1x)。  在实际设计过程中,为了降低设计成本,一般拿设备最高的分辨率作为设计稿的原始尺寸,拿iphone来说就是iphone5或5s的640x1136啦,当然也可以用iphone4或4s的640x96...

WPF使用Winform PDFView控件

最近开发wpf项目中有一个模块需要显示PDF文件内容。由于WPF本身没有PDF加载控件(似乎有收费的我查到过类似的资料。如果有新的pdf控件也请通知我一下谢谢)。 项目使用之前也是从网上获取的资料,因此接下来的控件命名和项目命名完全沿用原文档。具体地址忘记了。这样也好感谢原作者。 不过里面的逻辑根据项目实际应用做了变工 。每人理解不同为了大家更快更好地的使...

[WPF] UserControl vs CustomControl

介绍 WPF中有两种控件:UserControl和CustomControl,但是这两者有什么区别呢?这篇博客中将介绍两者之间的区别,这样可以在项目中合理的使用它们。 UserControl 将多个WPF控件(例如:TextBox,TextBlock,Button)进行组合成一个可复用的控件组; 由XAML和Code Behind代码组成; 不支持样式/...

WPF中如何为ItemsControl添加ScrollViewer并显示ScrollBar

    今天在开发的过程中突然碰到了一个问题,本来的意图是想当ItemsControl中加载的Item达到一定数量时,会出现ScrollViewer并出现垂直的滚动条,但是实际上并不能够达成目标,对于熟手来说这个问题非常简单,但是如果不了解WPF的模板的原理,可能并不清楚这些,这里举出一个例子来论证。 <Window x: xmln...