MessageBox.Show 导致无法拾取光标的变化

mar*_*etz 2 c# wpf

我有一个标准的 WPF MainWindow 类,我想要的是使用 显示一个消息框System.Windows.MessageBox,从用户那里获得响应,然后运行一个长时间运行的操作(下面通过调用 模拟Sleep(...))。我想将光标设置为Cursors.Wait操作前,并在结束时恢复正常。这是我所拥有的:

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
    }

    private void ui_button_Click(object sender, RoutedEventArgs e)
    {
        if (MessageBox.Show("Do you want to change the background?", "Change background", MessageBoxButton.YesNo) == MessageBoxResult.No)
        {
            return;
        }

        Cursor = Cursors.Wait;

        Dispatcher.BeginInvoke(DispatcherPriority.Normal, new Action(() =>
            {
                System.Threading.Thread.Sleep(1500);

                if (Background != Brushes.Green)
                {
                    Background = Brushes.Green;
                }
                else
                {
                    Background = Brushes.White;
                }
                Cursor = Cursors.Arrow;
            }));
    }
}
Run Code Online (Sandbox Code Playgroud)

这不起作用:光标永远不会作为等待光标出现。但是,如果我注释掉这些MessageBox行,它确实有效。这里发生了什么,我怎样才能让它按预期工作?

met*_*art 6

以下代码对我有用:而不是

Cursor = Cursors.Wait;
Run Code Online (Sandbox Code Playgroud)

尝试这个:

Mouse.OverrideCursor = Cursors.Wait;
Mouse.UpdateCursor();
Run Code Online (Sandbox Code Playgroud)

您以相反的方式关闭等待光标:

Mouse.OverrideCursor = null;
Mouse.UpdateCursor();
Run Code Online (Sandbox Code Playgroud)