0

I have a Xamarin Forms project using MVVM in which I am attempting to use Fody and the PropertyChanged.Fody package. No matter what I try it simply does not work. I have spent the day searching for examples of projects using PropertyChanged.Fody and can't find any example sample code that actually works. Can someone help me out? Thanks.

I have added both Fody and PropertyChanged.Fody nugets to the project.

I have added the FodyWeavers.xml file to the Forms project which looks like...

<?xml version="1.0" encoding="utf-8"?>
<Weavers>
    <PropertyChanged />
</Weavers>

My view is a standard ContentPage...

<ContentPage.BindingContext>
    <viewmodels:GetStartedViewModel />
</ContentPage.BindingContext>
<ContentPage.Content>
    <StackLayout >
         <Entry Text="{Binding TestMessage}" TextColor="Lime" FontSize="20" /> 
    </StackLayout>
</ContentPage.Content>

I have tried implementing PropertyChanged.Fody on the ViewModel in 2 ways, both of which fail to work.

1)

public class GetStartedViewModel : INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;
    public string TestMessage { get; set; }
}

2)

[AddINotifyPropertyChangedInterface]
public class GetStartedViewModel
{
    public string TestMessage { get; set; }
}
Sev
  • 883
  • 1
  • 14
  • 34
  • Try implementing your own `OnPropertyChanged([CallerMemberName] string propertyName = null)` method on your ViewModel and see if that works. It may not be properly injecting it for some reason. Also, is `TestMessage` ever actually being set to something? – Nick Peppers Sep 17 '18 at 18:25
  • I am replacing the standard, already existing "OnPropertyChanged([CallerMemberName] string propertyName = null)" with PropertyChanged.Fody, so I know it works using the standard pattern. And yes, I have a button with a command set to change the "TestMessage" property and I have watched it change while debugging. – Sev Sep 17 '18 at 18:53
  • If it works using the standard pattern why not stick with that? Surely a framework like this doesn't give you a huge amount of benefit aside from not having to write INPC implementations which you could create a snippet for to save on time. How does debugging work with something like `Fody`? – Bijington Sep 17 '18 at 19:10
  • I'm saying Fody tries to inject its own `OnPropertyChanged` if it's not implemented, but to know whether that is indeed working or not I was suggesting just add that method back and leave your TestMessage property the same `public string TestMessage { get; set; }`. If it's still not working something else is going on like maybe it doesn't like how you're setting your `BindingContext` from xaml, which you could try doing it from code instead. – Nick Peppers Sep 17 '18 at 19:17
  • I feel dumb now, but I figured it out. It is the interception call I have in the ViewModel to watch for changes in the view for the TwoWay bindings. public void OnPropertyChanged(string propertyName, object before, object after) {} I was just using the general override and checking the property name so it was intercepting all property change events, thus the UI never updated. – Sev Sep 17 '18 at 19:50

0 Answers0