C# Xamarin Forms - 始终调用事件 returns null

C# Xamarin Forms - Invoking an event always returns null

大家好,抱歉我的英语不好。

我正在 Xamarin Forms 上开发一个应用程序,我遇到了 event invocation 的问题。

我有一个带有各种标签的 ContentView 和两个用于编辑和删除相关对象的按钮。如您所见,我已经尝试了各种事件调用方法,但是当我调用它时 EventHandler 始终为 null。

InstrumentCard.xaml.cs代码:

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代码:

    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();
        }
}

当我调试方法时...

我不知道怎么了。 感谢您的帮助

问题就在这里

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));
});

只需将您创建的订阅事件的实例添加到 StackPanel(我希望它是 StackPanel

stcInstruments.Children.Add(ic);