Xamarin.Forms DataBinding not working


Xamarin.Forms DataBinding not working



I know that this question has been asked so many times before, but every situation is kinda 'unique', let's put in that way.



My environment is Visual Studio 15.7.4, using all the Nuget packages (Xamarin.Forms above all) updated to the latest version.



The problem is that I created a simple binding test app, and binding doesn't work anymore!
I already created an Android app with Xamarin.Forms some time ago and it all worked just fine.
Now all the bindings works like "OneTime", setting only once at beginning and not updatng in the UI (but still updating the data in the main ViewModel).



I would like to know if it a general Xamarin problem, or is it just me doing something wrong.
I already tried out everything I could, but nothing changed.



Here is the complete code (To test, create a Xamarin.Forms app with "Shared Project" as option; I usually put all the logic app code in the shared part of the project):



App.cs


namespace App1
{
public partial class App : Application
{
public static AppViewModel ViewModel { get; set; }

public App ()
{
InitializeComponent();

ViewModel = new AppViewModel();
MainPage = new MainPage();
}

// Other App lifecycle methods
}
}



AppViewModel.cs


namespace App1.ViewModels
{
public class AppViewModel : BaseBind
{
public AppViewModel()
{
TestoProva = "---";
BoolProva = false;
}

private string testoProva;
public String TestoProva
{
get { return testoProva; }
set
{
SetProperty(ref testoProva, value);
}
}

private bool boolProva;
public Boolean BoolProva
{
get { return boolProva; }
set { SetProperty(ref boolProva, value); }
}
}
}



MainPage


<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:local="clr-namespace:App1"
xmlns:vm="clr-namespace:App1.ViewModels"
x:Class="App1.MainPage">
<ContentPage.BindingContext>
<vm:AppViewModel/>
</ContentPage.BindingContext>
<StackLayout>
<Label Text="{Binding TestoProva, Mode=OneWay}"/>
<Button Text="Test" Clicked="Test"/>
</StackLayout>
</ContentPage>



MainPage.cs


namespace App1
{
public partial class MainPage : ContentPage
{
public static AppViewModel ViewModel => App.ViewModel;

public MainPage()
{
InitializeComponent();
BindingContext = ViewModel;
}

public void Test()
{
ViewModel.TestoProva = "TESTO CAMBIATO!!!";
}
}
}



BaseBind.cs


namespace App1.ViewModels
{
public class BaseBind
{
public event PropertyChangedEventHandler PropertyChanged;

public void OnPropertyChanged([CallerMemberName] string propertyName = null) =>
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));

protected bool SetProperty<T>(ref T storage, T value,
[CallerMemberName] String propertyName = null)
{
if (object.Equals(storage, value)) return false;
storage = value;
OnPropertyChanged(propertyName);
return true;
}
}
}



This is it… a simple app test, because the one at which I was working was display also this problem.



Would really like to know what is wrong..me or Xamarin framework.



Thanks everyone for your attention and your patience.




3 Answers
3



I have not created/run a test project with your code yet, but it looks likely the problem lies with your BaseBind class. While you've added the necessary event, you haven't actually marked it as implementing INotifyPropertyChanged, which is what the binding engine will look for in order to wire up the events.



Try this:


namespace App1.ViewModels
{
public class BaseBind : System.ComponentModel.INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;

public void OnPropertyChanged([CallerMemberName] string propertyName = null) =>
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));

protected bool SetProperty<T>(ref T storage, T value,
[CallerMemberName] String propertyName = null)
{
if (object.Equals(storage, value)) return false;
storage = value;
OnPropertyChanged(propertyName);
return true;
}
}
}





Thank you! I didn't remember the interface! How could I be so stupid. Now it works perfectly. Thanks again.
– LucaLindholm
Jul 1 at 14:48



Shouldn't your Clicked handler in the MainPage.cs be defined like this:
public void Test(object sender, System.EventArgs e) { ... } ?


public void Test(object sender, System.EventArgs e) { ... }





But it fires. By doing some debug, I discovered that everything works, but the UI updating!
– LucaLindholm
Jul 1 at 10:19



Maybe for a reason your OnPropertyChanged(propertyName); calls a OnPropertyChanged(null); at runtime. Check it in debugger.


OnPropertyChanged(propertyName);


OnPropertyChanged(null);






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

How to add background colour in existing image using Swift?

What's the difference between DDS and SOME/IP?

How to make file upload 'Required' in Contact Form 7?