如何使用鼠标在 WPF 中创建左右滑动?

我对 C# 很陌生。所以,我正在尝试在我的 WPF 中创建一个简单的滑动功能,如果我向左或向右滑动,它会转到另一个 wpf 窗口。请帮我!我在网上找不到太多资源。

所以我的问题是如何在 wpf 应用程序中使用鼠标滑动,以便我可以使用鼠标滑动在页面/窗口之间切换。

http://img2.mukewang.com/62a58ed10001df9006540249.jpg

我只是想做一个图像旋转木马。到目前为止,我已经按照这个WPF 图像滑动来更改图像,就像在 iOS 中一样 但是,它不会滑动,而是在移动鼠标时放大和缩小。



当年话下
浏览 261回答 2
2回答

蛊毒传说

我正在使用页面,但您也可以使用窗口。第一个。创建两个页面 LeftPage.xaml 和 RightPage.Xaml 并将以下代码添加到 MainWindow.xaml 和 MainWindows.xaml.csXAML主窗口<Window x:Class="SOWPF.MainWindow"&nbsp; &nbsp; xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"&nbsp; &nbsp; xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"&nbsp; &nbsp; xmlns:d="http://schemas.microsoft.com/expression/blend/2008"&nbsp; &nbsp; xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; xmlns:local="clr-namespace:SOWPF"&nbsp; &nbsp; mc:Ignorable="d"&nbsp;&nbsp; &nbsp; Title="MainWindow" Height="450" Width="800"&nbsp; &nbsp; MouseDown="Window_MouseDown" MouseMove="Window_MouseMove"><Grid>&nbsp; &nbsp; <Frame x:Name="MainFrame" NavigationUIVisibility="Hidden" /></Grid>C#public partial class MainWindow : Window{&nbsp; &nbsp; protected Point SwipeStart;&nbsp; &nbsp; public MainWindow()&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; InitializeComponent();&nbsp; &nbsp; &nbsp; &nbsp; MainFrame.Source = new Uri("LeftPage.xaml", UriKind.RelativeOrAbsolute);&nbsp; &nbsp; }&nbsp; &nbsp; private void Window_MouseDown(object sender, MouseEventArgs e)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; SwipeStart = e.GetPosition(this);&nbsp; &nbsp; }&nbsp; &nbsp; private void Window_MouseMove(object sender, MouseEventArgs e)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; if (e.LeftButton == MouseButtonState.Pressed)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var Swipe = e.GetPosition(this);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //Swipe Left&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (SwipeStart != null && Swipe.X > (SwipeStart.X + 200))&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // OR Use Your Logic to switch between pages.&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; MainFrame.Source = new Uri("LeftPage.xaml", UriKind.RelativeOrAbsolute);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //Swipe Right&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (SwipeStart != null && Swipe.X < (SwipeStart.X - 200))&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // OR Use Your Logic to switch between pages.&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; MainFrame.Source = new Uri("RightPage.xaml", UriKind.RelativeOrAbsolute);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; e.Handled = true;&nbsp; &nbsp; }}

FFIVE

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