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

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

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

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

我简单地做了个Sample。

效果图1:

效果图1

 

效果图2:

效果图2

设计思路

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

实现

xaml

 

 

[html]  view plain copy
 
  1. <Window x: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.             <RowDefinition Height="*"></RowDefinition>  
  8.             <RowDefinition Height="Auto"></RowDefinition>  
  9.         </Grid.RowDefinitions>  
  10.         <StackPanel Orientation="Horizontal"   
  11.                     x:Name="NavigationPanel"  
  12.                     Height="300"   
  13.                     HorizontalAlignment="Left"  
  14.                     VerticalAlignment="Top">  
  15.               
  16.             <Grid Width="{Binding RelativeSource={RelativeSource Mode=FindAncestor,AncestorLevel=1,AncestorType={x:Type Window}}, Path=ActualWidth }"  
  17.                   Height="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorLevel=1, AncestorType={x:Type StackPanel}}, Path=ActualHeight}"  
  18.                   Background="Blue" >  
  19.                 <TextBlock FontSize="36">Page1</TextBlock>  
  20.             </Grid>  
  21.             <Grid Width="{Binding RelativeSource={RelativeSource Mode=FindAncestor,AncestorLevel=1,AncestorType={x:Type Window}}, Path=ActualWidth }"  
  22.                   Height="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorLevel=1, AncestorType={x:Type StackPanel}}, Path=ActualHeight}"  
  23.                   Background="Violet">  
  24.                 <TextBlock FontSize="36">Page2</TextBlock>  
  25.             </Grid>  
  26.             <Grid Width="{Binding RelativeSource={RelativeSource Mode=FindAncestor,AncestorLevel=1,AncestorType={x:Type Window}}, Path=ActualWidth }"  
  27.                   Height="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorLevel=1, AncestorType={x:Type StackPanel}}, Path=ActualHeight}"  
  28.                   Background="Purple" >  
  29.                 <TextBlock FontSize="36">Page3</TextBlock>  
  30.             </Grid>  
  31.         </StackPanel>  
  32.           
  33.         <StackPanel Grid.Row="1"  Orientation="Horizontal" >  
  34.             <Button Content="Previous" x:Name="ButtonPreviousPage" Click="ButtonPreviousPage_Click"></Button>  
  35.             <Button Content="Next" x:Name="ButtonNextPage" Click="ButtonNextPage_Click"></Button>             
  36.         </StackPanel>  
  37.     </Grid>  
  38. </Window>  

 

 

cs中

 

 

[csharp]  view plain copy
 
    1. /// <summary>  
    2. /// Interaction logic for MainWindow.xaml  
    3. /// </summary>  
    4. public partial class MainWindow : Window  
    5. {  
    6.   
    7.     private static readonly double COUNT_PAGE = 3;  
    8.     private TranslateTransform NavigationPanelTranslateTransform;  
    9.   
    10.     public MainWindow()  
    11.     {  
    12.         InitializeComponent();  
    13.   
    14.         NavigationPanelTranslateTransform = new TranslateTransform();  
    15.           
    16.         this.Loaded += new RoutedEventHandler(MainWindow_Loaded);  
    17.     }  
    18.   
    19.     void MainWindow_Loaded(object sender, RoutedEventArgs e)  
    20.     {  
    21.         foreach (FrameworkElement fe in NavigationPanel.Children)  
    22.         {  
    23.             fe.RenderTransform = NavigationPanelTranslateTransform;  
    24.         }  
    25.   
    26.         DeterminButtonStates();  
    27.     }  
    28.   
    29.     private void DeterminButtonStates()  
    30.     {  
    31.         double currentTranX = NavigationPanelTranslateTransform.X;  
    32.   
    33.         if (currentTranX >= 0)  
    34.         {  
    35.             ButtonPreviousPage.IsEnabled = false;  
    36.         }  
    37.         else if (currentTranX <= -(COUNT_PAGE - 1) * this.ActualWidth)  
    38.         {  
    39.             ButtonNextPage.IsEnabled = false;  
    40.         }  
    41.         else  
    42.         {  
    43.             ButtonPreviousPage.IsEnabled = true;  
    44.             ButtonNextPage.IsEnabled = true;  
    45.         }  
    46.     }  
    47.   
    48.     private void ButtonPreviousPage_Click(object sender, RoutedEventArgs e)  
    49.     {  
    50.         double currentTranX = NavigationPanelTranslateTransform.X;  
    51.           
    52.         DoubleAnimation da = new DoubleAnimation(currentTranX, currentTranX+this.ActualWidth, TimeSpan.FromMilliseconds(250));  
    53.         da.Completed += (o1, e1) =>  
    54.             {  
    55.                 DeterminButtonStates();  
    56.             };  
    57.   
    58.         NavigationPanelTranslateTransform.BeginAnimation(TranslateTransform.XProperty, da);           
    59.     }  
    60.   
    61.     private void ButtonNextPage_Click(object sender, RoutedEventArgs e)  
    62.     {  
    63.         double currentTranX = NavigationPanelTranslateTransform.X;  
    64.   
    65.         DoubleAnimation da = new DoubleAnimation(currentTranX, currentTranX - this.ActualWidth, TimeSpan.FromMilliseconds(250));  
    66.         da.Completed += (o1, e1) =>  
    67.         {  
    68.             DeterminButtonStates();  
    69.         };  
    70.   
    71.         NavigationPanelTranslateTransform.BeginAnimation(TranslateTransform.XProperty, da);       
    72.     }  
    73. }  

 

 

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

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

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

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

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

 

XAML:

 

[html]  view plain copy
 
  1. <Window x: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.             <RowDefinition Height="*"></RowDefinition>  
  9.             <RowDefinition Height="Auto"></RowDefinition>  
  10.         </Grid.RowDefinitions>  
  11.         <local:NavigationPanel Orientation="Horizontal"   
  12.                     x:Name="NavigationPanel"  
  13.                     Height="300"   
  14.                     HorizontalAlignment="Left"  
  15.                     VerticalAlignment="Top">  
  16.               
  17.             <Grid Width="{Binding RelativeSource={RelativeSource Mode=FindAncestor,AncestorLevel=1,AncestorType={x:Type Window}}, Path=ActualWidth }"  
  18.                   Height="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorLevel=1, AncestorType={x:Type local:NavigationPanel}}, Path=ActualHeight}"  
  19.                   Background="Blue" >  
  20.                 <TextBlock FontSize="36">Page1</TextBlock>  
  21.             </Grid>  
  22.             <Grid Width="{Binding RelativeSource={RelativeSource Mode=FindAncestor,AncestorLevel=1,AncestorType={x:Type Window}}, Path=ActualWidth }"  
  23.                   Height="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorLevel=1, AncestorType={x:Type local:NavigationPanel}}, Path=ActualHeight}"  
  24.                   Background="Violet">  
  25.                 <TextBlock FontSize="36">Page2</TextBlock>  
  26.             </Grid>  
  27.             <Grid Width="{Binding RelativeSource={RelativeSource Mode=FindAncestor,AncestorLevel=1,AncestorType={x:Type Window}}, Path=ActualWidth }"  
  28.                   Height="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorLevel=1, AncestorType={x:Type local:NavigationPanel}}, Path=ActualHeight}"  
  29.                   Background="Purple" >  
  30.                 <TextBlock FontSize="36">Page3</TextBlock>  
  31.             </Grid>  
  32.         </local:NavigationPanel>  
  33.           
  34.         <StackPanel Grid.Row="1"  Orientation="Horizontal" >  
  35.             <Button Content="Previous" x:Name="ButtonPreviousPage"  
  36.                     IsEnabled="{Binding ElementName=NavigationPanel, Path=PreviousIsValid, Mode=OneWay}"  
  37.                     Click="ButtonPreviousPage_Click"></Button>  
  38.             <Button Content="Next" x:Name="ButtonNextPage" Click="ButtonNextPage_Click"  
  39.                     IsEnabled="{Binding ElementName=NavigationPanel, Path=NextIsValid, Mode=OneWay}"></Button>           
  40.         </StackPanel>  
  41.     </Grid>  
  42. </Window>  

 

 

C#:

 

 

[csharp]  view plain copy
 
  1. /// <summary>  
  2. /// Interaction logic for MainWindow.xaml  
  3. /// </summary>  
  4. public partial class MainWindow : Window  
  5. {  
  6.     public MainWindow()  
  7.     {  
  8.         InitializeComponent();        
  9.     }  
  10.   
  11.     private void ButtonPreviousPage_Click(object sender, RoutedEventArgs e)  
  12.     {  
  13.         NavigationPanel.Previous();  
  14.     }  
  15.   
  16.   
  17.     private void ButtonNextPage_Click(object sender, RoutedEventArgs e)  
  18.     {  
  19.         NavigationPanel.Next();  
  20.     }  
  21. }  



 

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

 

NavigationPanel设计图

 

具体实现如下:

 

 

[csharp]  view plain copy
 
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5. using System.Windows;  
  6. using System.Windows.Controls;  
  7. using System.Windows.Data;  
  8. using System.Windows.Documents;  
  9. using System.Windows.Input;  
  10. using System.Windows.Media;  
  11. using System.Windows.Media.Imaging;  
  12. using System.Windows.Navigation;  
  13. using System.Windows.Shapes;  
  14. using System.Windows.Media.Animation;  
  15.   
  16. namespace WPFNavigation  
  17. {  
  18.   
  19.     public class NavigationPanel : StackPanel  
  20.     {  
  21.         public event EventHandler AnimationComplte;  
  22.         private static double COUNT_OF_PAGE;  
  23.         private TranslateTransform NavigationPanelTranslateTransform;  
  24.       
  25.   
  26.         private static readonly TimeSpan DURATION = TimeSpan.FromMilliseconds(250);  
  27.   
  28.         public NavigationPanel():base()  
  29.         {  
  30.             this.Orientation = Orientation.Horizontal;  
  31.               
  32.   
  33.             NavigationPanelTranslateTransform = new TranslateTransform();  
  34.   
  35.             this.Loaded += new RoutedEventHandler(NavigationPanel_Loaded);            
  36.         }  
  37.   
  38.         void NavigationPanel_Loaded(object sender, RoutedEventArgs e)  
  39.         {  
  40.             COUNT_OF_PAGE = this.Children.Count;  
  41.             CurrentIndex = 0;  
  42.             foreach (FrameworkElement fe in this.Children)  
  43.             {  
  44.                 fe.RenderTransform = NavigationPanelTranslateTransform;  
  45.             }  
  46.         }  
  47.   
  48.         public void Next()  
  49.         {  
  50.             AnimationChildren(true);  
  51.         }  
  52.   
  53.         public void Previous()  
  54.         {  
  55.             AnimationChildren(false);  
  56.         }  
  57.   
  58.   
  59.         private bool ValidateNext()  
  60.         {  
  61.             return CurrentIndex < (COUNT_OF_PAGE - 1) && CurrentIndex >= 0;  
  62.         }  
  63.   
  64.   
  65.         private bool ValidatePrevious()  
  66.         {  
  67.             return CurrentIndex <= (COUNT_OF_PAGE - 1) && CurrentIndex > 0;  
  68.         }  
  69.   
  70.         private bool ValidateCurrentIndex()  
  71.         {  
  72.             if (CurrentIndex > -1 && CurrentIndex < this.Children.Count)  
  73.             {  
  74.                 return true;  
  75.             }  
  76.   
  77.             return false;  
  78.         }  
  79.   
  80.         private  void AnimationChildren(bool forward)  
  81.         {  
  82.             double currentTranX = NavigationPanelTranslateTransform.X;  
  83.             double nextTranX = currentTranX;  
  84.             if (forward)  
  85.             {  
  86.                 if (ValidateCurrentIndex())  
  87.                 {  
  88.                     nextTranX -= (this.Children[CurrentIndex] as FrameworkElement).ActualWidth;  
  89.                 }  
  90.                   
  91.             }  
  92.             else  
  93.             {  
  94.                 if (ValidateCurrentIndex())  
  95.                 {  
  96.                     nextTranX += (this.Children[CurrentIndex] as FrameworkElement).ActualWidth;  
  97.                 }  
  98.                   
  99.             }  
  100.   
  101.             DoubleAnimation da = new DoubleAnimation(currentTranX, nextTranX, DURATION);  
  102.             da.Completed += (o1, e1) =>  
  103.             {  
  104.                 CurrentIndex += forward ? 1 : -1;  
  105.                 if (AnimationComplte != null)  
  106.                 {  
  107.                     AnimationComplte(this, e1);  
  108.                 }  
  109.             };  
  110.   
  111.             NavigationPanelTranslateTransform.BeginAnimation(TranslateTransform.XProperty, da);  
  112.         }  
  113.  
  114.         #region CurrentIndex  
  115.   
  116.         /// <summary>  
  117.         /// The <see cref="CurrentIndex" /> dependency property's name.  
  118.         /// </summary>  
  119.         public const string CurrentIndexPropertyName = "CurrentIndex";  
  120.   
  121.         /// <summary>  
  122.         /// Gets or sets the value of the <see cref="CurrentIndex" />  
  123.         /// property. This is a dependency property.  
  124.         /// </summary>  
  125.         public int CurrentIndex  
  126.         {  
  127.             get  
  128.             {  
  129.                 return (int)GetValue(CurrentIndexProperty);  
  130.             }  
  131.             set  
  132.             {  
  133.                 SetValue(CurrentIndexProperty, value);  
  134.             }  
  135.         }  
  136.   
  137.         /// <summary>  
  138.         /// Identifies the <see cref="CurrentIndex" /> dependency property.  
  139.         /// </summary>  
  140.         public static readonly DependencyProperty CurrentIndexProperty = DependencyProperty.Register(  
  141.             CurrentIndexPropertyName,  
  142.             typeof(int),  
  143.             typeof(NavigationPanel),  
  144.             new UIPropertyMetadata(-1, OnCurrentIndexChanged));  
  145.   
  146.         private static void OnCurrentIndexChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)  
  147.         {  
  148.             (d as NavigationPanel).OnCurrentIndexChanged((int)e.OldValue, (int)e.NewValue);  
  149.         }  
  150.   
  151.         protected virtual void OnCurrentIndexChanged(int oldIndex, int newIndex)  
  152.         {  
  153.             NextIsValid = ValidateNext();  
  154.             PreviousIsValid = ValidatePrevious();  
  155.         }  
  156.  
  157.         #endregion // CurrentIndex  
  158.  
  159.  
  160.         #region NextIsValid  
  161.   
  162.         /// <summary>  
  163.         /// The <see cref="NextIsValid" /> dependency property's name.  
  164.         /// </summary>  
  165.         public const string NextIsValidPropertyName = "NextIsValid";  
  166.   
  167.         /// <summary>  
  168.         /// Gets or sets the value of the <see cref="NextIsValid" />  
  169.         /// property. This is a dependency property.  
  170.         /// </summary>  
  171.         public bool NextIsValid  
  172.         {  
  173.             get  
  174.             {  
  175.                 return (bool)GetValue(NextIsValidProperty);  
  176.             }  
  177.             set  
  178.             {  
  179.                 SetValue(NextIsValidProperty, value);  
  180.             }  
  181.         }  
  182.   
  183.         /// <summary>  
  184.         /// Identifies the <see cref="NextIsValid" /> dependency property.  
  185.         /// </summary>  
  186.         public static readonly DependencyProperty NextIsValidProperty = DependencyProperty.Register(  
  187.             NextIsValidPropertyName,  
  188.             typeof(bool),  
  189.             typeof(NavigationPanel),  
  190.             new UIPropertyMetadata(false));  
  191.  
  192.         #endregion // NextIsValid  
  193.  
  194.         #region PreviousIsValid  
  195.   
  196.         /// <summary>  
  197.         /// The <see cref="PreviousIsValid" /> dependency property's name.  
  198.         /// </summary>  
  199.         public const string PreviousIsValidPropertyName = "PreviousIsValid";  
  200.   
  201.         /// <summary>  
  202.         /// Gets or sets the value of the <see cref="PreviousIsValid" />  
  203.         /// property. This is a dependency property.  
  204.         /// </summary>  
  205.         public bool PreviousIsValid  
  206.         {  
  207.             get  
  208.             {  
  209.                 return (bool)GetValue(PreviousIsValidProperty);  
  210.             }  
  211.             set  
  212.             {  
  213.                 SetValue(PreviousIsValidProperty, value);  
  214.             }  
  215.         }  
  216.   
  217.         /// <summary>  
  218.         /// Identifies the <see cref="PreviousIsValid" /> dependency property.  
  219.         /// </summary>  
  220.         public static readonly DependencyProperty PreviousIsValidProperty = DependencyProperty.Register(  
  221.             PreviousIsValidPropertyName,  
  222.             typeof(bool),  
  223.             typeof(NavigationPanel),  
  224.             new UIPropertyMetadata(false));  
  225.  
  226.         #endregion // PreviousIsValid  
  227.   
  228.   
  229.   
  230.   
  231.     }  
  232. }  

转载于:https://www.cnblogs.com/hnfxs/p/3194496.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 在WPF中,可以通过使用不同的界面元素和绑定技术来实现上一步和下一步界面切换。 首先,我们可以使用StackPanel或Grid等布局容器来承载不同的页面内容。通过在容器中添加多个子元素,每个子元素对应一个页面。然后,我们可以通过调整页面的可见性来实现页面的切换效果。例如,设置当前页面可见,同时将其他页面隐藏。 其次,可以使用命令和绑定技术来实现上一步和下一步按钮的功能。我们可以为每个按钮创建一个命令,并将命令绑定到按钮的Click事件。然后,我们可以在命令的执行方法中处理页面切换的逻辑。例如,当点击上一步按钮时,我们可以将当前页面的索引减1,并更新页面的可见性。当点击下一步按钮时,我们可以将当前页面的索引加1,并更新页面的可见性。 此外,我们还可以使用数据绑定来实现页面之间的数据传递。例如,我们可以在ViewModel中定义一个当前页面的属性,并在页面切换时更新该属性的值。然后,我们可以利用数据绑定机制将该属性绑定到页面中的控件上,以便在不同页面之间共享数据。 综上所述,通过使用布局容器、可见性调整、命令和绑定技术,我们可以实现WPF中的上一步和下一步界面切换功能。这样,用户就可以轻松地在不同的页面之间进行切换,并且可以实现数据的传递和共享。 ### 回答2: WPF (Windows Presentation Foundation) 是一种用于创建Windows应用程序界面的框架。要实现上一步和下一步界面切换,可以按照以下步骤进行操作: 1. 创建多个界面:首先,需要创建多个用户界面,每个界面代表一个步骤。可以使用XAML语言来定义界面的布局和外观,同时也可以通过编写C#代码来处理逻辑。 2. 导航控件:在WPF中,可以使用一些导航控件来实现界面之间的切换。常用的导航控件有Frame和Page。可以在主界面中放置一个Frame控件,然后在该Frame控件中加载不同的Page,每个Page对应一个步骤的界面。 3. 上一步和下一步按钮:可以在每个步骤的界面上添加上一步和下一步按钮,通过点击按钮来切换界面。在点击上一步按钮时,可以使用导航控件的GoBack()方法来返回上一个界面;在点击下一步按钮时,可以使用导航控件的Navigate()方法来跳转到下一个界面。 4. 界面数据传递:如果需要在不同界面之间传递数据,可以使用WPF提供的一些机制,例如使用共享的ViewModel来保存数据,或者通过事件来传递数据。 5. 界面导航逻辑:可以根据实际需求在每个界面的按钮事件中编写界面导航的逻辑。例如,可以在点击下一步按钮时进行一些数据验证,如果验证通过则跳转到下一个界面,否则给出提示信息。 总的来说,WPF通过导航控件和事件机制,可以很方便地实现上一步和下一步界面切换。根据实际需求,可以进行适当的界面设计和数据传递,以提供良好的用户体验。 ### 回答3: WPF(Windows Presentation Foundation)是微软开发的一个用于构建Windows应用程序的UI框架。在WPF实现上一步和下一步界面切换可以通过以下步骤: 1. 首先,在XAML文件中创建两个或多个不同的界面,每个界面都使用一个Grid或者其他容器作为根元素。为了方便切换,可以给每个界面起一个独立的名字。 2. 创建一个主页面,该页面包含两个按钮,一个是“上一步”按钮,一个是“下一步”按钮。这两个按钮可以绑定到相应的事件处理方法。 3. 在代码中,需要创建事件处理方法来响应按钮的点击事件。在“上一步”按钮的事件处理方法中,使用VisualStateManager.GoToState方法来切换到上一个界面,通过设置要切换页面的Visibility属性来显示或隐藏相应的界面。在“下一步”按钮的事件处理方法中,同样使用VisualStateManager.GoToState方法来切换到下一个界面。 4. 在XAML文件中,通过VisualStateGroup、VisualState和Storyboard等元素来定义不同的界面状态,并在代码中使用VisualStateManager.GoToElementState来切换到相应的状态。 5. 最后,在主页面的Loaded事件中设置默认显示的界面,可以使用VisualStateManager.GoToState或者VisualStateManager.GoToElementState方法来切换到默认界面。 通过以上步骤,就可以在WPF实现上一步和下一步界面切换。关键是利用WPF的可视状态管理器(Visual State Manager)来定义界面状态,并在代码中切换到不同的状态,从而实现页面的切换效果

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值