【问题标题】:I am trying to implement a ICommand on a tableselection in C#/MAUI, but it seems that I never enter the command when debugging我正在尝试在 C#/MAUI 中的表选择上实现 ICommand,但似乎我在调试时从未输入命令
【发布时间】:2023-01-09 03:34:54
【问题描述】:

很抱歉或问这个问题,我知道我尝试过的这个社区中有类似的问题并没有解决我的问题。基本上我有一个名为 MainPage.xaml 的视图,其中包含以下代码:

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

我有 var a = 5 只是作为调试点,但是,我从来没有达到这一点。

我已经尝试迭代来自微软的这个链接的步骤我很困惑,我已经一步一步地跟随来自微软https://learn.microsoft.com/en-us/dotnet/maui/fundamentals/data-binding/commanding?view=net-maui-7.0的这个链接

我试图将 tableview 意图更改为我们的参数,因为之前的 stackoverlow 似乎表明重叠可能是问题所在,但这并没有解决任何问题或我。

我试图直接从链接复制/粘贴无济于事。

我真的不知道可能是什么问题。

任何帮助都会很棒!

【问题讨论】:

  • 你在哪里设置 BindingContext?
  • 所以我一直在阅读有关 bindingcontext 或几天的内容,但我不知所措或如何根据我的情况进行设置。

标签: c# maui


【解决方案1】:

如果您不定义 BindingContext,则绑定不起作用。在您的情况下,由于您绑定到代码中的属性而不是单独的 VM,因此您可以这样做

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

    AddNewShotComand = new Command(AddNewShotCmd);

    BindingContext = this;
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-08-28
    • 1970-01-01
    • 2022-08-19
    • 2021-11-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多