WPF圖片列表觸摸滑動


實現動態加載圖片列表,並能觸摸滑動的效果,所以使用UniformGrid控件來顯示列表,搭配surface的多點觸摸來做滑動。

<Window x:Class="WpfApplication1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:surface="http://schemas.microsoft.com/surface/2008"
        Title="MainWindow" Height="600" Width="800">
    <Grid>
        <surface:SurfaceScrollViewer x:Name="scrollViewer1" ScrollViewer.PanningMode="VerticalOnly" ScrollViewer.VerticalScrollBarVisibility="Hidden" ScrollViewer.HorizontalScrollBarVisibility="Hidden">
            <UniformGrid VerticalAlignment="Top" Name="uniformGrid1" Columns="3"></UniformGrid>
        </surface:SurfaceScrollViewer>
    </Grid>
</Window>

如果在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
{
    /// <summary>
    /// MainWindow.xaml 的交互邏輯
    /// </summary>
    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<TouchEventArgs>((o, e) =>
                {
                    isClick = true;
                });
                uniformGrid1.Children.Add(panel);
            }

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

 

這本不該是問題的,但就是這樣的小程序在公司的硬件設備上不能正常使用,原因是硬件對MouseUp與TouchUp支持不好。

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM