如何从 xamarin 表单中的绑定命令调用函数

How to call function from Binding Command in xamarin forms

我想知道如何从 xamarin 表单中的命令调用函数。我目前正在使用 https://github.com/robinmanuelthiel/swipecards 设置一堆可刷卡。我能够看到在元文件中有两个可绑定属性来处理左右滑动。

从元文件中截取:

public static BindableProperty SwipedRightCommandProperty;
public static BindableProperty SwipedLeftCommandProperty;

public Action<object> SwipedRight;
public Action<object> SwipedLeft;

public ICommand SwipedRightCommand { get; set; }
public ICommand SwipedLeftCommand { get; set; }

我可以在我的 XAML 内容页面中绑定 ItemSource,但我似乎无法绑定到向左滑动和向右滑动命令。

XAML:

<swipecards:CardStackView
            x:Name="CardStackView"
            ItemsSource="{Binding MyList}"
            SwipedLeftCommand="{Binding SwipedLeft}"
            SwipedRightCommand="{Binding SwipedRight}"
            CardMoveDistance="60"
            Grid.Column="0" Grid.ColumnSpan="3"
            Grid.Row="0">

C#(在我的视图模型中)

public void SwipedLeft(object obj)
{
    System.Diagnostics.Debug.WriteLine("Something Happened");
}

public void SwipedRight(object obj)
{
    System.Diagnostics.Debug.WriteLine("Something Happened");
}

有人知道如何正确绑定命令以便正确触发我的功能吗?

绑定到经典 Xamarin 按钮控件的命令如下所示:

XAML:

<Button Command="{Binding SwipeLeftCommand}" CommandParameter="{Binding SwipeLeftCommandParam}" />

视图模型:

public ICommand SwipedRightCommand { get; } //getter is sufficient


public YourViewModel()
{
    //plain code (often comes with bad maintainability)
    SwipeLeftCommand = new Command(() => { /* code */ });

    //simple method attachment (with command parameter)
    SwipeLeftCommand = new Command((commandParameter) => SwipedLeft(commandParameter));

    //simple method + async/await call
    SwipeLeftCommand = new Command(async (commandParameter) => await SwipedLeft(commandParameter));
}