xamarin.formsmvvmtextchangedbindableproperty

No property, BindableProperty, or event found for "Clicked", or mismatching type between value and property. TextChanged method doesn't get recognized


So, I found some similar questions but it didn't help me to solve my problem. Since it's the first time using Xamarin.Forms, I made the error of not using ViewModels so I'm moving all the methods to viewmodels. As I think I understood, TextChanged is an event and not a BindableProperty so what I'm doing won't work.

I'm checking regex to validate an email based on TextChanged. This is the code:

<controls:StandardEntry x:Name="email" Text="{Binding Email}" TextChanged="{Binding ValidateEmail}"/>

public void ValidateEmail(object sender, TextChangedEventArgs e)
        {
            if (RegexUtils.ValidEmail().IsMatch(email))
            {
                // do stuff
            }
            else
            {
                // do stuff
            }
        }

Solution

  • MyViewModel VM => (MyViewModel)BindingContext;
    

    Then to access a VM property:

    ... VM.SomeProperty ...
    

    So if TextChanged previously was able to use SomeProperty directly, because it was in the view, change that to VM.SomeProperty everywhere in TextChanged.