我想知道如何从xamarin表单中的命令调用函数。我目前正在使用https://github.com/robinmanuelthiel/swipecards来设置一叠可刷卡的卡片。我能够看到在元文件中有两个可绑定的属性来处理左右滑动。
从Meta文件中剪切:
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");
}
任何人都知道如何正确绑定命令,以便正确触发我的功能?
答案 0 :(得分:3)
绑定命令到经典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));
}