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

时间:2012-06-13 18:49:42

标签: c# wpf cursor

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

  1. 显示忙碌光标。
  2. 执行基本初始化。这需要几秒钟,需要在显示启动画面之前完成。
  3. 显示启动画面。此启动画面显示更深入的初始化进度,可能需要一段时间(从数据库缓存信息)。
  4. 显示默认光标。由于启动画面现在正在显示进度,因此无需显示忙碌光标。
  5. 完成启动画面进度后,显示主窗口。
  6. 关闭启动画面。
  7. 除了在显示启动画面之前显示忙碌光标外,一切正常。当我通过快捷方式执行应用程序时,等待光标闪烁,但很快又回到默认状态。我已经尝试了不同的方法设置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();
    }
    

5 个答案:

答案 0 :(得分:43)

这应该有效

Mouse.OverrideCursor = System.Windows.Input.Cursors.Wait;

使用System.Windows.Input而非System.Windows.Forms

答案 1 :(得分:23)

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

Application.Current.Dispatcher.Invoke(() =>
{
    Mouse.OverrideCursor = Cursors.Wait;
});

繁忙过程完成后,请使用:

Application.Current.Dispatcher.Invoke(() =>
{
    Mouse.OverrideCursor = null;
});

答案 2 :(得分:2)

我假设Initialize()是您希望忙碌光标出现的部分,是吗?

如果是,请尝试以下方法:

  1. 在您的MainWindow.xaml中的<Window>元素上,设置以下属性:Visibility="Hidden"Cursor="Wait"
  2. 在MainWindow.xaml.cs中,将初始化代码移出构造函数并移入公共Initialize()方法,以便任何依赖于Initialize()调用的代码都不会被执行。确保Initialize()方法的结尾将Visiblity属性设置为Visible并重置Cursor
  3. 将上面发布的代码段更新为以下内容:
  4. protected override void OnStartup(StartupEventArgs e)
    {
      base.OnStartup(e);
    
      MainWindow main = new MainWindow();
      main.Show(); // this should set the cursor how you want it
      Initialize();
      SplashWindow splash = new SplashWindow();
      splash.Show();
      main.Initialize(); // now invoke the Initialize method you created
      // Right now I'm showing main window right after splash screen but I will eventually wait until splash screen closes.
    }
    

答案 3 :(得分:0)

        Mouse.OverrideCursor = System.Windows.Input.Cursors.Wait;
        InitializeComponent();
        ...
        Mouse.OverrideCursor = null;

答案 4 :(得分:0)

对我来说,它结合了上面提到的内容而起作用:

class MyForm : System.Windows.Window {}

class Test{
   MyForm myForm;

   void ShowWaitCurserInMyForm(){
      //before kicking off the stuff I'm waiting for: 
      System.Windows.Forms.Application.UseWaitCursor = true; // disables all Input from the mouse
      myForm.Cursor = System.Windows.Input.Cursors.Wait; // actually displays a wait Cursor

      // do time intensive stuff here, if we wait for an event, following stuff belongs in its handler

      System.Windows.Forms.Application.UseWaitCursor = false; // reenables all Input from the mouse
      myForm.Cursor = null; // reset the Cursor visually
   }
}
相关问题