Forum Xamarin.Forms
We are excited to announce that the Xamarin Forums are moving to the new Microsoft Q&A experience. Q&A is the home for technical questions and answers at across all products at Microsoft now including Xamarin!

We encourage you to head over to Microsoft Q&A for .NET for posting new questions and get involved today.

ICommand not works

jon78jon78 Member ✭✭✭
edited October 6 in Xamarin.Forms

Hi, with this implementation my button in viewmodel is always disabled

in xaml i do
Command="{Binding SaveNewCustomerCommand}"
CommandParameter="{Binding NewCustomer}"

If i simply return true in CanExecute the Execute method works
but i cant implement CanExecute: is always disabled

`
public class SaveNewCustomerCommand : ICommand
{
public NewCustomerViewModel ViewModel { get; set; }

    public SaveNewCustomerCommand(NewCustomerViewModel newCustomerViewModel)
    {
        ViewModel = newCustomerViewModel;
    }

    public event EventHandler CanExecuteChanged;

    public bool CanExecute(object parameter)
    {
        var newCustomer = (NewCustomer)parameter;

        if (newCustomer == null) return false;

        if (string.IsNullOrEmpty(newCustomer.Name
            || string.IsNullOrEmpty(newCustomer.Email)
            )
            return false;

        return true;
    }

    public void Execute(object parameter)
    {
        var customer = (NewCustomer)parameter;

        ViewModel.SaveNewCustomer(customer);
    }`
Tagged:

Best Answer

Answers

  • ColeXColeX Member, Xamarin Team Xamurai
    edited October 7

    The type of CommandParameter is object

    You have to specify the property NewCustomer to object type in viewmodel .

     public object NewCustomer { get; set; } //not NewCustomer NewCustomer 
    

    If you use NewCustomer NewCustomer in viewmodel , the parameter passed into CanExecute method would be null .

  • jon78jon78 Member ✭✭✭

    Thanks for your help but

    Every type derives from object and i can say i have this implementation in loginviewmodel and works well

    public LoginCommand LoginCommand { get; }

  • ColeXColeX Member, Xamarin Team Xamurai

    @jon78 said:
    Thanks for your help but

    Every type derives from object and i can say i have this implementation in loginviewmodel and works well

    public LoginCommand LoginCommand { get; }

    Please post the whole code in ViewModel .

  • jon78jon78 Member ✭✭✭
    edited October 7

    With pleasure
    Obviously BaseViewModel is abstract class that implement INotifyPropertyChanged

    `public class NewCustomerViewModel : BaseViewModel
    {
    private readonly NewCustomerRepository newCustomerRepository;

        public SaveNewCustomerCommand SaveNewCustomerCommand { get; }
    
        public NewCustomer NewCustomer { get; set; }
    
        public NewCustomerViewModel(NewCustomerRepository newCustomerRepository)
        {
            NewCustomer = new NewCustomer()
            {
                UserCode = App.User.Code,
                Data = DateTime.Now.ToString(),
                UserId = App.User.Id
            };
    
            this.newCustomerRepository = newCustomerRepository;
            SaveNewCustomerCommand = new SaveNewCustomerCommand(this);
        }
    
        public string Name { get; set; }
        public string Email { get; set; }
        //other fields...
    
        public async void SaveNewCustomer(NewCustomer customer)
        {
            try
            {
                var success = await newCustomerRepository.AddOrUpdateNewCustomer(customer);
                await PageService.DisplayAlert("Success", "Customer saved!", "Ok");
                await PageService.PopModal();
            }
            catch (Exception ex)
            {
                await PageService.DisplayAlert("Error Saving Customer data", ex.Message, "Ok");
            }
        }
    }`
    
  • ColeXColeX Member, Xamarin Team Xamurai

    Change the code in NewCustomerViewModel

    from

     public NewCustomer NewCustomer { get; set; }
    

    to

    public object NewCustomer { get; set; }
    
  • jon78jon78 Member ✭✭✭

    I try two times but nothing change

  • ColeXColeX Member, Xamarin Team Xamurai

    I test on m side it works fine , did you use debugger in method CanExecute and Execute to figure out what's the value of parameter?

  • jon78jon78 Member ✭✭✭
    Maybe i forgot something in the binding of xaml...
    Tomorrow i retry
    Tks for your help
  • ColeXColeX Member, Xamarin Team Xamurai
    edited October 8

    @jon78 Have your problem been solved or not?

Sign In or Register to comment.