ListView滑动/幻灯片动画
发布时间:2020-12-13 20:26:09 所属栏目:Windows 来源:网络整理
导读:我有一个ListView控件来显示项目,我想提供一个滑动/滑动手势来选择一个项目.我使用 GestureRecognizer类来识别十字滑动手势,但我也希望通过水平移动所选项目来设置此手势的动画. 例如,这应该是来自iOS应用程序的此图像: 我在网上搜索但我找不到任何有用的链
我有一个ListView控件来显示项目,我想提供一个滑动/滑动手势来选择一个项目.我使用
GestureRecognizer类来识别十字滑动手势,但我也希望通过水平移动所选项目来设置此手势的动画.
例如,这应该是来自iOS应用程序的此图像: 我在网上搜索但我找不到任何有用的链接如何在ListView控件中动画这个手势.
您可以创建一个行为来侦听项目上的ManipulationXYZ事件,并为这些项目上的RenderTransform设置动画.我给你写了一个简单的例子:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Input; using Windows.UI.Xaml; using Windows.UI.Xaml.Input; using Windows.UI.Xaml.Media; using Windows.UI.Xaml.Media.Animation; using Microsoft.Xaml.Interactivity; namespace SOTestApp { [TypeConstraint(typeof(FrameworkElement))] public class SlideMechanicBehavior : DependencyObject,IBehavior { public void Attach(DependencyObject associatedObject) { AssociatedObject = associatedObject; var fw = (FrameworkElement) AssociatedObject; fw.ManipulationMode = ManipulationModes.TranslateX | ManipulationModes.System; fw.ManipulationDelta += fw_ManipulationDelta; fw.ManipulationCompleted += fw_ManipulationCompleted; if (fw.RenderTransform == null || fw.RenderTransform as TranslateTransform == null) { fw.RenderTransform = new TranslateTransform(); } } private const double Threshold = 100.0; private bool _canMove = true; public ICommand LeftDragCommand { get { return (ICommand)GetValue(LeftDragCommandProperty); } set { SetValue(LeftDragCommandProperty,value); } } public static readonly DependencyProperty LeftDragCommandProperty = DependencyProperty.Register("LeftDragCommand",typeof(ICommand),typeof(SlideMechanicBehavior),new PropertyMetadata(default(ICommand))); public ICommand RightDragCommand { get { return (ICommand)GetValue(RightDragCommandProperty); } set { SetValue(RightDragCommandProperty,value); } } public static readonly DependencyProperty RightDragCommandProperty = DependencyProperty.Register("RightDragCommand",new PropertyMetadata(default(ICommand))); void fw_ManipulationCompleted(object sender,ManipulationCompletedRoutedEventArgs e) { var fw = (FrameworkElement)AssociatedObject; var tr = (TranslateTransform) fw.RenderTransform; if (tr == null) return; tr.X = e.Cumulative.Translation.X; //call commands if (tr.X > Threshold && RightDragCommand != null && RightDragCommand.CanExecute(null)) RightDragCommand.Execute(null); //add params if necessary if (tr.X < -1 * Threshold && LeftDragCommand != null && LeftDragCommand.CanExecute(null)) LeftDragCommand.Execute(null); //add params if necessary //animate back var s = new Storyboard(); var d = new DoubleAnimation { To = 0.0,EasingFunction = new QuadraticEase(),Duration = TimeSpan.FromMilliseconds(300.0) }; Storyboard.SetTarget(d,tr); Storyboard.SetTargetProperty(d,"X"); //use nameof() in C# 6.0 s.Children.Add(d); _canMove = false; s.Completed += (o,o1) => _canMove = true; s.Begin(); } void fw_ManipulationDelta(object sender,ManipulationDeltaRoutedEventArgs e) { if(!_canMove) return; //move item var m = e.Delta.Translation.X; var fw = (FrameworkElement)AssociatedObject; var tr = (TranslateTransform) fw.RenderTransform; if (tr != null) tr.X += m; } public void Detach() { var fw = (FrameworkElement)AssociatedObject; fw.ManipulationCompleted -= fw_ManipulationCompleted; fw.ManipulationDelta -= fw_ManipulationDelta; AssociatedObject = null; } public DependencyObject AssociatedObject { get; private set; } } } 您可以在Xaml中使用它,如下所示: <ItemsControl ItemsSource="{Binding TextList}" HorizontalAlignment="Center"> <ItemsControl.ItemTemplate> <DataTemplate> <Grid Background="Red"> <interactivity:Interaction.Behaviors> <local:SlideMechanicBehavior /> </interactivity:Interaction.Behaviors> <TextBlock FontSize="22" Text="{Binding }" /> </Grid> </DataTemplate> </ItemsControl.ItemTemplate> </ItemsControl> 不要忘记在项目中添加Behaviors SDK>添加参考>扩展对话框. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- xaml实现无边框窗口
- Wix:我可以在RemoveFolderEx元素上强制条件只在卸载程序时
- 进程、线程、协程篇(下)
- .net – ListBox边距在Windows 7和Windows 8中是不一样的
- ms-access – Microsoft Jet的64位替代方案
- xaml – 如何在Windows Apps中更改RelativePanel附加属性?
- active-directory – dsadd给出“提供的名称不是正确形成的
- 如何运行heat.exe并在wix中注册一个dll
- windows-server-2003 – 启用终端服务后禁用远程桌面管理
- Windows – IIS日志文件和负载均衡器?
推荐文章
站长推荐
- 在Windows系统上安装PHP运行环境文字教程
- 可以从Windows机器上在iPad上远程调试javascript
- 有任何Windows记录器工具可以跟踪文件操作吗?
- windows – 如何有效地检索目录中的文件数?
- windows-phone-7 – ApplicationBar背后的背景图
- Windows批处理文件是否具有%*构造?
- Windows 8 C#和XAML应用程序的程序体系结构,具有
- 是否有适用于Windows的KornShell(ksh)IDE?
- jboss7.x – 无法登录到windows上的jboss 7.1管理
- windows – OpenGL GLEW MinGW应用程序链接问题
热点阅读