按钮上的 Xamarin 触发器未在 FreshMvvm / SwipeView 上下文中触发

问题描述

几乎相同的设置但没有 swipeview 上下文工作正常。 触发器被触发,我做“我的事”。

见下面的xaml代码+PageModel.cs代码

<CollectionView.ItemTemplate>
<DataTemplate>
    <swipeview BackgroundColor="LightYellow">
        <swipeview.RightItems>
            <SwipeItems Mode="Execute" SwipeBehaviorOnInvoked="Remainopen">
                <SwipeItemView>
                    <StackLayout Orientation="Vertical" WidthRequest="60" BackgroundColor="LightGray" Padding="2,5,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);
    }
}
    

ExecuteUpdateCommand 函数包含要执行的实际代码。 永远不会访问 AssignCommand 函数

解决方法

根据你的描述,你需要使用Xamarin.Forms Relative Bindings来绑定Button命令的命令,我做了一个示例,在Collectionview中使用SwipView,然后删除当前项。

<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>

您需要命名 ContentPage 的名称

<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",new swipmodel(){Name="barry",new swipmodel(){Name="cole",new swipmodel(){Name="leon",new swipmodel(){Name="leo",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; }
}

截图:

enter image description here

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...