TableView - custom ViewCell - Command property

DanielLDanielL PLInsider ✭✭✭✭
    class MainMenuItem : ViewCell
    {
        public MainMenuItem(string text, string icon)
        {
            View = new StackLayout() {
                Orientation = StackOrientation.Horizontal,
                Spacing = 20,
                Padding = 20,
                Children = {
                    new Image() {
                        Source = ImageSource.FromFile(icon),
                        VerticalOptions = LayoutOptions.Center,
                    },
                    new Label() {
                        Text = text,
                        VerticalOptions = LayoutOptions.Center,
                        Font = Font.BoldSystemFontOfSize(25)
                    }
                }
            };
        }
    }

        var menu = new TableView {
            Intent = TableIntent.Menu,
            Root = new TableRoot {
                new TableSection("Menu")
                { 
                    new MainMenuItem("Item1", "test.png")
                    {
                        Command = new Command(async () => {  }) // DOESN'T WORK - compiler error ??? 
                    },

                    new ImageCell()
                    {
                        ImageSource = ImageSource.FromFile("test.png"),
                        Text = "Item1",
                        Detail = "Details",
                        Command = new Command(async () => {  })  // OK !!!
                    }
                }
            }
        };

Can You explain why it won't compile? What is ViewCell.Tapped Event? How is it different from Command?

Posts

  • rene_ruppertrene_ruppert DEXamarin Team, University, XamUProfessors Xamurai

    Your MainMenuItem is a ViewCell. ViewVell is a subclass of Cell.
    ImageCell is a subclass of TextCell (which is a subclass of Cell).

    TextCell defines a Command property which allows binding a command to the Tapped event.
    ViewCell does not expose such a Command property.

    In other words: Command is an ICommand and Tapped is an event.

  • BobisbackBobisback USUniversity ✭✭
    edited July 2015

    What would the code look like to bind a command to a tapped event in a custom view cell?

    Is there any chance we could get the implamentation in the Textcell for the command property?

  • CarlosSilva.6280CarlosSilva.6280 USMember ✭✭

    Well, i know this is quit an old entry but why not!! I've just spent a few hours (newbee pains) trying to solve this until i got pragmatic and peek at xamarin code (see github).

    My prefered sollution is..... create a custom VewCell with a Command.

        public class ViewCellEx : ViewCell
        {
            public static readonly BindableProperty CommandProperty =
                BindableProperty.Create("Command", typeof(ICommand), typeof(ViewCellEx), null, propertyChanged: OnCommandPropertyChanged);
            public static readonly BindableProperty CommandParameterProperty =
                BindableProperty.Create("CommandParameter", typeof(object), typeof(ViewCellEx), null, propertyChanged: OnCommandParameterPropertyChanged);
            public ICommand Command { get => (ICommand)GetValue(CommandProperty); set => SetValue(CommandProperty, value); }
            public object CommandParameter { get => (ICommand)GetValue(CommandParameterProperty); set => SetValue(CommandParameterProperty, value); }
            private static void OnCommandParameterPropertyChanged( BindableObject bindable, object oldValue, object newValue )
            {
            //Stuff to handle changes, not really required
            }
            private static void OnCommandPropertyChanged( BindableObject bindable, object oldValue, object newValue )
            {
            //More stuff to handle changes
            }
    
            protected override void OnTapped()
            {
                if ( Command != null ) {
                    var parameter = CommandParameter;
                    Command.Execute( parameter );
                }
            }
        }
    

    Of course you can do better, just go and have a look at Xamarin's code

Sign In or Register to comment.