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

huangapple go评论71阅读模式
英文:

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

问题

抱歉,我只会翻译代码部分:

public ICommand AddNewShotComand { set; get; }

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

    AddNewShotComand = new Command(AddNewShotCmd);
}

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

请注意,你提供的代码中存在一些拼写错误,"AddNewShotComand" 应该是 "AddNewShotCommand",以及 "MyInit()" 方法没有在提供的代码中定义。这些错误可能会影响代码的执行。

英文:

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!

答案1

得分: 0

绑定不起作用,如果您没有定义BindingContext。在您的情况下,因为您要绑定的是代码后台的属性,而不是单独的 VM,您可以这样做:

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

    AddNewShotComand = new Command(AddNewShotCmd);

    BindingContext = this;
}
英文:

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;
}

huangapple
  • 本文由 发表于 2023年1月9日 03:01:07
  • 转载请务必保留本文链接:https://go.coder-hub.com/75050518.html
  • c#
  • maui

List: 如何为某个索引元素应用“where”条件? go 55
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定

  • 开发者交流平台

    本页二维码