2013-05-12 68 views
0

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

+0

用静态变量创建一个类.. – matzone 2013-05-12 03:15:39

回答

1

制作一个负责打开窗口的对象。例如,这可能是程序类或向导类(这听起来像是我的向导类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(); 
+0

'或者如果用户按下按钮'是更棘手的要求。 – Tim 2013-05-12 12:50:20

+0

@Tim - 是的但不是不可能的:创建一个记录时间戳的对象,并在需要的地方传递对该对象的引用。 – 2013-05-12 13:05:55

+0

我添加到我的答案 – 2013-05-12 13:12:58

0

我最终做了如下的事情。它包含静态类,并且我将一个对象(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