-
-
Notifications
You must be signed in to change notification settings - Fork 38
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
AsyncCommand: How to set CanExecute to a delegate which includes AsyncCommand.Executing #11
Comments
I am working on an easier way to work in this scenario, but for now the best solution is to have your own // skipping INotifyPropertyChanged implementation for user/pass for brevity
public string Username { get; set; }
public string Password { get; set; }
bool SignInExecuting { get; set; }
public IAsyncCommand SignInCommand { get; set; }
public SignInPageModel()
{
SignInCommand = new AsyncCommand(SignInAsync, SignInCommandCanExecute);
PropertyChanged += delegate { ((AsyncCommand) SignInCommand).OnCanExecuteChanged(); };
}
private async Task SignInAsync(object _)
{
SignInExecuting = true;
// actual sign-in process here
await DoSomeWork();
SignInExecuting = false;
}
private bool SignInCommandCanExecute(object _)
{
return !string.IsNullOrEmpty(Username) &&
!string.IsNullOrEmpty(Password) &&
!SignInExecuting;
} Regarding |
That is, in fact, exactly what I'm currently doing. :) How do you envision this working in the future? And do you have an (order-of-magnitude) ETA? |
One part of it is that I'm planning to enhance Another part of it is a fixing of the (horrible) You'd still have to change the command type to |
So if I understand correctly, you want something like a In short, can you give an example (based on the above) of how would a developer would use Note that I have only actually worked as a software developer (and with C#) for a few months, so I might be missing stuff that's obvious to more experienced developers. |
Another possibility might be to use the bound functions from issue #5, or more generally to call CanExecuteChanged from a property notification callback. |
Oops issue #3 not 5. |
I have a viewmodel for a sign-in form (in Xamarin.Forms). I want the following:
Simplified, the viewmodel (which doesn't work properly) is defined like this:
What I'm mainly wondering is how I should change this to implement requirement 1. When the command is invoked, there is no way I can call
OnCanExecuteChanged
so thatSignInCommandCanExecute
returns the correct value. If I callOnCanExecuteChanged
just before and afterawait DoSomeWork()
(which is the earliest and latest I can do it AFAIK, at least without mucking around in the UI codebehind), then the command (and the button that binds to it) is disabled just a tad bit late, and not re-enabled at all (because it's still executing as long asSignInAsync
is running).Unless I have missed an obvious solution, I suggest you fix this by calling
OnCanExecuteChanged
during the command execution process whether or not CanExecute is set when instantiating the AsyncCommand (i.e., https://github.com/StephenCleary/Mvvm.Async/blob/master/src/Nito.Mvvm.Async/AsyncCommand.cs#L126-L127 and later in that method).Furthermore, for requirement 2, the obvious solution is to bind the text fields'
IsEnabled
toSignInCommand.Executing
. However, the command is of typeIAsyncCommand
and needs to be cast toAsyncCommand
in order to access theExecuting
property. Do I really have to make a ValueConverter just for this, or is there a simpler/more elegant way?The text was updated successfully, but these errors were encountered: