Reputation: 1664
So I have some popup menu button in some activities. The problem is that in every activity I have to use the same code to initialize popup menu over and over again...
The code for initialization looks like this:
var button = FindViewById(Resource.Id.moreButton);
button.Click += (s, arg) =>
{
PopupMenu menu = new PopupMenu(this, button);
menu.Inflate(Resource.Menu.PopupMenu);
menu.Show();
menu.MenuItemClick += (s1, arg1) =>
{
switch (arg1.Item.TitleFormatted.ToString())
{
case "Profile":
StartActivity(typeof(ProfileView));
break;
case "Prices":
StartActivity(typeof(PricesView));
break;
case "Terms":
StartActivity(typeof(TermsView));
break;
case "Privacy":
StartActivity(typeof(PrivacyView));
break;
}
};
};
So in every activity that has popup menu button, I have to use this code to make button work.... How could I share this code part through all the activities?
Upvotes: 0
Views: 167
Reputation: 1961
I would create your own class that extends 'PopupMenu' and then add a method to initialize everything and display it.
public class MyPopupMenu : PopupMenu
{
//May need to add constructors
public void display()
{
this.Inflate(Resource.Menu.PopupMenu);
this.Show();
//May need to change EventArgs to a valid subclass of EventArgs
this.MenuItemClicked += (object sender, EventArgs arg1) =>
{
switch (arg1.Item.TitleFormatted.ToString())
{
case "Profile":
break;
case "Prices":
break;
case "Terms":
break;
case "Privacy":
break;
}
}
}
}
Then you can shorten your code on each page to:
var button = FindViewById(Resource.Id.moreButton);
button.Click += (s, arg) => {
PopupMenu menu = new PopupMenu(this, button);
menu.display();
};
Quick disclaimer, I hadn't had a chance to test this. Just my ideas.
Upvotes: 1