【问题标题】:Do an action from ViewModel right after ShowDialog在 ShowDialog 之后立即从 ViewModel 执行操作
【发布时间】:2016-07-22 11:19:54
【问题描述】:

我有一个这样的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,其 Completed 事件在其上的命令完成时触发(该事件仅在使用正确的登录凭据时触发)。 OtherVm 是另一个虚拟机,它的完成事件因任何原因而被触发。

我使用DataTemplate 渲染View。例如:

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

此窗口的 DataContext 设置为上面的WelcomeWindowVm 类,在ShowDialog 之前。

这很好用。当使用 ShowDialog 显示窗口时,会显示LoginVm。然后OtherVm当LoginVm的任何任务完成时,以此类推。

现在我想将 Completion 的东西转换为 Async/await 模式。 LoginVm 现在看起来像这样:

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 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);
        // ...
    }
}

这样我就可以这样使用它了:

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
}

但是我不能在构造函数中使用等待,即使我为此使用异步方法,由于ShowDialog 阻塞,在调用ShowDialog 之后我将如何在另一个类中调用它?

我认为使用async void 会起作用。但据我所知,除非我在事件处理程序中使用它,否则应该避免它。

也许使用async Task 方法而不是await 它?

【问题讨论】:

    标签: c# wpf mvvm async-await task


    【解决方案1】:

    你可以这样做:

        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 void 方法中并从构造函数中调用它,但这很糟糕(因为如何处理异常)所以最好不要这样做。我个人认为 ContinueWith 没有问题,但如果您这样做,请考虑阅读以下内容:blog.stephencleary.com/2013/01/async-oop-2-constructors.html
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-01-06
    • 1970-01-01
    • 1970-01-01
    • 2018-03-16
    • 2012-02-23
    • 1970-01-01
    相关资源
    最近更新 更多