标题说明了一切......
老实说,我迷失在这里......我正在使用WPF来做到这一点。我已经尝试过使用后台工作程序,Thread,Dispatcher.BeginInvoke()和async-await来实现我的目标但无济于事...我已经确定async-await,因为我认为这更容易在SO中发布...
这是我的代码:
C#代码背后:
private async void btnLogin_Click(object sender, RoutedEventArgs e)
{
btnLogin.Visibility = Visibility.Hidden;
RollingIcon.Visibility = Visibility.Visible;
await DoLogin();
btnLogin.Visibility = Visibility.Visible;
RollingIcon.Visibility = Visibility.Collapsed;
}
private Task DoLogin()
{
return TaskEx.Run(() => {
//code for validation if no nulls
this.ViewModel.ValidateUserCredentials();
});
}
XAML
<Button x:Name="btnLogin"
Content="Login"
Margin="10,10,10,0"
IsDefault="True"
Foreground="White"
Background="#FF757575"
Click="btnLogin_Click" />
<local:SpinningAnimation x:Name="RollingIcon" Width="40" Height="40" Margin="10,10,10,0" Visibility="Collapsed">
</local:SpinningAnimation >
显然,当我试图在另一个线程上填充ViewModel时,会弹出System.InvalidOperationException
,因为ViewModel是在主线程中创建的。
请引导我走正确的道路,让我远离黑暗面......
任何事情都会受到赞赏。
TIA