C# 如何在 WPF 应用程序启动期间显示等待光标?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11021422/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I display wait cursor during a WPF application's startup?
提问by bsh152s
Here are the basic events I want to happen when my WPF application starts. This is very similar to how Word starts on my machine.
以下是我希望在 WPF 应用程序启动时发生的基本事件。这与 Word 在我的机器上启动的方式非常相似。
- Display busy cursor.
- Perform basic initialization. This takes a couple of seconds and needs to be done before splash screen is displayed.
- Display splash screen. This splash screen displays progress into more in-depth initialization and can take awhile (caches information from database).
- Display default cursor. Since splash screen is displaying progress now, there's no need to display a busy cursor.
- Once splash screen progress is complete, display main window.
- Close splash screen.
- 显示忙光标。
- 执行基本初始化。这需要几秒钟的时间,并且需要在显示闪屏之前完成。
- 显示启动画面。此初始屏幕显示进入更深入初始化的进度,可能需要一段时间(从数据库缓存信息)。
- 显示默认光标。由于启动画面现在正在显示进度,因此无需显示繁忙的光标。
- 启动画面进度完成后,显示主窗口。
- 关闭启动画面。
Everything works fine except for the displaying of the busy cursor prior to the splash screen being displayed. When I execute the application through a shortcut, the wait cursor flashes, but soon goes back to the default. I've tried different ways to set the Cursor but none work, but I think the problem is that I'm not in a control/window--I'm doing it from within App.xaml.cs. And, the properties I'm setting seem to be Windows Forms properties. Here is an excerpt from my code in App.xaml.cs.
除了在显示闪屏之前显示忙光标外,一切正常。当我通过快捷方式执行应用程序时,等待光标闪烁,但很快又恢复到默认值。我尝试了不同的方法来设置 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();
}
回答by Kevin DiTraglia
This should work
这应该工作
Mouse.OverrideCursor = System.Windows.Input.Cursors.Wait;
Use System.Windows.Inputnot System.Windows.Forms.
使用System.Windows.Input不System.Windows.Forms。
回答by user2069105
If you have a task that takes a significant amount of time, and it is running on a non-GUI thread, (which is a good idea) you can use this code to change the application cursor:
如果您的任务需要花费大量时间,并且它在非 GUI 线程上运行(这是个好主意),您可以使用以下代码更改应用程序光标:
Application.Current.Dispatcher.Invoke(() =>
{
Mouse.OverrideCursor = Cursors.Wait;
});
When the busy process completes, use this:
当繁忙的进程完成时,使用这个:
Application.Current.Dispatcher.Invoke(() =>
{
Mouse.OverrideCursor = null;
});
回答by Nathan Strong
I'm assuming the Initialize() is the part that you want your busy cursor to appear for, yes?
我假设 Initialize() 是您希望忙碌光标出现的部分,是吗?
If so, try the following approach:
如果是这样,请尝试以下方法:
- In your MainWindow.xaml, on the
<Window>element, set the following properties:Visibility="Hidden"andCursor="Wait". - In your MainWindow.xaml.cs, move the initialization code out of the constructor and into a public Initialize() method, so that any code that depends on the Initialize() call doesn't get executed. Make sure the end of your Initialize() method sets the
Visiblityproperty toVisibleand resets theCursoras well. - Update the code snippet posted above to something like the following:
- 在 MainWindow.xaml 中的
<Window>元素上,设置以下属性:Visibility="Hidden"和Cursor="Wait". - 在 MainWindow.xaml.cs 中,将初始化代码移出构造函数并移到公共 Initialize() 方法中,以便不会执行依赖于 Initialize() 调用的任何代码。确保 Initialize() 方法的末尾也将
Visiblity属性设置为Visible并重置Cursor。 - 将上面发布的代码片段更新为如下所示:
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.
}
回答by Bianca Kalman
Mouse.OverrideCursor = System.Windows.Input.Cursors.Wait;
InitializeComponent();
...
Mouse.OverrideCursor = null;
回答by Tanque
For me it worked using a mix of the stuff stated above:
对我来说,它混合使用了上述内容:
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
}
}

