Hi guys,
If I have a ViewModel with an async method, how can I define a Command to bind to a button in my View?
The problem I see is that when I click the Login button, I am not navigated from my login View to my main view, but if I click somewhere on the page, then the navigation happens indeed. If I remove async from the method, this works from the first click.
I am reading in the docs about Application.Update(this), but I don’t think it is reasonable to call Application.Update(this) from the ViewModel, what “this” should be in that case?
Is this even possible, or am I trying in vain?
Best regards,
Alex
For example, I have a LoginViewModel with the following method.
And I am trying to define my command in the constructor:
You’re correct that the call to Application.Update does not have to happen in the ViewModel.
Here’s a sample showing how to do async commands in Wisej MVVM.
– The async method should be in the ViewModel,
– Navigation or shell swapping should happen in an application-level ViewModel reacting to authentication state
– Any Wisej-specific Application.Update(…) usage should be hidden in infrastructure that restores/pushes the Wisej context rather than called from the ViewModel itself.
– In the sample, look at:
– LoginViewModel.cs for the async login logic
– ApplicationViewModel.cs for shell swapping (MainViewModel and LoginViewModel are the shells)
– WisejAsyncCommand.cs and WisejUiDispatcher.cs for the Wisej-aware command/context handling
– WisejUIDispatcher for the call to Application.Update
– Window1.cs for the shell host.
Dear Julie,
thanks for your answer.
So you mean in the definition of the LoginCommand in the ViewModel’s constructor:
However I find it a bit strange, in the sense I would expect my ViewModel to be unaware of Wisej’s Application.Current etc. What if I wanted to use the same ViewModel for a WinForms application and not a Wisej one?
Best regards,
Alex
Application.Update(this) works when this is a Wisej control. But in your case, this is the ViewModel. So don’t use Application.Update(this). Instead, you can save the context before the await by doing:
var context = Application.Current.
Then when you need to update the UI you can do:
Application.Update(context);
More information on Application.Current: https://docs.wisej.com/api/wisej.web/general/application#current
Please login first to submit.
