简体   繁体   中英

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

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.

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

在此处输入图像描述

and this is my BaseViewModel

在此处输入图像描述

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>

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM