1 回答

TA貢獻1854條經驗 獲得超8個贊
這是我能夠實現(xiàn)目標的一種方式。我不確定這是不是這樣做的正確方法。
我如何實現(xiàn)我的目標是使用 EventHandler。
為 AncSubscriberWrapper 類創(chuàng)建一個接口(IAncSubscriberWrapper)
public interface IAncSubscriberWrapper
{
event EventHandler UnshownNotificationCounterUpdated;
}
并像下面那樣實現(xiàn)它
public class AncSubscriberWrapper : NotificationCenterFacade, IAncSubscriberWrapper
然后實現(xiàn)AncSubscriberWrapper類里面的接口成員
public event EventHandler UnshownNotificationCounterUpdated;
并在 OnUnshownCounterUpdatedAsync 方法中調用方法
public Task OnUnshownCounterUpdatedAsync(long counter)
{
UnshownNotificationCounterUpdated?.Invoke(this, null);
return Task.CompletedTask;
}
然后去 BasePageViewModel 注冊和注銷監(jiān)聽器。
public void Initialize()
{
m_ancSubscriberWrapper.UnshownNotificationCounterUpdated += OnUnshownNotificationCounterUpdated;
}
public void Teardown()
{
m_ancSubscriberWrapper.UnshownNotificationCounterUpdated -= OnUnshownNotificationCounterUpdated;
}
由于 BasePageViewModel 是一個抽象類,任何使用它的具體類都會覆蓋這些方法。(下面的代碼片段與這個問題沒有直接關系,而是為了理解我的代碼基礎上的設計)
/*
* BaseContainerViewController is only accepting TPageViewModel generic type of classes only.
* TPageViewModel inherits BasePageViewModel and BaseContainerViewController inherits BaseViewController
* That's what following line is all about
*/
public abstract class BaseContainerViewController<TPageViewModel> : BaseViewController where TPageViewModel : BasePageViewModel
和
public class WardListViewController : BaseContainerViewController<WardListPageViewModel>
回到答案的最后一部分。在 BasePageViewModel 類中添加以下方法,這將完成該過程。
protected virtual void OnUnshownNotificationCounterUpdated(object sender, EventArgs eventArgs)
{
GetUnseenNotificationsCount();
}
- 1 回答
- 0 關注
- 109 瀏覽
添加回答
舉報