如何在WPF应用程序启动期间显示等待光标?

bsh*_*52s 18 c# wpf cursor

以下是我希望在WPF应用程序启动时发生的基本事件.这与Word在我的机器上启动的方式非常相似.

  1. 显示忙碌光标.
  2. 执行基本初始化.这需要几秒钟,需要在显示启动画面之前完成.
  3. 显示启动画面.此启动画面显示更深入的初始化进度,可能需要一段时间(从数据库缓存信息).
  4. 显示默认光标.由于启动画面现在正在显示进度,因此无需显示忙碌光标.
  5. 启动屏幕进度完成后,显示主窗口.
  6. 关闭启动画面.

一切正常,除了在显示启动画面之前显示忙碌光标.当我通过快捷方式执行应用程序时,等待光标闪烁,但很快又回到默认状态.我已经尝试了不同的方法设置Cursor但没有工作,但我认为问题是我不在控件/窗口中 - 我是从App.xaml.cs中做到的.而且,我设置的属性似乎是Windows窗体属性.以下是我在App.xaml.cs中的代码的摘录.

protected override void OnStartup(StartupEventArgs e)
{
  base.OnStartup(e);

  System.Windows.Forms.Application.UseWaitCursor = true;
  //System.Windows.Forms.Cursor.Current = System.Windows.Forms.Cursors.WaitCursor;
  //System.Windows.Forms.Application.DoEvents();

  Initialize();

  SplashWindow splash = new SplashWindow();
  splash.Show();

  System.Windows.Forms.Cursor.Current = System.Windows.Forms.Cursors.Default;

  // Right now I'm showing main window right after splash screen but I will eventually wait until splash screen closes.
  MainWindow main = new MainWindow();
  main.Show();
}
Run Code Online (Sandbox Code Playgroud)

Kev*_*lia 46

这应该工作

Mouse.OverrideCursor = System.Windows.Input.Cursors.Wait;
Run Code Online (Sandbox Code Playgroud)

使用System.Windows.InputSystem.Windows.Forms.


小智 28

如果您的任务需要花费大量时间,而且它在非GUI线程上运行(这是一个好主意),您可以使用此代码更改应用程序游标:

Application.Current.Dispatcher.Invoke(() =>
{
    Mouse.OverrideCursor = Cursors.Wait;
});
Run Code Online (Sandbox Code Playgroud)

繁忙过程完成后,使用以下命令:

Application.Current.Dispatcher.Invoke(() =>
{
    Mouse.OverrideCursor = null;
});
Run Code Online (Sandbox Code Playgroud)


Bia*_*man 5

        Mouse.OverrideCursor = System.Windows.Input.Cursors.Wait;
        InitializeComponent();
        ...
        Mouse.OverrideCursor = null;
Run Code Online (Sandbox Code Playgroud)