我構建了一個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