Gibt es keine einfache Möglichkeit, WPF StatusBar Text zu setzen?
-
13-09-2019 - |
Frage
Ich will den Text eines Textblock in meinem StatusBar setzen, bevor der Benutzer warten, für einen kurzen Moment zu machen, während mein Programm funktioniert ein wenig Arbeit.
Aparently, statt eine nette kleine Funktion wie dies zu tun (was nicht funktioniert):
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
Ich muss stattdessen diese Ungeheuerlichkeit verwenden:
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
Und die Load () Funktion ist nun ein Sub !!
Es muss einen besseren Weg, eine solche einfache Situation zu handhaben. Das muss nicht asynchron sein.
Lösung
Haben Sie einen Blick auf diese Frage. Anzeige Wait Bildschirm in WPF
Die akzeptierte Antwort hat eine Art und Weise zu tun, was ich glaube, Sie wollen, ohne Hintergrund Arbeiter-Klasse.
Von den Link in der Antwort auf die andere Frage ist, dies für VB.NET arbeiten (ich habe es nicht versucht aber)
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