2009-04-23 113 views
0

我想在我的StatusBar中設置TextBlock的文本,然後讓用戶等待一小段時間,而我的程序做了一些工作。有沒有簡單的方法來設置WPF StatusBar文本?

Aparently,而不是做這樣一個可愛的小功能(不工作):

Function Load(ByVal Id As Guid) As Thing 
    Cursor = Cursors.Wait 
    TextBlockStatus.Text = "Loading..." 
    TextBlockStatus.UpdateLayout() 
    Dim Found = From t In db.Thing _ 
       Where t.Id = Id _ 
       Select t 
    TextBlockStatus.Text = String.Empty 
    Cursor = Cursors.Arrow 
    Return Found 
End Function 

我不得不改用這個畸形:

Private WithEvents LoadWorker As BackgroundWorker 

Private Sub Window1_Loaded(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles MyBase.Loaded 
    InitializeLoadWorker() 
End Sub 

Private Sub InitializeLoadWorker() 
    LoadWorker = New BackgroundWorker 
    LoadWorker.WorkerSupportsCancellation = False 
    LoadWorker.WorkerReportsProgress = False 
    AddHandler LoadWorker.DoWork, AddressOf LoadBackgroundWorker_DoWork 
    AddHandler LoadWorker.RunWorkerCompleted, AddressOf LoadBackgroundWorker_RunWorkerCompleted 
End Sub 

Sub Load(ByVal Id As Guid) 
    Cursor = Cursors.Wait 
    LoadWorker.RunWorkerAsync(Argument) 
End Sub 

Private Sub LoadBackgroundWorker_DoWork(ByVal sender As Object, ByVal e As DoWorkEventArgs) 
    Dim Worker As BackgroundWorker = DirectCast(sender, BackgroundWorker) 
    Dim Id As Guid = DirectCast(e.Argument, Guid) 
    e.Result = From t In db.Thing _ 
       Where t.Id = Id _ 
       Select t 
End Sub 

Private Sub LoadBackgroundWorker_RunWorkerCompleted(ByVal sender As Object, ByVal e As RunWorkerCompletedEventArgs) 
    TextBlockStatus.Text = String.Empty 
    Cursor = Cursors.Arrow 
    Dim Found As Thing = DirectCast(e.Result, Thing) 
    'now do something with the found thing here instead of where Load() is called.' 
End Sub 

和負載()函數現在是一個小組!

必須有更好的方法來處理這種簡單的情況。這不需要是異步的。

+0

爲什麼不工作? – 2009-04-23 21:45:40

+0

文字不更新。 – 2009-04-23 22:02:06

回答

1

看看這個問題:Display Wait Screen in WPF

接受的答案有一種做我認爲你想要的,沒有背景工人類的方式。

從在回答其他問題的聯繫,這可能會爲VB.NET工作(我還沒有嘗試過,雖然它)

Public Sub AllowUIToUpdate() 

    Dim frame As New DispatcherFrame() 

    Dispatcher.CurrentDispatcher.BeginInvoke(DispatcherPriority.Render, New DispatcherOperationCallback(AddressOf JunkMethod), frame) 

    Dispatcher.PushFrame(frame) 

End Sub 

Private Function JunkMethod(ByVal arg As Object) As Object 

    DirectCast(arg, DispatcherFrame).Continue = False 
    Return Nothing 

End Function 
相關問題