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

标签 c# wpf swipe swipe-gesture

<分区>

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

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

Sketch

我只是想像图像轮播那样做。到目前为止,我一直在关注这个 WPF image swipe to change image like in iOS 但是,它不会滑动,而是会在移动鼠标时放大和缩小。

最佳答案

我正在使用页面,但您也可以使用窗口。

第一。创建两个页面 LeftPage.xaml 和 RightPage.Xaml 以及 MainWindow.xaml 和 MainWindows.xaml.cs 的以下代码

XAML

主窗口

<Window x:Class="SOWPF.MainWindow"
    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:local="clr-namespace:SOWPF"
    mc:Ignorable="d" 
    Title="MainWindow" Height="450" Width="800"
    MouseDown="Window_MouseDown" MouseMove="Window_MouseMove">
<Grid>
    <Frame x:Name="MainFrame" NavigationUIVisibility="Hidden" />
</Grid>

C#

public partial class MainWindow : Window
{
    protected Point SwipeStart;
    public MainWindow()
    {
        InitializeComponent();
        MainFrame.Source = new Uri("LeftPage.xaml", UriKind.RelativeOrAbsolute);
    }

    private void Window_MouseDown(object sender, MouseEventArgs e)
    {
        SwipeStart = e.GetPosition(this);
    }

    private void Window_MouseMove(object sender, MouseEventArgs e)
    {
        if (e.LeftButton == MouseButtonState.Pressed)
        {
            var Swipe = e.GetPosition(this);                

            //Swipe Left
            if (SwipeStart != null && Swipe.X > (SwipeStart.X + 200))
            {
                // OR Use Your Logic to switch between pages.
                MainFrame.Source = new Uri("LeftPage.xaml", UriKind.RelativeOrAbsolute);
            }

            //Swipe Right
            if (SwipeStart != null && Swipe.X < (SwipeStart.X - 200))
            {
                // OR Use Your Logic to switch between pages.
                MainFrame.Source = new Uri("RightPage.xaml", UriKind.RelativeOrAbsolute);
            }
        }
        e.Handled = true;
    }
}

Demo

关于c# - 如何使用鼠标在 WPF 中创建左右滑动?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53755920/

相关文章:

c# - WPF:绑定(bind)到自定义类属性。 PropertyChanged 已触发但 View 未更新

c# - WPF 中的绑定(bind)数据未更新

android - 未检测到滑动

c# - 基于抽象类公开 WCF 子类

c# - 从 web api 返回 bool 值

c# - Type.GetType() 带空

c# - ViewModel 是否应该具有绑定(bind)到模型上的 DependencyProperties 的直通 DependencyProperties?

c# - 无泄漏中断循环

安卓网页浏览: prevent touch gestures being passed to parent viewpager

ios - 滑入/滑出 subview Controller 在 Swift 的父 View Controller 上部分可见