- Hands-On Mobile Development with .NET Core
- Can Bilgin
- 235字
- 2021-06-24 13:55:35
Event aggregator
An event aggregator (also known as publisher/subscriber or Pub-Sub) is a messaging pattern where senders of messages/events, called publishers, do not program the messages to be sent directly to a single/specific receiver, called subscribers, but, instead, categorize published messages into classes without knowledge of which subscribers, if any, there may be. Messages are then funneled through a so-called aggregator and delivered to the subscribers. This approach provides a complete decoupling between the publishers and subscribers while maintaining an effective messaging channel.
Event aggregator implementation within the Xamarin.Forms framework is done via MessagingCenter. MessagingCenter exposes a simple API that is composed of two methods for subscribers (that is, subscribe and unsubscribe) and one method for publishers (send).
We can demonstrate the application of the event aggregator pattern by creating a simple event sink for service communication or authentication issues in our application. In this event sink (which will be our subscriber), we can subscribe to error messages that are received from various view-models (given that they all implement the same base type) and alert the user with a friendly dialog:
MessagingCenter.Subscribe<BaseViewModel> (this, "ServiceError", (sender, arg) =>
{
// TODO: Handler the error
});
We would have the following in the event of an unhandled exception on our view-model:
public void Login()
{
try
{
//TODO: Login
Result = "Successfully Logged In!";
}
catch(Exception ex)
{
MessagingCenter.Send(this, "ServiceError", ex.Message);
}
}
- 腦血管病的防與治
- 陳衛(wèi)川中回醫(yī)臨證實(shí)錄方
- 腦卒中預(yù)防與控制
- 食品毒理學(xué)
- CT診斷報(bào)告書寫技巧
- 圖說災(zāi)難逃生自救叢書:海嘯
- 何以為人:試管嬰兒技術(shù)的起源與發(fā)展
- 骨骼肌靜力性負(fù)荷所致?lián)p傷機(jī)理的研究
- 硅橡膠修補(bǔ)關(guān)節(jié)軟骨的實(shí)驗(yàn)研究
- 腫瘤揭秘
- 悅讀中醫(yī):首屆全國(guó)悅讀中醫(yī)校園之星作品集(第二輯)
- 運(yùn)動(dòng)損傷的治療與康復(fù)
- 中醫(yī)病機(jī)辨證學(xué)(第二版)
- 戰(zhàn)勝癌癥需“三防”:你不可不懂的癌癥三防常識(shí)
- 家庭護(hù)理指導(dǎo):腫瘤病人家庭護(hù)理