以下是我希望在WPF应用程序启动时发生的基本事件。这与Word在我的机器上启动的方式非常相似。
除了在显示启动画面之前显示忙碌光标外,一切正常。当我通过快捷方式执行应用程序时,等待光标闪烁,但很快又回到默认状态。我已经尝试了不同的方法设置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();
}
答案 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()是您希望忙碌光标出现的部分,是吗?
如果是,请尝试以下方法:
<Window>
元素上,设置以下属性:Visibility="Hidden"
和Cursor="Wait"
。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.
}
答案 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
}
}