Reputation: 3071
I was trying to add a custom event to my custom control in Xamarin.Forms. Please take a look at the code below:
public delegate void ImageSelectedHandler(object sender, EventArgs e);
public static event ImageSelectedHandler OnImageSelected;
private void OnImageBtnTapped(object sender, EventArgs e)
{
if (OnImageSelected != null)
{
OnImageSelected(sender,e);
}
}
In the page which is using the control:
SelectMultipleBasePage<ListItems>.OnImageSelected += ListPage_OnImageSelected;
void ListPage_OnImageSelected(object sender, EventArgs e)
{
//code here
}
I could access the event by using the above code. But I would like to use the control on different pages. On different pages different OnImageSelected
even will behave differently. And hence I would like to have something like this:
SelectMultipleBasePage<ListItems> multiPage = new SelectMultipleBasePage<ListItems>(items);
multiPage.OnImageSelected += ListPage_OnImageSelected;
But when I do that I get error:
Cannot be accessed with an instance reference; qualify it with a type name instead
What am I doing wrong in accessing the event?
Upvotes: 1
Views: 3248
Reputation: 14750
Just remove the static
.
public event ImageSelectedHandler OnImageSelected;
Then you can call
SelectMultipleBasePage<ListItems> multiPage = new SelectMultipleBasePage<ListItems>(items);
multiPage.OnImageSelected += ListPage_OnImageSelected;
Of course you have to change the static call, too.
Upvotes: 1