簡體   English   中英

如何從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 Button控件如下所示:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM