WPF等待光标BackgroundWorker的主题光标、主题、WPF、BackgroundWorker

2023-09-03 01:12:26 作者:天黑怕被劫色

我想显示沙漏光标和禁用窗口,而一个BackgroundWorker进程运行在另一个线程。

I want to show the hourglass cursor and disable the window while a BackgroundWorker process runs in another thread.

这是我在做什么:

Private Sub MyButton_Click(...)
    Dim box As New AnotherWpfWindow()
    box.Owner = Me
    ...
    box.ShowDialog()
    If (box.DialogResult.GetValueOrDefault = True) Then
        Me.IsEnabled = False
        Me.Cursor = Cursors.Wait
        MyBackgroundWorker.RunWorkerAsync()
    End If
End Sub

Private Sub MyBackgroundWorker_RunWorkerCompleted(...)
    UpdateInterface()
    Me.IsEnabled = True
    Me.Cursor = Cursors.Arrow
End Sub

窗口则变成身患残疾,我想,但光标仍然是一个箭头。我怎样才能使它的等待光标?

The window becomes disabled like I want, but the cursor remains an arrow. How can I make it the Wait cursor?

这似乎根据这个问题工作 vg1890 :Disabling所有,但在一个WPF窗口一个控制

It seems to work for vg1890 according to this question: Disabling all but one control in a WPF window

推荐答案

什么似乎发生在这里的是,WPF是忽略了光标的残疾人窗口设置。以下解决方法似乎工作:而不是禁用窗口本身,禁用的内容窗口的:

What seems to be happening here is that WPF is ignoring the Cursor setting on the disabled window. The following workaround seems to work: instead of disabling the window itself, disable the content of the window:

C#:

((UIElement)Content).IsEnabled = false;
Cursor = Cursors.Wait;

// and in RunWorkerCompleted handler:
((UIElement)Content).IsEnabled = true;
Cursor = Cursors.Arrow;

Visual Basic中:

Visual Basic:

DirectCast(Content, UIElement).IsEnabled = False
Cursor = Cursors.Wait

' and in RunWorkerCompleted handler:'
DirectCast(Content, UIElement).IsEnabled = True
Cursor = Cursors.Arrow