2013-05-12 72 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