Command binding not working on iOS

RiadRiad NLUniversity ✭✭

Hello everyone,

I am facing a strange situation here. I have a command binding that works on Android and not on iOS... Here a part of my VM :

public class MainPageVm : BaseVm { private ICommand _connectionButtonCommand; private ICommand _contactButtonCommand; public MainPageVm (INavigation navigation) { Navigation = navigation; Titre = ResourcesStrings.MainPageTitre; ConnectionButtonText = ResourcesStrings.MainPageConnection; ContactButtonText = ResourcesStrings.MainPageContact; SetTextInView(); } private void SetTextInView() { RaisePropertyChanged(() => Titre); RaisePropertyChanged(() => ConnectionButtonText); RaisePropertyChanged(() => ContactButtonText); } public string Titre { get; } public string ConnectionButtonText { get; } public string ContactButtonText { get; } public INavigation Navigation { get; set; } public ICommand ConnectionButtonCommand { get { if (_connectionButtonCommand != null) return _connectionButtonCommand; var action = new Action(async () => await Navigation.PushAsync(new ConnexionPage()) ); _connectionButtonCommand = new Command(action); return _connectionButtonCommand; } } public ICommand ContactButtonCommand { get { if (_contactButtonCommand != null) return _contactButtonCommand; var action = new Action(async () => await Navigation.PushAsync(new ContactPage())); _contactButtonCommand = new Command(action); return _contactButtonCommand; } } }

And here is a part of my view :
private void SetBindings() { _titreLabel.SetBinding(Label.TextProperty, "Titre"); _connectionButton.SetBinding(Button.TextProperty, "ConnectionButtonText"); _connectionButton.SetBinding(Button.CommandProperty, "ConnectionButtonCommand"); _contactButton.SetBinding(Button.TextProperty, "ContactButtonText"); _contactButton.SetBinding(Button.CommandProperty, "ContactButtonCommand"); BindingContext = new MainPageVm(Navigation); }

As said before, when a _connectionButton is tapped, the event is fired on Android and nothing happens on iOS !

Do you see anything that could help ?

Thank you very much.

Best Answer


  • NMackayNMackay GBInsider, University mod


    Where are you setting the BindingContext?

    Do you call SetBindings from the ctor of the page/view?

    I use a view 1st approach to binding and a ViewmodelLocator pattern but I have set binding context before in code with no issues in iOS or Android. It's hard to see from your code sample where you are setting the binding.

  • RiadRiad NLUniversity ✭✭

    Yes, SetBindings is called from the constructor.

    I am working on someone else code, so I am not aware of the whole project. He did not use an MvvM approach at all, I am implementing it. May be he did something that breaks the binding... Is this something possible ?

  • NMackayNMackay GBInsider, University mod
    edited July 2015


    It's hard to say without seeing the app. I use MVVM light for my Xamarin apps.

    It's worth checking if your command is been created in iOS. Do you get binding errors?

    Bit more here on using commands.

    I never set bindings and define views in code so I'm not sure if your approach works generally. Maybe someone else will spot the issue.

  • RiadRiad NLUniversity ✭✭
    edited July 2015

    Well... It was because of the linker !

    When the linker is set to "Link all assemblies" some of my bindings do not work.
    When the linker is set to "Link SDK assemblies only" I have this compilation error : "Failed to resolve Xamarin.Forms.BindableProperty"
    And when the linker is set to "Don't link" everything - so far - is okay.

    I really need to understand this Linker stuff...

    Thank you for your help @NMackay

  • ManishJain.1426ManishJain.1426 USUniversity ✭✭

    Thanks @Riad
    Your comment fixed my issues on Android too.

  • RichardPesciRichardPesci USUniversity

    So no one has found why Command Bindings are getting severed when using "Link All Assemblies"? What would be a fix if you had to use "Link All Assemblies"?

  • DevinSmithDevinSmith GBUniversity ✭✭

    You might be missing the Xamarin.Build.Download assembly, if it is missing, you can add using nuget.

    I found installing Firebase Analytics failed to load it as an assembly.

  • ReneAlbertReneAlbert USMember

    Android amd UWP works perfectly, baut iOS 10.3 on tablet dosent work.
    On the simulator works.
    Waht ist differnence. What do I have to that works also on teal hardware??????????

  • ReneAlbertReneAlbert USMember

    Android and UWP works perfectly, baut iOS 10.3 on tablet dosent work.
    On the simulator works.
    What is the difference ? What do I have to that works also on the real hardware??????????

Sign In or Register to comment.