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支持不好。

 

posted on 2013-04-18 22:04  toven  阅读(3431)  评论(0编辑  收藏  举报

导航