Xamarin trigger on button not firing in a FreshMvvm / SwipeView context

253 Views Asked by At

The almost identical setup but without the SwipeView context is working fine. The trigger gets fired and I do "my thing".

See the following xaml code + PageModel.cs code :

<CollectionView.ItemTemplate>
<DataTemplate>
    <SwipeView BackgroundColor="LightYellow">
        <SwipeView.RightItems>
            <SwipeItems Mode="Execute" SwipeBehaviorOnInvoked="RemainOpen">
                <SwipeItemView>
                    <StackLayout Orientation="Vertical" WidthRequest="60" BackgroundColor="LightGray" Padding="2,5,0,5" >
                        <Button x:Name="Btn_Assign" 
                                Text="{x:Static fa:FontAwesomeIcons.UserPlus}" FontSize="20" FontFamily="FAS" 
                                HorizontalOptions="Center" 
                                Command="{Binding AssignCommand}"/>
                        <Label Text="Assign" FontSize="Subtitle" HorizontalOptions="Center"/>
                    </StackLayout>
                </SwipeItemView>
            </SwipeItems>
        </SwipeView.RightItems>
    </SwipeView>
</DataTemplate>
</CollectionView.ItemTemplate>

PageModel.cs

public Command AssignCommand
{
    get
    {
        return new Command(ExecuteUpdateCommand);
    }
}
    

The ExecuteUpdateCommand function contains the actual code to be executed. The AssignCommand function is never accessed.

1

There are 1 best solutions below

6
Cherry Bu - MSFT On

From your description, you need to use Xamarin.Forms Relative Bindings to bind command for Button's Command, I do one sample that using SwipView in Collectionview, then delete current item.

<StackLayout>
        <CollectionView ItemsSource="{Binding models}" SelectedItem="{Binding selecteditem}">
            <CollectionView.ItemTemplate>
                <DataTemplate>
                    <StackLayout>
                        <SwipeView>
                            <SwipeView.LeftItems>
                                <SwipeItems>
                                    <SwipeItem
                                        BackgroundColor="LightPink"
                                        Command="{Binding Path=BindingContext.deletecommand, Source={x:Reference page1}}"
                                        CommandParameter="{Binding Name}"
                                        IconImageSource="delete.png"
                                        Text="Delete" />
                                </SwipeItems>
                            </SwipeView.LeftItems>
                            <StackLayout>
                                <Label Text="{Binding Name}" />
                                <Label Text="{Binding Age}" />
                            </StackLayout>
                        </SwipeView>

                    </StackLayout>
                </DataTemplate>
            </CollectionView.ItemTemplate>
        </CollectionView>
    </StackLayout>

You need to name ContentPage's Name

<ContentPage
x:Class="FormsSample.collectionviewsample.Page18"
xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Name="page1">

 public partial class Page18 : ContentPage
{
   
    public Page18()
    {
        InitializeComponent();
        this.BindingContext = new swipviewmodel();
       
    }      
}
public class swipviewmodel:ViewModelBase
{
    public ObservableCollection<swipmodel> models { get; set; }
    public ICommand deletecommand { get; set; }       
    public swipviewmodel()
    {
        models = new ObservableCollection<swipmodel>()
        {
            new swipmodel(){Name="cherry",Age=28},
            new swipmodel(){Name="wendy",Age=28},
            new swipmodel(){Name="barry",Age=28},
            new swipmodel(){Name="cole",Age=28},
            new swipmodel(){Name="leon",Age=28},
            new swipmodel(){Name="leo",Age=28},
            new swipmodel(){Name="stfan",Age=28}
        };
        deletecommand = new Command((obj)=> {
            if(obj!=null)
            {
                swipmodel selecteditem = models.FirstOrDefault(a => a.Name == (string)obj);
                models.Remove(selecteditem);
            }                            
        });
    }
   
}
public class swipmodel
{
    public string Name { get; set; }
    public int Age { get; set; }
}

The screenshot:

enter image description here