I am wondering how I can call a function from a command in xamarin forms. I am currently using https://github.com/robinmanuelthiel/swipecards to set up a stack of swipe-able cards. I am able to see that in the meta file there are two bindable properties to handle left and right swipes.
Snip from the Meta file:
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; }
I am able to bind the ItemSource in my XAML content page but I can't seem to bind to the swipe left and swipe right commands.
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# (in my view model)
public void SwipedLeft(object obj)
{
System.Diagnostics.Debug.WriteLine("Something Happened");
}
public void SwipedRight(object obj)
{
System.Diagnostics.Debug.WriteLine("Something Happened");
}
anyone know how to properly bind to the commands so my function is properly triggered?
Binding Command to classic Xamarin Button control looks like this:
XAML:
<Button Command="{Binding SwipeLeftCommand}" CommandParameter="{Binding SwipeLeftCommandParam}" />
ViewModel:
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));
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.