微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

如何解决按钮上的 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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?