Hooking Up Contextual Actions to Methods

The next step was to hook up the item contextual actions that I created in my previous post to the actual methods that would do the work when they were tapped.


I planned on putting the methods in the BuyPage and AddPage classes since all items would need to call them when their context actions were tapped

BuyPage Item Context Actions

  • +1 – ItemQuantityIncreased
  • Bought – ItemBought (set the item’s Buy property to false so it can be removed from the Buy list)

AddPage Item Context Actions/Methods

  • Buy – BuyItem (set the item’s Buy property to true so it can be added to the Buy list)
  • Remove – RemoveItem (remove the item entirely from the Add list)

The question was how to hook up each contextual action to these higher-level methods. At first I thought I would have to pass a reference to the BuyPage into each item when it was created or do some reflection and casting to call the individual “grandfather” methods from each item, but none of those seemed workable or elegant.

Then I discovered MessagingCenter, a lightweight publish-and-subscribe that comes built into Xamarin.Forms. It enabled me to pass messages between classes.

The first step was to add event handlers to each MenuItem’s Clicked event that “publish” a message.

// BuyCell MenuItem declarations
var boughtAction = new MenuItem { Text = "Bought", IsDestructive = true };
boughtAction.Clicked += (sender, e) => MessagingCenter.Send<BuyCell>(this, "BoughtItem");

var plusOneAction = new MenuItem { Text = "+1" };
plusOneAction.Clicked += (sender, e) => MessagingCenter.Send<BuyCell>(this, "AddOne");

// AddCell MenuItem declarations
var buyAction = new MenuItem { Text = "Buy" };
buyAction.Clicked += (sender, e) => MessagingCenter.Send<AddCell>(this, "BuyItem");

var removeAction = new MenuItem { Text = "Remove", IsDestructive = true };
removeAction.Clicked += (sender, e) => MessagingCenter.Send<AddCell>(this, "RemoveItem");

The next step was to subscribe to those messages in each page’s Constructor.

// in BuyPage constructor
MessagingCenter.Subscribe<BuyCell>(this, "BoughtItem", ItemBought);
MessagingCenter.Subscribe<BuyCell>(this, "AddOne", ItemQuantityIncreased);

// in AddPage constructor
MessagingCenter.Subscribe<AddCell>(this, "BuyItem", BuyItem);
MessagingCenter.Subscribe<AddCell>(this, "RemoveItem", RemoveItem);

When each message subscriber receives its particular message it calls the appropriate method.

// in BuyPage
internal void ItemBought(BuyCell item)
	DisplayAlert("Alert", item.Text + " bought.", "OK");

internal void ItemQuantityIncreased(BuyCell item)
	DisplayAlert("Alert", item.Text + " quantity increased by one", "OK");

// in AddPage
internal void BuyItem(AddCell item)
	DisplayAlert("Alert", item.Text + " added to Buy list", "OK");

internal void RemoveItem(AddCell item)
	DisplayAlert("Alert", item.Text + " removed from Add list", "OK");

Right now each method displays a custom dialog so that I know that the correct method is being called for each tap.





This entry was posted in Side Projects, Software Development and tagged , , , . Bookmark the permalink.