1

我想为触摸屏设计一个界面原型,以便在用户测试期间记录每次鼠标点击。我成功地制作了故事板,但未能记录鼠标点击。

我查找了其他问题 - 如何在 WPF 中获取当前鼠标屏幕坐标?,如何在 WPF 中获取当前鼠标屏幕坐标?- 但不明白如何将这些代码应用到 .xaml 或代码隐藏文件(每次试验我都会收到错误消息。)

如果我想记录测试人员在画布上的点击位置,如何跟踪坐标并将日志导出为其他文件格式?

4

1 回答 1

0

一个非常简单的例子,

这将记录用户单击的每个位置以及时间:

在此处输入图像描述

<Window x:Class="WpfApplication6.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow"
        Width="525"
        Height="350"
        MouseDown="MainWindow_OnMouseDown">
    <Grid>
        <Button Width="75"
                Margin="5"
                HorizontalAlignment="Left"
                VerticalAlignment="Top"
                Click="Button_Click"
                Content="_Show log" />

    </Grid>
</Window>

后面的代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows;
using System.Windows.Input;

namespace WpfApplication6
{
    /// <summary>
    ///     Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        private readonly List<Tuple<DateTime, Point>> _list;

        public MainWindow()
        {
            InitializeComponent();
            _list = new List<Tuple<DateTime, Point>>();
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            IEnumerable<string> @select = _list.Select(s => string.Format("{0} {1}", s.Item1.TimeOfDay, s.Item2));
            string @join = string.Join(Environment.NewLine, @select);
            MessageBox.Show(join);
        }

        private void MainWindow_OnMouseDown(object sender, MouseButtonEventArgs e)
        {
            Point position = e.GetPosition((IInputElement) sender);
            var tuple = new Tuple<DateTime, Point>(DateTime.Now, position);
            _list.Add(tuple);
        }
    }
}
于 2014-04-11T00:38:04.613 回答