问题描述
几乎相同的设置但没有 swipeview
上下文工作正常。
触发器被触发,我做“我的事”。
<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; }
}
截图: