我在异步等待方面相当落后,所以这可能是一个“duh”问题。
我正在开发一个非常小的 UI 应用程序,它使用以下命令从系统托盘运行WPF 通知图标 http://www.codeproject.com/Articles/36468/WPF-NotifyIcon#context_menus图书馆。
应用程序应该以以下方式非常简单地运行(对于用户而言):
- 节目开始
- 如有必要,会有一个启动屏幕告诉用户程序正在运行并提示他们登录(如果他们在之前的迭代中尚未登录)。
- WPF NotifyIcon 将出现在系统托盘中。
- 异步执行开始
我遇到的问题是“异步执行开始”部分。在此之前发生的所有事情都工作正常,但是当程序开始尝试“运行”时,UI 会锁定(我的意思是,用户可以像疯子一样单击托盘图标,并且上下文菜单拒绝出现)。
这种锁定发生的时间长得令人无法接受。
这是启动代码:
private async void AppStartup( object sender, StartupEventArgs e ) {
this.TRSIcon = this.FindResource( "TRSIcon" ) as TaskbarIcon;
if ( Settings.Default.DoUpgrade ) { //Upgrade if necessary.
Settings.Default.Upgrade( );
Settings.Default.DoUpgrade = false;
Settings.Default.Save( );
}
if ( string.IsNullOrEmpty( Settings.Default.Username ) || string.IsNullOrEmpty( Settings.Default.Password ) ) {
new Help( ).ShowDialog( );
Tuple<string, string> UP;
if ( ( UP = Login.Instance.GetUserPassword( ) ) != null ) {
Settings.Default.Username = UP.Item1;
Settings.Default.Password = UP.Item2;
Settings.Default.Save( );
} else
return;
}
await this.Start( ); //<-----This is where the meat of the program runs and it hangs the UI until it finishes.
return; //<-----This is just so that I have a break point to see that await this.Start is blocking (I have to do it like that right? or do I?)
}
这是Resources.xaml
:
<ResourceDictionary
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:Tools="clr-namespace:WPFTools.TaskbarNotification;assembly=WPFTools"
xmlns:TR="clr-namespace:TriviaRetriever">
<ContextMenu x:Key="TSRInterfaceMenu" x:Shared="false">
<MenuItem Header="Login" Command="{Binding cmdLogin}"/>
<MenuItem Header="Get My Trivia" Command="{Binding cmdDownload}"/>
<MenuItem Header="Register" Command="{Binding cmdRegister}"/>
<MenuItem Header="Lost Password" Command="{Binding cmdLostPassword}"/>
<MenuItem Header="About" Command="{Binding cmdAbout}"/>
<MenuItem Header="Log Out" Command="{Binding cmdLogout}"/>
<MenuItem Header="Exit" Command="{Binding cmdExit}"/>
</ContextMenu>
<Tools:TaskbarIcon
x:Key="TRSIcon"
MenuActivation="LeftOrDoubleClick"
IconSource="/TRIcon.ico"
DoubleClickCommand="{Binding cmdAbout}"
ContextMenu="{StaticResource TSRInterfaceMenu}">
<Tools:TaskbarIcon.DataContext>
<TR:TRSIViewModel/>
</Tools:TaskbarIcon.DataContext>
</Tools:TaskbarIcon>
</ResourceDictionary>
这是上下文菜单命令的 MVVM:
public class TRSIViewModel {
public ICommand cmdLogin {
get {
return new DelegateCommand {
fncCanExecute = ( ) => ( Application.Current as App ).Core == null,
actCommand = async ( ) => {
Tuple<string, string> LoginPassword = Login.Instance.GetUserPassword( );
if ( LoginPassword != null ) {
Settings.Default.Username = LoginPassword.Item1;
Settings.Default.Password = LoginPassword.Item2;
Settings.Default.Save( );
await ( Application.Current as App ).Start( );
}
}
};
}
}
public ICommand cmdLogout {
get {
return new DelegateCommand {
fncCanExecute = ( ) => ( Application.Current as App ).Core != null,
actCommand = ( ) => {
( Application.Current as App ).Core.Terminate( );
( Application.Current as App ).Core = null;
}
};
}
}
public ICommand cmdRegister {
get {
return new DelegateCommand {
fncCanExecute = ( ) => true,
actCommand = ( ) => Process.Start( @"https://www.digigames.com/weekly_subscriptions/index.php" )
};
}
}
public ICommand cmdLostPassword {
get {
return new DelegateCommand {
fncCanExecute = ( ) => true,
actCommand = ( ) => Process.Start( @"https://www.digigames.com/weekly_subscriptions/lost_password.php" )
};
}
}
public ICommand cmdAbout {
get {
return new DelegateCommand {
fncCanExecute = ( ) => true,
actCommand = ( ) => ( Application.Current as App ).TRSIcon.ShowCustomBalloon( new About( ), PopupAnimation.Slide, 5000 )
};
}
}
public ICommand cmdExit {
get {
return new DelegateCommand {
fncCanExecute = ( ) => true,
actCommand = ( ) => {
if ( ( Application.Current as App ).Core != null )
( Application.Current as App ).Core.Terminate( );
Application.Current.Shutdown( 0 );
}
};
}
}
public ICommand cmdDownload {
get {
return new DelegateCommand {
fncCanExecute = ( ) => ( Application.Current as App ).Core != null,
actCommand = async ( ) => await ( Application.Current as App ).Core.DownloadTrivia( true )
};
}
}
public class DelegateCommand : ICommand {
public Action actCommand { get; set; }
public Func<bool> fncCanExecute { get; set; }
public bool CanExecute( object parameter ) {
return this.fncCanExecute != null && this.fncCanExecute( );
}
public event EventHandler CanExecuteChanged {
add { CommandManager.RequerySuggested += value; }
remove { CommandManager.RequerySuggested -= value; }
}
public void Execute( object parameter ) { this.actCommand( ); }
}
}
我在这里做错了什么?