如何从另一个类中的另一个回调方法执行抽象类内部的异步方法

时间:2019-01-07 04:23:37

标签: c# async-await task abstract-class

我有一个具体的类调用AncSubscriberWrapper,它有一个回调方法

    public Task OnUnshownCounterUpdatedAsync(long counter)
    {
        return Task.CompletedTask;
    }

还有一个抽象类叫BasePageViewModel,它有一个异步方法

    protected async void GetUnseenNotificationsCount()
    {
        UnseenNotificationCount  = await m_ancSubscriberWrapper.TryGetNotificationCountAsync(NotificationStatus.Delivered | NotificationStatus.Created);
    }

类路径类似于下面的项目内部。

  

./ src / Visit.Common / Services / AncSubscriberWrapper.cs   ./src/Visit.Common/ViewModels/PageViewModels/BasePageViewModel.cs

我需要从OnUnshownCounterUpdatedAsync()方法执行GetUnseenNotificationsCount()方法。

我该怎么做?

1 个答案:

答案 0 :(得分:-1)

这是我达到目标的一种方式。我不确定这是否是正确的方法。

我如何实现我的目标是使用EventHandler。

为AncSubscriberWrapper类创建接口(IAncSubscriberWrapper)

InputCount = 136
OutputCount = 21
H1_Count = 84
H2_Count = 42
H3_Count = 0
H4_Count = 0
initializer = 'he_normal'
activation = ['' ,'elu','elu','elu','elu']
dropout    = [0.5,0.5  ,0.5  ,0.5  ,0.5  ]
batch_norm = 1

optimizer = optimizers.Adam(lr=0.001, beta_1=0.9, beta_2=0.999, epsilon=None, decay=0.0, amsgrad=False)
loss = 'mse' 

epochs = 500
batch_size = 10000  #Stochatic mini-batch size

model = Sequential()

if (dropout[0] > 0) :
    model.add(Dropout(dropout[0], input_shape=(InputCount,)) )
    model.add(Dense(H1_Count, activation=activation[1], kernel_initializer=initializer ))
    if (dropout[1] > 0) : model.add(Dropout(dropout[1]))
else :
    model.add(Dense(H1_Count, activation=activation[1], kernel_initializer=initializer, input_shape=(InputCount,) ))
    if (dropout[1] > 0) : model.add(Dropout(dropout[1]))

if (batch_norm == 1) : model.add(BatchNormalization())

if (H2_Count > 0) :
    model.add(Dense(H2_Count,activation=activation[2], kernel_initializer=initializer))
    if (batch_norm == 1) : model.add(BatchNormalization())
    if (dropout[2] > 0) : model.add(Dropout(dropout[2]))

if (H3_Count > 0) :
    model.add(Dense(H3_Count, activation=activation[3], kernel_initializer=initializer))
    if (batch_norm == 1) : model.add(BatchNormalization())
    if (dropout[3] > 0) : model.add(Dropout(dropout[3]))

if (H4_Count > 0) :
    model.add(Dense(H4_Count, activation=activation[4], kernel_initializer=initializer))
    if (batch_norm == 1) : model.add(BatchNormalization())
    if (dropout[4] > 0) : model.add(Dropout(dropout[4]))

model.add(Dense(OutputCount, activation='linear'))

model.summary()

并像下面一样实现

public interface IAncSubscriberWrapper
{
    event EventHandler UnshownNotificationCounterUpdated;
}

然后在AncSubscriberWrapper类中实现接口成员

public class AncSubscriberWrapper : NotificationCenterFacade, IAncSubscriberWrapper

并在OnUnshownCounterUpdatedAsync方法内调用方法

public event EventHandler UnshownNotificationCounterUpdated;

然后转到BasePageViewModel并注册和取消注册侦听器。

public Task OnUnshownCounterUpdatedAsync(long counter)
{
    UnshownNotificationCounterUpdated?.Invoke(this, null);
    return Task.CompletedTask;
}

因为BasePageViewModel是一个抽象类,所以使用它的具体类都会覆盖这些方法。 (以下代码段与该问题没有直接关系,而是为了根据我的代码理解设计)

public void Initialize()
{
    m_ancSubscriberWrapper.UnshownNotificationCounterUpdated += OnUnshownNotificationCounterUpdated;
}

public void Teardown()
{
    m_ancSubscriberWrapper.UnshownNotificationCounterUpdated -= OnUnshownNotificationCounterUpdated;
}

/*
* 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

返回答案的最后一部分。在BasePageViewModel类中添加以下方法,即可完成该过程。

public class WardListViewController : BaseContainerViewController<WardListPageViewModel>