Home > Enterprise >  I am trying to implement a ICommand on a tableselection in C#/MAUI, but it seems that I never enter
I am trying to implement a ICommand on a tableselection in C#/MAUI, but it seems that I never enter

Time:01-09

I am sorry or asking this question, I know there are similar questions in this community that I tried that did not ix my problem. Essentially I have a view, called MainPage.xaml, which contains this code:

   <ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:local="clr-namespace:VideoDemos.Views"
             x:Class="VideoDemos.Views.MainPage"
             Title="Home Page">
...
    <TableView Intent="Settings">
                <TableRoot>
                    <TableSection>
                        <ImageCell Text="Add new shot"
                          Detail="Add a new shot, the starting shot is a serve"
                          ImageSource="Resources/Images/add_icon_3.png"
                          Command="{Binding AddNewShotComand}"
                          />
                    </TableSection>
            </TableView>
          
...
    </ContentPage>

    public ICommand AddNewShotComand { set; get; }
 public MainPage()
    {
        InitializeComponent();
        MyInit();

        AddNewShotComand = new Command(AddNewShotCmd);
    }
 public void AddNewShotCmd()
    {
       var a = 5;
    }

I have var a = 5 just as a debug point, however, I never hit this point.

I've tried to iterate the steps o this link rom microsot I am very conused, I've gone step by step ollowing this link rom microsot https://learn.microsoft.com/en-us/dotnet/maui/fundamentals/data-binding/commanding?view=net-maui-7.0

I've tried to change the tableview intent to the our parameters, as a previous stackoverlow seemed to suggest that overlap may have ben the problem, but this did not ix anything or me.

I've tried to copy/paste directly rom the link to no avail.

I don't really know what the problem might be.

Any help would be great!

CodePudding user response:

bindings don't work if you don't define a BindingContext. In your case, since you are binding to properties in your code behind and not a separate VM, you would do this

public MainPage()
{
    InitializeComponent();
    MyInit();

    AddNewShotComand = new Command(AddNewShotCmd);

    BindingContext = this;
}
  • Related