MVVMCross not binding BarButton click event on device

christopherpolskachristopherpolska PLUniversity ✭✭✭
edited February 2017 in Cross Platform with Xamarin

Hi,

I have a button of type UIBarButtonItem, I wired up the click event to the MvxCommand, on the simulator the button does work (it opens a new window), however when I deploy the app to the iPhone, then the button is not working - not displaying the new window.
The application output says: "Failed to create target binding for binding Clicked for NewPersonCommand"

Code:

public partial class PersonListView : MvxViewController<PersonListViewModel> 
{
    protected PersonListViewModel PersonListViewModel => ViewModel as PersonListViewModel;
    UIBarButtonItem button = new UIBarButtonItem();

    public PersonListView() : base("PersonListView", null)
    {
        (...)
        NavigationItem.RightBarButtonItem = button;
    }

    protected void CreateBindings()
    {
        var set = this.CreateBindingSet<PersonListView, PersonListViewModel>();
        (...)
        this.AddBindings(
            new Dictionary<object, string>()
            {
                {button, "Clicked NewPersonCommand"}
            }
        );
            set.Apply();
        }
(...)

Best Answer

  • christopherpolskachristopherpolska PL ✭✭✭
    edited February 2017 Accepted Answer

    The solution which worked for me is to add a new file to iOS project called "LinkerPleaseInclude" and add this code there:

        public class LinkerPleaseInclude
        {
            public void Include(UIBarButtonItem button)
            {
                button.Clicked += (sender, e) => { button.Title = button.Title + ""; };
            }
        }
    

    Unfortunately I don't know how and why this works at this moment.

Answers

  • christopherpolskachristopherpolska PLUniversity ✭✭✭

    Using this code to bind this button:
    set.Bind(NavigationItem).For(s => s.RightBarButtonItem).To(vm => vm.NewPersonCommand);

    Gave me a different error:
    MvxBind: Error: 145,30 Problem seen during binding execution for binding RightBarButtonItem for NewPersonCommand - problem ArgumentException: Object of type 'MvvmCross.Core.ViewModels.MvxCommand' cannot be converted to type 'UIKit.UIBarButtonItem'.

  • christopherpolskachristopherpolska PLUniversity ✭✭✭
    edited February 2017 Accepted Answer

    The solution which worked for me is to add a new file to iOS project called "LinkerPleaseInclude" and add this code there:

        public class LinkerPleaseInclude
        {
            public void Include(UIBarButtonItem button)
            {
                button.Clicked += (sender, e) => { button.Title = button.Title + ""; };
            }
        }
    

    Unfortunately I don't know how and why this works at this moment.

  • RamohanRamohan Member ✭✭

    Hi @christopherpolska

    NavigationItem.RightBarButtonItem action working in Simulator but in device it's not working.
    Can Pls help me on this.

    Thanks & Regards
    Ramohan Reddy

Sign In or Register to comment.