FFIVE
我创建了一个Behavior,这样整个事情就可以在不需要任何代码的情况下完成。使用 a 的好处Behavior是您可以在解决方案中的任何地方重用它,对它进行单元测试以确保它按您的意愿运行或扩展它的功能。主窗口<Window x:Class="TestWpfApplication.MainWindowView" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity" xmlns:testWpfApplication="clr-namespace:TestWpfApplication" mc:Ignorable="d" Title="MainWindow" Height="450" Width="800"> <i:Interaction.Behaviors> <testWpfApplication:SwipeBehavior TargetContentControl="{Binding ElementName=MainContentControl}" LeftUserControl="{Binding Path=LeftControl}" RightUserControl="{Binding Path=RightControl}" /> </i:Interaction.Behaviors> <Grid> <ContentControl Name="MainContentControl" /> </Grid></Window>主窗口代码背后using System.Windows;namespace TestWpfApplication{ public partial class MainWindowView : Window { private readonly MainWindowViewModel _mainWindowViewModel = new MainWindowViewModel(); public MainWindowView() { InitializeComponent(); DataContext = _mainWindowViewModel; } }}滑动行为using System.Windows;using System.Windows.Controls;using System.Windows.Input;using System.Windows.Interactivity;namespace TestWpfApplication{ public class SwipeBehavior : Behavior<Window> { public static readonly DependencyProperty TargetContentControlProperty = DependencyProperty.RegisterAttached("TargetContentControl", typeof(ContentControl), typeof(SwipeBehavior), new UIPropertyMetadata(null)); public static readonly DependencyProperty LeftUserControlProperty = DependencyProperty.RegisterAttached("LeftUserControl", typeof(UserControl), typeof(SwipeBehavior), new UIPropertyMetadata(null)); public static readonly DependencyProperty RightUserControlProperty = DependencyProperty.RegisterAttached("RightUserControl", typeof(UserControl), typeof(SwipeBehavior), new UIPropertyMetadata(null)); public static ContentControl GetTargetContentControl(DependencyObject dependencyObject) { return (ContentControl) dependencyObject.GetValue(TargetContentControlProperty); } public static void SetTargetContentControl(DependencyObject dependencyObject, ContentControl value) { dependencyObject.SetValue(TargetContentControlProperty, value); } public static ContentControl GetLeftUserControl(DependencyObject dependencyObject) { return (UserControl) dependencyObject.GetValue(LeftUserControlProperty); } public static void SetLeftUserControl(DependencyObject dependencyObject, UserControl value) { dependencyObject.SetValue(LeftUserControlProperty, value); } public static ContentControl GetRightUserControl(DependencyObject dependencyObject) { return (UserControl) dependencyObject.GetValue(RightUserControlProperty); } public static void SetRightUserControl(DependencyObject dependencyObject, UserControl value) { dependencyObject.SetValue(RightUserControlProperty, value); } private Point _swipeStart; protected override void OnAttached() { base.OnAttached(); AssociatedObject.MouseDown += OnMouseDown; AssociatedObject.MouseMove += OnMouseMove; } private void OnMouseDown(object sender, MouseButtonEventArgs e) { _swipeStart = e.GetPosition(AssociatedObject); } private void OnMouseMove(object sender, MouseEventArgs e) { var targetContentControl = GetValue(TargetContentControlProperty) as ContentControl; if (targetContentControl == null) { return; } if (e.LeftButton == MouseButtonState.Pressed) { var swipe = e.GetPosition(AssociatedObject); //Swipe Left if (swipe.X > (_swipeStart.X + 200)) { // OR Use Your Logic to switch between pages. targetContentControl.Content = new LeftControl(); } //Swipe Right if (swipe.X < (_swipeStart.X - 200)) { // OR Use Your Logic to switch between pages. targetContentControl.Content = new RightControl(); } } e.Handled = true; } }}主窗口视图模型using System.Windows.Controls;namespace TestWpfApplication{ internal class MainWindowViewModel { public UserControl LeftControl { get; } = new LeftControl(); public UserControl RightControl { get; } = new RightControl(); }}注意: LeftControl 和 RightControl 在此示例中是 WPF 用户控件。此外,您必须在项目中引用 System.Window.Interactivity 才能使用Behavior该类