从不同窗口在vb.net wpf项目中记录时间戳

从不同窗口在vb.net wpf项目中记录时间戳,.net,wpf,vb.net,.net,Wpf,Vb.net,我正在构建一个vb.net/wpf应用程序,它是一系列wpf窗口。每次打开或关闭新窗口,或者用户按下按钮时,我都要记录一个时间戳。我试图找到一个解决方案,如何记录事件发生时相对于程序开始的时间。我不知道如何创建一种“全局”秒表,并从不同的窗口访问它以要求记录时间戳。应该采取什么方法?使单个对象负责打开窗口。例如,可以是程序类或向导类(对我来说,这听起来像是一个向导类的UI) 当向导类打开每个窗口并等待它关闭时,调用ShowDialog 如果你这样做的话,给窗户的打开和关闭加上时间戳就不难了 单击

我正在构建一个vb.net/wpf应用程序,它是一系列wpf窗口。每次打开或关闭新窗口,或者用户按下按钮时,我都要记录一个时间戳。我试图找到一个解决方案,如何记录事件发生时相对于程序开始的时间。我不知道如何创建一种“全局”秒表,并从不同的窗口访问它以要求记录时间戳。应该采取什么方法?

使单个对象负责打开窗口。例如,可以是程序类或向导类(对我来说,这听起来像是一个向导类的UI)

当向导类打开每个窗口并等待它关闭时,调用
ShowDialog

如果你这样做的话,给窗户的打开和关闭加上时间戳就不难了

单击按钮时记录时间戳可以通过使单个对象负责记录并将该对象传递给需要记录的函数来完成

这很容易污染方法的签名

有几种方法可以解决这个问题,但大多数方法都会创建一个不需要传递的已知对象(静态类)

使用能够基于所需接口解决对象请求的容器可能更好

//应用程序启动。。。
var container=新的WindsorContainer();
//通过执行程序集,使用WindsOrInstaller添加和配置所有组件
container.Install(fromsassembly.This());
//实例化和配置根组件及其所有依赖项及其依赖项和。。。
var logger=container.Resolve();
Log(“单击!”);
//清理,应用程序退出
container.Dispose();

我最终做了如下事情。它包括静态类,我在窗口之间传递一个对象(字典)。我不确定这是否是正确的方法,但窗口会相互激活,并在下一个窗口被称为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

创建一个带有静态var..@Tim的类-是的,但并非不可能:创建一个用于记录时间戳的对象,并在需要的地方传递对该对象的引用。
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