Do an action from ViewModel right after ShowDialog

wingerse

I have a ViewModel like this:

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 is another Viewmodel whose Completed event is triggered when a Command on it is completed (The event is only triggered when correct login credentials are used). OtherVm is another vm whose completed event is triggered for whatever reason.

I render the View using a DataTemplate. For example:

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

The DataContext of this window is set to WelcomeWindowVm class above, before ShowDialog.

This works well. When the Window is shown using ShowDialog, LoginVm is shown. Then OtherVm when whatever task of LoginVm is completed, and so on.

Now I thought of converting the Completion stuff to Async/await pattern. The LoginVm now looks like this:

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;
    }
}

Instead of this:

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

So that I can use it like this:

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
}

But I can't use await in a Constructor and even if I use an async Method for that, how will I call it in another class after calling ShowDialog since ShowDialog blocks?

I think using an async void will work. But from what I have heard, it should be avoided unless I am using it in an event handler.

Maybe use an async Task method but not await it?

Evk

You can do it like this:

    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 =>
            {

            });
        });
    }

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Do not understand form action right

From Java

showDialog from root widget

From Dev

Get size from ShowDialog

From Dev

Do action after foreach and dispatch

From Dev

Xamarin View not Binding from viewModel after Constructor

From Dev

How do you decouple your ViewModel properties validation from ViewModel?

From Dev

how to perform an action right after user finish inputting data in the textfield?

From Dev

Why MDI Child not gaining focus after user returns from Form.ShowDialog()

From Java

How do I get the result from login action to check in another component Menu after login success

From Dev

Selenium IDE - how to get result of verifyNotText and do the right action

From Dev

Selenium IDE - how to get result of verifyNotText and do the right action

From Dev

Do custom action after devise model confirmation

From Dev

Javascript : Do Action after elapsed time

From Dev

Jquery do another action after load() complete

From Dev

Do custom action after devise model confirmation

From Dev

Do an action after two rounds of a for loop

From Dev

Alertdialog automatically do action after timer

From Dev

ViewModel not passing value from Register Action to Async Register Action in MVC 5

From Dev

Form hides behind the other form after ShowDialog()

From Dev

Form hides behind the other form after ShowDialog()

From Dev

SaveFileDialog closes automatically directly after calling showDialog()

From Dev

the "done" message after a &-command is not displayed right away, but after a user action. Why?

From Dev

Right way to expose properties in a ViewModel

From Dev

Do overridden actions inherit action filters from the base action?

From Dev

How do I pass a ViewModel to an Edit Action in ASP.NET MVC4

From Dev

Floating Action Menu to right

From Dev

ShowDialog() ends prematurely if called from hidden windows

From Dev

Form ShowDialog Issue When Drag From TabControl

From Dev

How to do some operations right after application is installed?

Related Related

  1. 1

    Do not understand form action right

  2. 2

    showDialog from root widget

  3. 3

    Get size from ShowDialog

  4. 4

    Do action after foreach and dispatch

  5. 5

    Xamarin View not Binding from viewModel after Constructor

  6. 6

    How do you decouple your ViewModel properties validation from ViewModel?

  7. 7

    how to perform an action right after user finish inputting data in the textfield?

  8. 8

    Why MDI Child not gaining focus after user returns from Form.ShowDialog()

  9. 9

    How do I get the result from login action to check in another component Menu after login success

  10. 10

    Selenium IDE - how to get result of verifyNotText and do the right action

  11. 11

    Selenium IDE - how to get result of verifyNotText and do the right action

  12. 12

    Do custom action after devise model confirmation

  13. 13

    Javascript : Do Action after elapsed time

  14. 14

    Jquery do another action after load() complete

  15. 15

    Do custom action after devise model confirmation

  16. 16

    Do an action after two rounds of a for loop

  17. 17

    Alertdialog automatically do action after timer

  18. 18

    ViewModel not passing value from Register Action to Async Register Action in MVC 5

  19. 19

    Form hides behind the other form after ShowDialog()

  20. 20

    Form hides behind the other form after ShowDialog()

  21. 21

    SaveFileDialog closes automatically directly after calling showDialog()

  22. 22

    the "done" message after a &-command is not displayed right away, but after a user action. Why?

  23. 23

    Right way to expose properties in a ViewModel

  24. 24

    Do overridden actions inherit action filters from the base action?

  25. 25

    How do I pass a ViewModel to an Edit Action in ASP.NET MVC4

  26. 26

    Floating Action Menu to right

  27. 27

    ShowDialog() ends prematurely if called from hidden windows

  28. 28

    Form ShowDialog Issue When Drag From TabControl

  29. 29

    How to do some operations right after application is installed?

HotTag

Archive