2011-04-08 48 views
0

我正在編寫一個顯示當前時間的Visual Studio工具欄加載項。Visual Studio加載項:強制失效

我有一個CommandBar toolbar其上CommandBarButton timeLabel(因爲沒有標籤可用)和Timer
每當定時器事件命中,我將標題設置爲當前時間。

DateTime t = DateTime.Now; 
timeLabel.Caption = String.Format("{0}:{1}:{2}", t.Hour, t.Minute, t.Second); 
// force Invalidate/repaint 
timeLabel.Visible = !timeLabel.Visible; 
timeLabel.Visible = !timeLabel.Visible; 

有沒有更優雅的方式來做Invalidate()?我對這個解決方案感到非常不舒服。

感謝&親切的問候
西蒙

回答

1

我找到了答案VS 2010,但是這似乎並沒有爲VS 08遺憾的是工作。
它更新了整個UI:

// Invalidate the VS UI 
ThreadHelper.Generic.Invoke(new System.Action(() => 
{ 
    ServiceProvider serviceProvider = new ServiceProvider(((DTE)Microsoft.VisualStudio.Shell.ServiceProvider.GlobalProvider.GetService(typeof(DTE))) as Microsoft.VisualStudio.OLE.Interop.IServiceProvider); 
    IVsUIShell uiShell = serviceProvider.GetService(typeof(SVsUIShell)) as IVsUIShell; 
    uiShell.UpdateCommandUI(0); 
})); 

恕我直言我的第一個解決方案比這3行代碼更容易理解,但是這可能有助於萬一有人遇到另外一個問題,像我一樣。