Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

I am trying to implement a ICommand on a tableselection in C#/MAUI, but it seems that I never enter the command when debugging

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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!

>Solution :

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;
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading