我构建了一个vb.net/wpf应用程序,它是一个wpf窗口的序列。每当新窗口打开或关闭,或者用户按下按钮时,我都想记录一个时间戳。我试图找到一个解决方案,记录事件发生时相对于程序开始的时间。我不知道如何创建一种“全球”秒表,并从不同的窗口访问它,要求记录一个时间戳。应该采取什么方法?在vb.net记录时间戳wpf项目从不同的窗口
回答
制作一个负责打开窗口的对象。例如,这可能是程序类或向导类(这听起来像是我的向导类UI)
何时让Wizard类打开每个窗口并通过调用ShowDialog
来等待它关闭。
如果你这样做,并不难打开和关闭窗口的时间戳。
单击按钮时记录时间戳可以通过使一个对象负责记录并将对象传递给需要记录的函数来完成。
这很容易污染方法的签名。
解决这个问题有几种方法,但大多数方法都会创建一个不需要传递的单个已知对象(静态类)。
使用一种能够根据所需接口解决对象请求的容器可能会更好。 Castle windsor might be an option
//application starts...
var container = new WindsorContainer();
// adds and configures all components using WindsorInstallers from executing assembly
container.Install(FromAssembly.This());
// instantiate and configure root component and all its dependencies and their dependencies and...
var logger= container.Resolve<ILog>();
logger.Log("Click!");
// clean up, application exits
container.Dispose();
'或者如果用户按下按钮'是更棘手的要求。 – Tim 2013-05-12 12:50:20
@Tim - 是的但不是不可能的:创建一个记录时间戳的对象,并在需要的地方传递对该对象的引用。 – 2013-05-12 13:05:55
我添加到我的答案 – 2013-05-12 13:12:58
我最终做了如下的事情。它包含静态类,并且我将一个对象(Dictionary)从一个窗口传递到另一个窗口。我不确定它是否正确,但是窗口相互激活并在下一个窗口关闭后关闭showdialog。 感谢您的帮助。
Public Class HelpTools
Shared startKey As String = "start_time"
Public Shared Sub initializeData(ByRef data As Dictionary(Of String, String), ByVal id As String, ByVal startTime As DateTime, ByVal firstTimeStamp As Double)
AddStringOutput(data, "id", id)
Dim start As String = startTime
AddStringOutput(data, startKey, start)
AddStringOutput(data, "next_timestamp", firstTimeStamp)
End Sub
Public Shared Sub recordTime(ByRef data As Dictionary(Of String, String), ByVal info As String)
'Set the StartTime at the begin of the processing
' for which you want to capture ElapsedTime
Dim StartTime As DateTime = DateTime.Parse(getStringOutput(data, startKey)) 'Now
'Capture the Elapsed Time here as follows
Dim ElapsedTime As TimeSpan = Now().Subtract(StartTime)
'Now we will report the output
'display format is Hours:Minutes:Seconds
Dim timestamp As String = String.Format(info & ": elapsed Time : {0:00}:{1:00}:{2:00}", CInt(ElapsedTime.TotalHours), _
CInt(ElapsedTime.TotalMinutes) Mod 60, _
CInt(ElapsedTime.TotalSeconds) Mod 60)
Dim nextTimestampNumber As Integer = getDoubleOutput(data, "next_timestamp")
AddStringOutput(data, "timestamp_" & nextTimestampNumber, timestamp)
AddDoubleOutput(data, "next_timestamp", 1)
End Sub
...
end class
- 1. 在目录上同步时间戳
- 2. Wpf检测何时从不同的窗口关闭窗口
- 3. 从其他项目打开wpf窗口
- 4. 没有时间戳记的记录
- 5. 当从shell中同步时忽略子目录时间戳
- 6. Pyspark - 根据时间戳值加入时间戳窗口
- 7. PHP date()返回不同时间戳的不同时间戳
- 8. 从其他目录更新目录参考的时间戳?
- 9. ActiveModel时间戳记字段:时间戳记从哪里来?
- 10. 记录Google SketchUp时间戳记事件
- 11. 控制台项目中的WPF窗口?
- 12. WPF的MVVM:从不同的项目
- 13. MySQL的记录时间戳改变
- 14. 有时间戳的订购记录
- 15. 多个记录的时间戳处理
- 16. 时间戳是不同的
- 17. WPF VS2013 - 添加新窗口作为主窗口的子项目
- 18. winXP,win7和vb.net下的不同时间戳
- 19. 时间戳在Cognito窗体
- 20. 如何按时间戳过滤记录
- 21. Blend“窗口在WPF项目中不受支持”
- 22. 登录窗口上的DLL C#项目
- 23. 在非WPF项目中等待WPF窗口
- 24. WPF:如何从一个窗口开始在不同的装配
- 25. c#wpf更新从另一个项目的主窗口
- 26. WPF在运行时具有不同的窗口配置
- 27. 从时间戳记提取SQL小时
- 28. 与mysql时间戳不同的Rails时间戳
- 29. FAT32 SD卡记录时间戳记的时区是什么?
- 30. VB.Net每日时间记录MySQL
用静态变量创建一个类.. – matzone 2013-05-12 03:15:39