gpt4 book ai didi

.net - 我可以在让用户等待之前更新 WPF 状态栏文本吗?

转载 作者:行者123 更新时间:2023-12-04 22:53:48 25 4
gpt4 key购买 nike

我有一个带有状态栏的 WPF 应用程序。

<StatusBar Grid.Row="1"
Height="23"
Name="StatusBar1"
VerticalAlignment="Bottom">
<TextBlock Name="TextBlockStatus" />
</StatusBar>

当我做少量工作时,我想在那里显示文本并切换到沙漏等待光标。

此代码将更新光标,但状态栏 文本不更新 ...
Cursor = Cursors.Wait
TextBlockStatus.Text = "Loading..."
System.Threading.Thread.Sleep(New TimeSpan(0, 0, 3))
TextBlockStatus.Text = String.Empty
Cursor = Cursors.Arrow

更新

灵感来自 Alexandraanswer ...

它有效如果我这样做,但我对这个解决方案一点也不满意。有没有更简单的方法?
Delegate Sub Load1()
Sub Load2()
System.Threading.Thread.Sleep(New TimeSpan(0, 0, 3))
End Sub
Dim Load3 As Load1 = AddressOf Load2

Sub Load()
Cursor = Cursors.Wait
TextBlockStatus.Text = "Loading..."
Dispatcher.Invoke(DispatcherPriority.Background, Load3)
TextBlockStatus.Text = String.Empty
Cursor = Cursors.Arrow
End Sub

我宁愿它看起来像这样......
Sub Load()
Cursor = Cursors.Wait
TextBlockStatus.Text = "Loading..."

'somehow put all the Dispatcher, Invoke, Delegate,
AddressOf, and method definition stuff here'

TextBlockStatus.Text = String.Empty
Cursor = Cursors.Arrow
End Sub

或者甚至更好...
Sub Load()
Cursor = Cursors.Wait
ForceStatus("Loading...")
System.Threading.Thread.Sleep(New TimeSpan(0, 0, 3))
ForceStatus(String.Empty)
Cursor = Cursors.Arrow
End Sub

Sub ForceStatus(ByVal Text As String)
TextBlockStatus.Text = Text
'perform magic'
End Sub

更新

我还尝试将 TextBlock 绑定(bind)到公共(public)属性并实现 INotifyPropertyChanged作为 IanGilham suggested .这个 不工作 .

XAML:
<TextBlock Text="{Binding Path=StatusText}"/>

视觉基础:
Imports System.ComponentModel
Partial Public Class Window1
Implements INotifyPropertyChanged

Private _StatusText As String = String.Empty
Public Event PropertyChanged As PropertyChangedEventHandler Implements INotifyPropertyChanged.PropertyChanged

Property StatusText() As String
Get
Return _StatusText
End Get
Set(ByVal value As String)
_StatusText = value
OnPropertyChanged("StatusText")
End Set
End Property

Shadows Sub OnPropertyChanged(ByVal name As String)
RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs(name))
End Sub
...

Sub Load()
...
Cursor = Cursors.Wait
StatusText = "Loading..."
System.Threading.Thread.Sleep(New TimeSpan(0, 0, 3))
StatusText = String.Empty
Cursor = Cursors.Arrow
...
End Sub
...

最佳答案

您应该使用 BackgroundWorker .这项工作将在一个单独的线程中进行,这意味着您的 UI 线程将是免费的,您的应用程序仍将是响应式的。

它不会是一个非常紧凑的代码解决方案,但它对您的用户来说是最强大和最友好的。

关于.net - 我可以在让用户等待之前更新 WPF 状态栏文本吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/782359/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com