I would like to do a simple Can Execute for my login page. Here is my code:
(Email & Password are view model properties)
(ex// string email; public string Email { get {...} set {...} })
LoginPageVM
{
Login = new AsyncCommand(OnLogin, LoginEntered())
}
public bool LoginEntered()
{
return !string.IsNullOrEmpty(email) && !string.IsNullOrEmpty(password);
}
The LoginEntered() in the command init gives me this error:
CS1503: Argument 2: cannot convert from 'bool' to 'System.Func<object, bool>?'
Could anybody explain how I should fix this. I would also like to eventually pass parameters with the func so if somebody could explain what is going on here that would be awesome! I understand that Func<object,bool> is a func delegate that takes in an object then returns a bool but I can't seem to figure out how to correctly return a bool.
I tried to change the syntax and got another error:
Func<object, bool> LoginEntered = (object obj) => !string.IsNullOrEmpty(email) && !string.IsNullOrEmpty(password);
This gave me more errors claiming that I can't access non-static variables:
CS0236: A field initializer cannot reference the non-static field, method, or property 'LoginPageVM.Email'
CS0236: A field initializer cannot reference the non-static field, method, or property 'LoginPageVM.Password'
To make my question clear, how would I be able to write a Can Execute for an async command with no parameters as well as one with parameters. I would also like to know how to trigger the CanExecuteChanged with the XCT/MVVM Helpers plugin syntax within my properties.
Hope to hear back from somebody soon!
Thank You in Advance!
Thanks for the fast response!
Everything in my view model is bound correctly and I have fixed my syntax error by making my method:
public bool LoginEntered(object obj)
{
return { ... }
}
Making the method void gives me the following error:
CS0407: 'void LoginPageVM.LoginEntered(object)' has the wrong return type
The problem I'm having now is triggering the CanExecuteChanged. My properties look like this:
string email;
public string Email
{
get => email;
set
{
SetProperty(ref email, value);
Login.CanExecuteChanged()
}
}
The problem is that Login.CanExecuteChanged() gives me these errors:
CS0079: The event 'ICommand.CanExecuteChanged' can only appear on the left hand side of += or -=
CS7036: There is no argument given that corresponds to the required formal parameter 'sender' of 'EventHandler
Hope to hear from you soon!