Home > other >  How to delete data in a .NET MAUI list
How to delete data in a .NET MAUI list

Time:01-12

I am currently doing a project with the MVVM method in NET MAUI to add, modify and delete drivers. I have a template that contains the name, first name and number of points of the driver. Then I have two views each with a model view: - One that represents the list of my drivers with the possibility to add a driver, to select a driver from the list by going to another page (PageListPilotViewModel). - And another one which represents the selected driver in another page to be able to modify its data and the possibility of removing it. (ProfilePilotViewModel) At the moment I can select, add the driver and modify the driver in the other page. But I can't delete the driver in the profile page. Here is what I have done so far:

-> Models : Pilote Model

    public class PiloteModel : INotifyPropertyChanged
    {

        private string _nom;
            public string Nom
            {
                get { return _nom; }
                set { _nom = value; OnPropertyChanged(); }
            }

            private string _prenom;
            public string Prenom
            {
                get { return _prenom; }
                set { _prenom = value; OnPropertyChanged(); }
            }

            private int _points;
            public int Points
            {
                get { return _points; }
                set { _points = value; OnPropertyChanged(); }
            }

            public event PropertyChangedEventHandler PropertyChanged;

            protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
            {
                PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
            }
    }

-> View : ProfilPilotePage

        <vm:PageListPiloteViewModel></vm:PageListPiloteViewModel>
    </ContentPage.BindingContext>
    <VerticalStackLayout>
        <StackLayout>
            <Entry Text="{Binding Pilote.Nom, Mode=TwoWay}" Placeholder="{Binding Nom}"></Entry>
        <Entry Text="{Binding Pilote.Prenom}" Placeholder="{Binding Pilote.Prenom}"></Entry>
        <Entry Text="{Binding Pilote.Points}" Placeholder="{Binding Pilote.Points}"></Entry>

            <Button Command="{Binding OnsupprimerPiloteCommand}">
            </Button>
        </StackLayout>

-> code behind the profilePilotPage view

public partial class ProfilPilotePage : ContentPage
{
    private PageListPiloteViewModel _viewModel;
    public ProfilPilotePage(PageListPiloteViewModel viewModel)
    {
        InitializeComponent();

        _viewModel = viewModel;
        _viewModel.SupprimerPiloteClicked  = OnSupprimerPiloteClicked;

        BindingContext = _viewModel;


    }

    private void OnSupprimerPiloteClicked(object sender, PiloteModel e)
    {
        _viewModel.ListePilotes.Remove(e);
    }

-> model views : PageListPilotViewModel , to be able to delete also the driver in the list

public ICommand OnsupprimerPiloteCommand { get; set; }


        public PageListPiloteViewModel()
        {
            ValiderCommand = new Command(AjouterPilote);
            OnsupprimerPiloteCommand = new Command(OnSupprimerPiloteClicked);
            SelectedPilote = new PiloteModel();
            ListePilotes = new ObservableCollection<Models.PiloteModel>();





           
            ListePilotes.Add(new Models.PiloteModel { Nom = "Fabio", Prenom = "Quartaro", Points = 215 });
    }

        private void OnSupprimerPiloteClicked()
        {
            SupprimerPiloteClicked?.Invoke(this, SelectedPilote);
        }

->code behind the PageListPiloteView: with the error I encounter on the last : await Navigation.PushAsync(new ProfilePilotPage{ BindingContext = viewModel }) : CS7036 Error None of the specified arguments match the 'viewModel' mandatory parameter of 'ProfilePilotPage.ProfilePilotPage(PageListPilotViewModel)'

private async void SelectionnerPilote(object sender, SelectionChangedEventArgs e)
    {
        PiloteModel selectedPilote = (PiloteModel)((CollectionView)sender).SelectedItem;
        ProfilPiloteViewModel viewModel = new ProfilPiloteViewModel();
        viewModel.Pilote = selectedPilote;
        await Navigation.PushAsync(new ProfilPilotePage{ BindingContext = viewModel });
    }

}

Do you have any idea how to make the specified arguments mandatory please ?

CodePudding user response:

You've mixed up constructor and initializer.

This line

await Navigation.PushAsync(new ProfilPilotePage{ BindingContext = viewModel });

should be

await Navigation.PushAsync(new ProfilPilotePage(viewModel));

The reason for this is that you're defining an argument in the signature of the ProfilPilotePage's constructor:

public ProfilPilotePage(PageListPiloteViewModel viewModel)
{
    //...
}

Therefore, you must pass the ViewModel argument.

CodePudding user response:

At first, you can try to use the ewerspej's solution or only add a default construction method without any parameter into the ProfilPilotePage to fix the error caused by await Navigation.PushAsync(new ProfilePilotPage{ BindingContext = viewModel }) . Such as:

public partial class ProfilPilotePage : ContentPage
{
    private PageListPiloteViewModel _viewModel;
    public ProfilPilotePage()
    {
        InitializeComponent();
    }

    public ProfilPilotePage(PageListPiloteViewModel viewModel)
    {
        InitializeComponent();
        _viewModel = viewModel;
        _viewModel.SupprimerPiloteClicked  = OnSupprimerPiloteClicked;
        BindingContext = _viewModel;
    }
}

And then I saw you used both the mvvm and the code behind. You can remove the OnSupprimerPiloteClicked(object sender, PiloteModel e) in the page.cs and change the OnSupprimerPiloteClicked() in the view model. Such as:

private void OnSupprimerPiloteClicked()
{
   ListePilotes.Remove(SelectedPilote);
}

Finally, I saw SelectedPilote = new PiloteModel(); in your viewmodel. Which item in the list did you want to delete? I thinl it should be the seleted item not a new PiloteModel().

  • Related