3

我的代码实现有问题。我目前正在使用 PushSharp 库,我想做的一件事是如果事件触发,我想根据它是什么事件返回一个真值或假值。这是代码:

public static bool SenddNotification()
{
var push = new PushBroker();

        //Wire up the events for all the services that the broker registers
        push.OnNotificationSent += NotificationSent;
        push.OnDeviceSubscriptionChanged += DeviceSubscriptionChanged;

}


static bool DeviceSubscriptionChanged(object sender, string oldSubscriptionId, string newSubscriptionId, INotification notification)
    {
        //Currently this event will only ever happen for Android GCM
        Console.WriteLine("Device Registration Changed:  Old-> " + oldSubscriptionId + "  New-> " + newSubscriptionId + " -> " + notification);
        return false;
    }

    static bool NotificationSent(object sender, INotification notification)
    {
        Console.WriteLine("Sent: " + sender + " -> " + notification);
        return true;
    }

所以我想要的是如果事件触发,根据发生的情况返回真或假,然后最终在第一个方法中返回这个值

4

1 回答 1

3

您可以设置一个全局布尔变量,并让您的事件设置该变量,然后让您的第一个方法返回它。像这样的东西:

private bool globalBool;

public static bool SenddNotification()
{
var push = new PushBroker();

        //Wire up the events for all the services that the broker registers
        push.OnNotificationSent += NotificationSent;
        push.OnDeviceSubscriptionChanged += DeviceSubscriptionChanged;

        return globalBool;  
}


static bool DeviceSubscriptionChanged(object sender, string oldSubscriptionId, string newSubscriptionId, INotification notification)
    {
        //Currently this event will only ever happen for Android GCM
        Console.WriteLine("Device Registration Changed:  Old-> " + oldSubscriptionId + "  New-> " + newSubscriptionId + " -> " + notification);
        globalBool = false;
    }

    static bool NotificationSent(object sender, INotification notification)
    {
        Console.WriteLine("Sent: " + sender + " -> " + notification);
        globalBool = true;
    }

当然,您必须null在退货之前对其进行检查,并妥善处理。

于 2013-07-30T21:35:46.643 回答