嘿伙计们,我在执行代码方面遇到了麻烦。我目前正在使用PushSharp库,我想做的其中一件事是,如果事件触发,我想根据它是什么事件返回true或false值。下面是代码:
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;
}
所以我想要的是,如果事件触发,根据发生的情况返回true或false,然后最终在第一个方法中返回此值
答案 0 :(得分:2)
您可以设置一个全局bool变量,并将您的事件设置为该变量,然后让您的第一个方法返回它。像这样:
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
,并妥善处理。