我有一个标准的WPF MainWindow类,我想要显示一个消息框使用System.Windows.MessageBox
,得到用户的响应,然后运行长时间运行(通过致电Sleep(...)
进行模拟)。我想在操作前将光标设置为Cursors.Wait
,并在结束时恢复正常。这是我得到的:MessageBox.Show导致光标不被拾取
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;
}));
}
}
这不起作用:游标永远不会显示为等待游标。但是,如果我注释掉MessageBox
这一行,它的确行得通。这里发生了什么,我如何才能使其按预期工作?
尝试'Mouse.OverrideCursor = Cursors.Wait;' –