0

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.

fnllc
  • 3,047
  • 4
  • 25
  • 42
  • Any reason why not using the [pre-built validation behaviors](https://learn.microsoft.com/en-us/xamarin/community-toolkit/behaviors/validationbehavior) from xamarin community toolkit package ? With Flags="ValidateOnValueChanging" – Cfun Jan 30 '21 at 20:56
  • please post the relevant code. Linking to a blog post or repo is not sufficient, – Jason Jan 30 '21 at 21:48
  • I posted the github repo, since I'm unable to get that project to work either. I will look into the Xamarin Toolkit. I didn't see that. It might be a much better solution. – fnllc Jan 31 '21 at 00:17

2 Answers2

0

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>
cahyo
  • 597
  • 2
  • 5
  • 14
0

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".

Cfun
  • 8,442
  • 4
  • 30
  • 62