WPF图片列表触摸滑动

实现动态加载图片列表,并能触摸滑动的效果,所以使用UniformGrid控件来显示列表,搭配surface的多点触摸来做滑动。


    
        
            
        
    

如果在TouchUp或MouseUp是就出发查看图片详情,会在触摸滑动后也触发此事件,所以需要判断是否点击后弹起,这样就可以在图片的MouseUp事件里加入查看图片详情。

后台代码:

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace WpfApplication1
{
    /// 
    /// MainWindow.xaml 的交互逻辑
    /// 
    public partial class MainWindow : Window
    {
        bool isMoving = false, isClick = false;

        public MainWindow()
        {
            InitializeComponent();

            for (int i = 0; i < 10; i++)
            {
                StackPanel panel = new StackPanel();
                panel.Width = 200;
                panel.Height = 200;
                panel.Margin = new Thickness(10);
                panel.Background = Brushes.Black;
                panel.PreviewMouseUp += new MouseButtonEventHandler((o, e) =>
                {
                    if (!isMoving && isClick)
                    {
                        isClick = false;
                    }
                });
                panel.PreviewTouchDown += new EventHandler((o, e) =>
                {
                    isClick = true;
                });
                uniformGrid1.Children.Add(panel);
            }

            scrollViewer1.PreviewTouchDown += new EventHandler((o, e) =>
            {
                isMoving = false;
                isClick = false;
            });
            scrollViewer1.PreviewTouchUp += new EventHandler((o, e) =>
            {
                isMoving = false;
            });
            scrollViewer1.ScrollChanged += new ScrollChangedEventHandler((o, e) =>
            {
                isMoving = true;
                isClick = false;
            });
        }
    }
}

 

这本不该是问题的,但就是这样的小程序在公司的硬件设备上不能正常使用,原因是硬件对MouseUp与TouchUp支持不好。

 

你可能感兴趣的:(WPF图片列表触摸滑动)