如何解决如何使用 Xamarin Forms 中的 SwipeView 从 CollectionView 中删除项目?
我目前正在制作一个笔记应用。我是新手,请原谅我缺乏知识。 我正在使用默认弹出菜单模板,并进行了更改以满足我的需要。我在我的 CollectionView 中使用了 swipeview,因此当您在“注释”上滑动时,该项目将在执行时删除。
我可以进行滑动,但滑动后无法删除该项目。
这是我的 ItemsPage.xaml
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="xamarinMobileTest.Views.ItemsPage"
Title="{Binding Title}"
xmlns:local="clr-namespace:xamarinMobileTest.viewmodels"
xmlns:model="clr-namespace:xamarinMobileTest.Models"
x:Name="browseItemsPage">
<ContentPage.toolbaritems>
<ToolbarItem Text="Add Note" Command="{Binding Additemcommand}" />
</ContentPage.toolbaritems>
<ContentPage.BindingContext>
<local:Itemsviewmodel/>
</ContentPage.BindingContext>
<RefreshView x:DataType="local:Itemsviewmodel" Command="{Binding LoadItemsCommand}" IsRefreshing="{Binding IsBusy,Mode=TwoWay}">
<CollectionView x:Name="ItemsListView"
ItemsSource="{Binding Items}"
SelectionMode="None">
<CollectionView.ItemTemplate>
<DataTemplate>
<swipeview>
<swipeview.RightItems>
<SwipeItems Mode="Execute">
<SwipeItem Text="Delete"
BackgroundColor="Red"
Command="{Binding Deleteitemcommand}"
CommandParameter="{Binding .}"/>
</SwipeItems>
</swipeview.RightItems>
<StackLayout Padding="10" x:DataType="model:Item">
<Label Text="{Binding Text}"
LineBreakMode="Nowrap"
Style="{DynamicResource ListItemTextStyle}"
FontSize="16"
FontAttributes="Bold"
TextColor="Black"/>
<Label Text="{Binding Description}"
LineBreakMode="Nowrap"
Style="{DynamicResource ListItemDetailTextStyle}"
FontSize="13"
TextColor="Black"/>
<Label Text="{Binding DueDate}"
Style="{DynamicResource ListItemDetailTextStyle}"
FontSize="13"
TextColor="Black"/>
<StackLayout.GestureRecognizers>
<TapGestureRecognizer
NumberOfTapsrequired="1"
Command="{Binding Source={RelativeSource AncestorType={x:Type local:Itemsviewmodel}},Path=ItemTapped}"
CommandParameter="{Binding .}">
</TapGestureRecognizer>
</StackLayout.GestureRecognizers>
</StackLayout>
</swipeview>
</DataTemplate>
</CollectionView.ItemTemplate>
</CollectionView>
</RefreshView>
</ContentPage>
Itemsviewmodel:
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Diagnostics;
using System.Linq;
using System.Threading.Tasks;
using Xamarin.Forms;
using xamarinMobileTest.Models;
using xamarinMobileTest.Views;
namespace xamarinMobileTest.viewmodels
{
public class Itemsviewmodel : Baseviewmodel
{
private Item _selectedItem;
public ObservableCollection<Item> Items { get; }
public Command LoadItemsCommand { get; }
public Command Additemcommand { get; }
public Command<Item> ItemTapped { get; }
public Command<Item> Deleteitemcommand { get; }
public Itemsviewmodel()
{
Title = "Notes";
Items = new ObservableCollection<Item>();
LoadItemsCommand = new Command(async () => await ExecuteLoadItemsCommand());
ItemTapped = new Command<Item>(OnItemSelected);
Additemcommand = new Command(OnAddItem);
Deleteitemcommand = new Command<Item>(OnDeleteItem);
}
async Task ExecuteLoadItemsCommand()
{
IsBusy = true;
try
{
Items.Clear();
var items = await DataStore.GetItemsAsync(true);
foreach (var item in items)
{
Items.Add(item);
}
}
catch (Exception ex)
{
Debug.WriteLine(ex);
}
finally
{
IsBusy = false;
}
}
public void OnAppearing()
{
IsBusy = true;
SelectedItem = null;
}
public Item SelectedItem
{
get => _selectedItem;
set
{
SetProperty(ref _selectedItem,value);
OnItemSelected(value);
}
}
private async void OnAddItem(object obj)
{
await Shell.Current.GoToAsync(nameof(NewItemPage));
}
async void OnItemSelected(Item item)
{
if (item == null)
return;
// This will push the ItemDetailPage onto the navigation stack
await Shell.Current.GoToAsync($"{nameof(ItemDetailPage)}?{nameof(ItemDetailviewmodel.ItemId)}={item.Id}");
}
private void OnDeleteItem(Item item)
{
Items.Remove(item);
}
}
}
如何在 observable 集合中通过滑动删除一个 Item,以便在我删除该 Item 时它会自动看到该项目已被删除?
Items.Remove(item);
似乎不起作用(没有错误,只是没有从 CollectionView 中删除项目)这是为什么?
await DataStore.DeleteItemAsync(item);
也有同样的问题,即滑动项目时什么也没有发生。
感谢任何帮助。
解决方法
把你的参数改成这个
CommandParameter="{Binding Source={RelativeSource Self},Path=BindingContext}"
,
您在 OnDeleteItem
中的代码是无意义的。它应该看起来像这样
private void OnDeleteItem(Item item)
{
Items.Remove(item);
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。