Unable to get PropertyChanged.Fody to work in Xamarin.Forms App

520 Views Asked by At

I'm trying to implement PropertyChanged.Fody in my Xamarin.Forms app. I followed this blog post:

https://xamgirl.com/validation-snippets-in-xamarin-forms/

and used the following git project as a reference:

https://github.com/CrossGeeks/ValidationXFSample.

I'm able to get the validations to work once I press the submit button. However, the validations do not trigger when the field is updated. I'm also unable to get t the example project to work. It properly displays the validations on submit, but not when the field is updated.

I'm using the latest Visual Studio on Mac and iOS Simulator 14.3. Otherwise, I use the defaults of the example project.

2

There are 2 best solutions below

0
On BEST ANSWER

If you want to make your life easier in the future use the pre-built validation behaviors from xamarin community toolkit package (contains lot of repetitive/common stuff used by dev). Email validation, max characters validation, custom validation.. and more out of the box. Regarding when to validate you can set Flags="ValidateOnValueChanging".

0
On

I have another work around that similar like this. Here is my nuget config

enter image description here

and this is my BaseViewModel

enter image description here

Explanation:

  1. Simply add ReactiveAttribute to property you want to

    [Reactive] public bool IsLoading { get; set; }

  2. If you want to handle when the value changed, just add new method with this format On<Property_Name>Changed change <Property_Name> with the property name you want to be handled like OnIsLoadingChanged()

  3. Make sure your BaseViewModel implement INotifyPropertyChanged

  4. This approach depend on ReactiveUI but it will not use all feature of ReactiveUI. You can freely to use another MVVM framework or just use pure Xamarin.Forms

  5. This approach use the power of PropertyChanged.Fody and the simplicity of ReactiveUI.Fody sintax to declare property changed

  6. You just need to add these library to your shared project and nothing todo in platforms specific

I can bind IsLoading just like

<ListView
    IsPullToRefreshEnabled="True"
    IsRefreshing="{Binding IsLoading}">
</ListView>