我想为触摸屏设置原型界面,以便在用户测试期间记录每次鼠标点击。 我成功制作了故事板,但没有记录鼠标点击。
我抬起其他问题 - How do I get the current mouse screen coordinates in WPF?,How do I get the current mouse screen coordinates in WPF? - 但无法理解如何将这些代码应用于.xaml或代码隐藏文件(每次试用都会收到错误消息。)
如果我想记录测试人员在画布上点击的位置,我该如何跟踪坐标并将日志导出为其他文件格式?
答案 0 :(得分: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);
}
}
}