I’m trying to setup deleting functionality for my term tracking app, but I’m having some issue where when I swipe, the swipe view closes, but no deleting happens. My breakpoint on my DeleteTermAsync method is not even being hit so I’m think there’s some issue with the Command=”{}” part of the code that is making the DeleteTermAsyncCommand not be hit/found. If anyone knows how I can modify it to make sure it’s hit, I’d really appreciate it!
Flyout terms swipe delete
<Shell
x:Class="TermTracker.AppShell"
xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:local="clr-namespace:TermTracker"
xmlns:view="clr-namespace:TermTracker.View"
xmlns:model="clr-namespace:TermTracker.Model"
xmlns:viewModel="clr-namespace:TermTracker.ViewModel"
Shell.FlyoutBehavior="Flyout"
Title="TermTracker">
<Shell.FlyoutContentTemplate>
<DataTemplate>
<CollectionView ItemsSource="{Binding Terms}" ItemSizingStrategy="MeasureAllItems">
<CollectionView.ItemTemplate>
<DataTemplate>
<SwipeView>
<SwipeView.RightItems>
<SwipeItems>
<SwipeItem Text="Delete"
BackgroundColor="Red"
CommandParameter="{Binding .}"
Command="{Binding Source={RelativeSource AncestorType={x:Type viewModel:TermViewModel}}, Path=DeleteTermAsyncCommand}" />
</SwipeItems>
</SwipeView.RightItems>
<Grid Padding="10">
<VerticalStackLayout>
<Label Text="{Binding Name}" FontSize="16" FontAttributes="Bold"/>
<Label Text="{Binding StartDate, StringFormat='Start: {0:MM/dd/yyyy}'}" FontSize="14"/>
<Label Text="{Binding EndDate, StringFormat='End: {0:MM/dd/yyyy}'}" FontSize="14"/>
</VerticalStackLayout>
</Grid>
</SwipeView>
</DataTemplate>
</CollectionView.ItemTemplate>
</CollectionView>
</DataTemplate>
</Shell.FlyoutContentTemplate>
<ShellContent Title="Home"
ContentTemplate="{DataTemplate view:MainPage}"
Route="MainPage"/>
</Shell>
using CommunityToolkit.Mvvm.Input;
using SQLite;
using TermTracker.Model;
using TermTracker.View;
using TermTracker.ViewModel;
namespace TermTracker
{
public partial class AppShell : Shell
{
public AppShell(TermViewModel viewModel)
{
InitializeComponent();
BindingContext = viewModel;
Routing.RegisterRoute(nameof(AddTermPage), typeof(AddTermPage));
}
}
}
using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using CommunityToolkit.Mvvm.Messaging;
using System.Collections.ObjectModel;
using System.Threading.Tasks;
using TermTracker.Model;
using static TermTracker.ViewModel.AddTermPageViewModel;
namespace TermTracker.ViewModel
{
public partial class TermViewModel : ObservableObject
{
[ObservableProperty]
private ObservableCollection<Term>? terms;
public TermViewModel()
{
LoadTermsAsync();
// Subscribe to the TermAddedMessage
WeakReferenceMessenger.Default.Register<TermAddedMessage>(this, (r, m) =>
{
LoadTermsAsync();
});
}
async void LoadTermsAsync()
{
var termList = await TermRepository.GetTerms();
Terms = new ObservableCollection<Term>(termList);
}
[RelayCommand]
async Task DeleteTermAsync(Term term)
{
if (term == null)
return;
await TermRepository.DeleteTerm(term);
// Reload terms
LoadTermsAsync();
}
}
}
I tried changing the Ancestory type, setting the path to the DeleteTermAsync directly, and adding breakpoints.
Lorenzo Ortiz is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.