Home > database >  C# Xamarin Forms - Invoking an event always returns null
C# Xamarin Forms - Invoking an event always returns null

Time:11-16

Hi everyone and sorry for my bad english.

I am developing an app on Xamarin Forms and I have encountered a problem with the event invocation.

I have a ContentView with various labels and two buttons to edit and delete the object in question. As you see I've tried various methods for event invocation, but the EventHandler is always null when I invocate it.

InstrumentCard.xaml.cs code:

public partial class InstrumentCard : ContentView
    {
        public event EventHandler<InstrumentEventArgs> DeleteClicked;
        public event EventHandler<InstrumentEventArgs> EditClicked;

        private Instrument _i;

        public InstrumentCard(Instrument i)
        {
            InitializeComponent();

            _i = i;
            lblID.Text = i.ID.ToString();
            lblName.Text = i.Nome;
            lblQty.Text = $"Quantità: {i.Qty.ToString()}";
        }

        public Instrument GetInstrument()
        {
            return _i;
        }

        private void btnEdit_Clicked(object sender, EventArgs e)
        {
            RaiseEdit(new InstrumentEventArgs { Instrument = _i});
        }

        private void RaiseEdit(InstrumentEventArgs args)
        {
            EditClicked?.Invoke(this, args);
        }

        private void btnDelete_Clicked(object sender, EventArgs e)
        {
            DeleteClicked?.Invoke(this, new InstrumentEventArgs
            {
                Instrument = _i
            });
        }
    }

MainPage.xaml.cs code:

    public partial class MainPage : ContentPage
    {
        List<Instrument> instruments = new List<Instrument>();
        public MainPage()
        {
            InitializeComponent();
        }

        protected override async void OnAppearing()
        {
            base.OnAppearing();
            
            Reload();
        }

        public async void Reload()
        { 
            List<Instrument> instruments = await App.Database.GetInstrumentsAsync();

            lblCounter.Text = $"Ci sono {instruments.Count} strumenti";

            if (instruments.Count == 0)
            {
                stcNoData.IsVisible = true;
                stcData.IsVisible = false;
                stcDock.IsVisible = false;
            }
            else
            {
                stcNoData.IsVisible = false;
                stcData.IsVisible = true;
                stcDock.IsVisible = true;

                stcInstruments.Children.Clear();

                instruments.ForEach(instr =>
                {
                    InstrumentCard ic = new InstrumentCard(instr);
                    ic.DeleteClicked  = Ic_DeleteClicked;
                    ic.EditClicked  = Ic_EditClicked;

                    stcInstruments.Children.Add(new InstrumentCard(instr));
                });
            }
        }

        private void Ic_EditClicked(object sender, InstrumentEventArgs e)
        {
            Navigation.PushAsync(new AddInstrument(e.Instrument));
        }

        private void Ic_DeleteClicked(object sender, InstrumentEventArgs e)
        {
            stcInstruments.Children.Remove(sender as InstrumentCard);
            App.Database.DeleteInstrumentAsync(e.Instrument);
            Reload();
        }
}

When I debug the method... enter image description here

I don't know what is wrong. Thank you for your help

CodePudding user response:

The problem is here

instruments.ForEach(instr =>
{
  //here you create your instrument card
  InstrumentCard ic = new InstrumentCard(instr);
  ic.DeleteClicked  = Ic_DeleteClicked;
  ic.EditClicked  = Ic_EditClicked;
  //here you should use it but instead you create another one
  stcInstruments.Children.Add(new InstrumentCard(instr));
});

Just add your created instance where you are subscribed to events to the StackPanel (I am expecting that it is StackPanel)

stcInstruments.Children.Add(ic);
  • Related