I am currently assigning an Event Handler to an Event, but I did need to pass one other argument to know what produced this event. It is in a function to set up a XAML file. I did something like that :
chart[i].PaintSurface += (sender, e) => OnCanvasViewPaintSurface(sender, e, i);
But this same line is throwing a Null Reference Exception.
I looked down on the web to know why, and most of the times it's because the object isn't initialised, but here it is.
Here is the function in which occurs this problem. The array chart is a global variable.
private void CreateUIElements()
{
int MAXCHANNELS = (int)App.UserPrefs[UserPreferenceReference.TotalChannels];
ChannelLabels = new Label[MAXCHANNELS];
Plotlayouts = new AbsoluteLayout[MAXCHANNELS];
for (int i = 0; i < MAXCHANNELS; i++)
{
MainGrid.RowDefinitions.Add(new RowDefinition { Height = 15 });
if (i != MAXCHANNELS - 1)
{
MainGrid.RowDefinitions.Add(new RowDefinition { Height = 75 });
}
else
{
MainGrid.RowDefinitions.Add(new RowDefinition { Height = 100 });
}
ChannelLabels[i] = new Label()
{
Text = "Channel" + (i + 1),
TextColor = Color.Gray
};
MainGrid.Children.Add(ChannelLabels[i], 0, 2 * i);
Plotlayouts[i] = new AbsoluteLayout();
chart[i] = new SKCanvasView
{
BackgroundColor = Color.White,
VerticalOptions = LayoutOptions.FillAndExpand, // stretch the view
HorizontalOptions = LayoutOptions.FillAndExpand, // stretch the view
// WidthRequest = 100, // specify a size
// HeightRequest = 100, // specify a size
};
chart[i].PaintSurface += (sender, e) => OnCanvasViewPaintSurface(sender, e, i);
Plotlayouts[i].Children.Add(chart[i], new Rectangle(0, 0, 1, 1), AbsoluteLayoutFlags.All);
MainGrid.Children.Add(Plotlayouts[i], 0, 2 * i + 1);
}
MainGrid.RowDefinitions.Add(new RowDefinition { Height = 50 });
var buttonLayout = new StackLayout
{
Orientation = StackOrientation.Horizontal,
HorizontalOptions = LayoutOptions.Center
};
MainGrid.Children.Add(buttonLayout, 0, MAXCHANNELS);
}
Thank you very much for your help.