C# 在ShowDialog之后立即从ViewModel执行操作

C# 在ShowDialog之后立即从ViewModel执行操作,c#,wpf,mvvm,async-await,task,C#,Wpf,Mvvm,Async Await,Task,我有一个像这样的ViewModel: public class WelcomeWindowVm : ViewModel { private ViewModel view; public WelcomeWindowVm(){ this.View = new LoginVm() { Completed += (o, e) => { this.View = new OtherVm(e.User){

我有一个像这样的
ViewModel

public class WelcomeWindowVm : ViewModel
{
    private ViewModel view;

    public WelcomeWindowVm(){
        this.View = new LoginVm() { 
            Completed += (o, e) => {
                this.View = new OtherVm(e.User){ 
                    Completed += (o, e) =>; // and so on
                } 
            }
        };
    }

    public ViewModel View {
        get {
            return this.view;
        }

        set {
            this.view = value;
            this.OnPropertyChanged(nameof(this.View));
        }
    }
}
LoginVm
是另一个Viewmodel,其已完成事件在其上的命令完成时触发(仅当使用正确的登录凭据时才会触发该事件)
OtherVm
是另一个vm,其已完成事件因任何原因被触发

我使用
数据模板
呈现
视图
。例如:

<Window.Resources>   
   <DataTemplate DataType="vm:LoginVm">
         Textboes and buttons here
    </DataTemplate>
    <DataTemplate DataType="vm:OtherVm">
        ...
    </DataTemplate>
</Window.Resources>
<ContentControl Content={Binding View} />
与此相反:

public LoginVm{
    public event EventHandler<CustomArgs> Completed;

    // This is the Relay command handler
    public async void Login()
    {
        // Code to check if credentials are correct
        OnCompleted(this.user);
        // ...
    }
}
但我不能在构造函数中使用await,即使我使用异步方法,调用
ShowDialog
之后,在另一个类中如何调用它,因为
ShowDialog

我认为使用
异步void
将有效。但据我所知,除非我在事件处理程序中使用它,否则应该避免使用它


可以使用
异步任务
方法,但不能使用
等待
方法?

您可以这样做:

public LoginVm{
    ...
    private TaskCompletionSource<User> taskCompletionSource = new TaskCompletionSource<User>();
    ...
    // This is the Relay command handler
    public async void Login()
    {
        // Code to check if credentials are correct
        this.taskCompletionSource.SetResult(this.user);
        // ...
    }

    public Task<User> Completion(){
        return this.taskCompletionSource.Task;
    }
}
public WelcomeWindowVm(){
    var loginVm = new LoginVm();
    this.View = new LoginVm();
    User user = await loginVm.Completion();

    var otherVm = new OtherVm(user);
    this.View = otherVm;
    Whatever wev = await otherVm.Completion();

    //And so on
}
    public WelcomeWindowVm() {
        var loginVm = new LoginVm();
        this.View = loginVm;
        loginVm.Completion().ContinueWith(loginCompleted =>
        {
            var otherVm = new OtherVm(loginCompleted.Result);
            this.View = otherVm;
            otherVm.Completion().ContinueWith(whateverCompleted =>
            {

            });
        });
    }

谢谢,把它放在一个异步任务方法中而不等待它怎么样?我不太熟悉像这样的定制任务。所以我很好奇。那么,你应该确保异常不能在该方法中抛出。通常不建议忽略等待,因为可能无法观察到内部抛出的异常。若你们考虑到这一点,那个就没问题了(尽管我个人更喜欢上面的方法)。有什么方法可以不使用ContinueWith就完成你们所做的事情吗?仅使用async Wait?我认为您不能做到完全相同。您可以将这些内容放在async void方法中,并从构造函数调用它,但这是不好的(因为异常是如何处理的),所以最好不要这样做。我个人并不认为ContinueWith有问题,但如果你真的这么认为,请考虑一下: