WPF 等待游标与后台工作线程

发布于 2024-07-18 02:49:52 字数 799 浏览 8 评论 0原文

我想在 BackgroundWorker 进程在另一个线程中运行时显示沙漏光标并禁用窗口。

这就是我正在做的:

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

窗口像我想要的那样被禁用,但光标仍然是箭头。 如何使其成为等待光标?

根据这个问题,它似乎适用于 vg1890禁用 WPF 窗口中除一个控件之外的所有控件

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

This is what I'm doing:

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?

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

没有伤那来痛 2024-07-25 02:49:52

另一种方法是全局更改光标,是..

Mouse.OverrideCursor = Cursors.Wait;

//Do something
//...

Mouse.OverrideCursor = null;

Another way is to change the cursor globally, is..

Mouse.OverrideCursor = Cursors.Wait;

//Do something
//...

Mouse.OverrideCursor = null;
心如荒岛 2024-07-25 02:49:52

这里发生的情况似乎是 WPF 忽略了禁用窗口上的光标设置。 以下解决方法似乎有效:不要禁用窗口本身,而是禁用窗口的内容

C#:

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

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

Visual Basic:

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

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

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:

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

' and in RunWorkerCompleted handler:'
DirectCast(Content, UIElement).IsEnabled = True
Cursor = Cursors.Arrow
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文