有没有简单的方法来设置WPF StatusBar文本?
我想在我的 StatusBar 中设置 TextBlock 的文本,然后让用户在我的程序执行一些工作时等待一小会儿。
显然,我不能做一个像这样的漂亮的小函数(这不起作用):
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
并且 Load() 函数现在是一个 Sub!
一定有更好的方法来处理如此简单的情况。 这不需要是异步的。
I want to set the Text of a TextBlock in my StatusBar before making the user wait for a short moment while my program does a little bit of work.
Aparently, instead of doing a nice little function like this (which does not work):
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
I have to instead use this monstrosity:
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
And the Load() Function is now a Sub!!
There must be a better way to handle such a simple situation. This doesn't need to be asynchronous.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
看一下这个问题:在 WPF 中显示等待屏幕。
接受的答案有一种方法可以做我认为你想做的事情,而无需后台工人阶级。
从另一个问题的答案中的链接来看,这可能适用于 VB.NET(不过我还没有尝试过)
Have a look at this question: Display Wait Screen in WPF.
The accepted answer has a way of doing what I think you want, without a Background worker class.
From the link in the answer to the other question, this might work for VB.NET (I haven't tried it though)