简体   繁体   English

跟踪窗口打开的时间 WPF XAML

[英]Tracking how long a window is open WPF XAML

I was wondering if i could get a little help.我想知道我是否能得到一点帮助。 I am working on a WPF application and i have to keep track how long it takes a user to make a report(make a report is one of the buttons), want the amount of time elapsed between when the XAML view is first shown (loaded) to the user and when the user clicks the 'Submit' button我正在开发一个 WPF 应用程序,我必须跟踪用户制作报告所需的时间(制作报告是按钮之一),希望 XAML 视图首次显示(加载) 给用户,当用户点击“提交”按钮时

This is how I would approach it.这就是我将如何处理它。 Define 2 private member variable of type Datetime.定义 2 个 Datetime 类型的私有成员变量。 Let's name those variables startTime and endTime.让我们将这些变量命名为 startTime 和 endTime。 Wire-up the XAML Window's Loaded event.连接 XAML 窗口的 Loaded 事件。 In the Loaded event set the startTime = DateTime.Now.在 Loaded 事件中设置 startTime = DateTime.Now。 In the event handler for the 'Submit' button set endTime = DateTime.Now.在“提交”按钮的事件处理程序中设置 endTime = DateTime.Now。 To get the total elapsed time simply subtract the startTime from the endTime (var elapsedTime = endTime - startTime).要获得总经过时间,只需从结束时间中减去开始时间(var elapsedTime = endTime - startTime)。 The process should be able to be repeated unless this is the main control of your application and is always in scope.除非这是您的应用程序的主要控制并且始终在范围内,否则该过程应该能够重复。 So, if this is your main Window that stays open you'll need a way to set the startTime for each subsequent operation.因此,如果这是保持打开状态的主窗口,您将需要一种方法来为每个后续操作设置 startTime。 Maybe re-set the startTime right after you retrieve the elapsed time.也许在您检索经过的时间后立即重新设置 startTime。 Hope this helps!希望这可以帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM