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评论129阅读模式
英文:

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

问题

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

  1. public ICommand AddNewShotComand { set; get; }
  2. public MainPage()
  3. {
  4. InitializeComponent();
  5. MyInit();
  6. AddNewShotComand = new Command(AddNewShotCmd);
  7. }
  8. public void AddNewShotCmd()
  9. {
  10. var a = 5;
  11. }

请注意,你提供的代码中存在一些拼写错误,"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:

  1. <ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
  2. xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
  3. xmlns:local="clr-namespace:VideoDemos.Views"
  4. x:Class="VideoDemos.Views.MainPage"
  5. Title="Home Page">
  6. ...
  7. <TableView Intent="Settings">
  8. <TableRoot>
  9. <TableSection>
  10. <ImageCell Text="Add new shot"
  11. Detail="Add a new shot, the starting shot is a serve"
  12. ImageSource="Resources/Images/add_icon_3.png"
  13. Command="{Binding AddNewShotComand}"
  14. />
  15. </TableSection>
  16. </TableView>
  17. ...
  18. </ContentPage>
  19. public ICommand AddNewShotComand { set; get; }
  20. public MainPage()
  21. {
  22. InitializeComponent();
  23. MyInit();
  24. AddNewShotComand = new Command(AddNewShotCmd);
  25. }
  26. public void AddNewShotCmd()
  27. {
  28. var a = 5;
  29. }

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,您可以这样做:

  1. public MainPage()
  2. {
  3. InitializeComponent();
  4. MyInit();
  5. AddNewShotComand = new Command(AddNewShotCmd);
  6. BindingContext = this;
  7. }
英文:

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

  1. public MainPage()
  2. {
  3. InitializeComponent();
  4. MyInit();
  5. AddNewShotComand = new Command(AddNewShotCmd);
  6. BindingContext = this;
  7. }

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

发表评论

匿名网友

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

确定