Visual Studio App Center:'await' 运算符只能在异步方法中使用
Visual Studio App Center: The 'await' operator can only be used within an async method
我需要有关 App Center 推送通知的帮助。我想知道用户是否在他的 iOS/Android 设备上启用或禁用了推送通知。此任务应在应用程序启动时完成。我遵循了这个 App Center 教程,但是当我检查推送通知是启用还是禁用时,我收到一条错误消息。
Error CS4033: The 'await' operator can only be used within an async
method. Consider marking this method with the 'async' modifier and
changing its return type to 'Task'.
怎么了?我如何才能知道用户是否启用或禁用了推送通知?
using Foundation;
using UIKit;
using Microsoft.AppCenter;
using Microsoft.AppCenter.Analytics;
using Microsoft.AppCenter.Crashes;
using Microsoft.AppCenter.Push;
namespace iosprojectnew.iOS
{
[Register("AppDelegate")]
class Program : UIApplicationDelegate
{
private static Game1 game;
internal static void RunGame()
{
game = new Game1();
game.Run();
}
static void Main(string[] args)
{
UIApplication.Main(args, null, "AppDelegate");
}
public override void FinishedLaunching(UIApplication app)
{
if (!AppCenter.Configured)
{
bool isEnabled = await Push.IsEnabledAsync();
Push.PushNotificationReceived += (sender, e) =>
{
// Add the notification message and title to the message
var summary = $"Push notification received:" +
$"\n\tNotification title: {e.Title}" +
$"\n\tMessage: {e.Message}";
// If there is custom data associated with the notification,
// print the entries
if (e.CustomData != null)
{
summary += "\n\tCustom data:\n";
foreach (var key in e.CustomData.Keys)
{
summary += $"\t\t{key} : {e.CustomData[key]}\n";
}
}
// Send the notification summary to debug output
System.Diagnostics.Debug.WriteLine(summary);
};
}
AppCenter.Start("...", typeof(Analytics), typeof(Crashes), typeof(Push));
RunGame();
}
}
}
使用 await
的方法必须标记为 async
。尝试在 void FinishedLaunching
之前添加 async
关键字:
public override async void FinishedLaunching(UIApplication app) { ... }
await 关键字只能用于异步方法,但此方法是重写方法,因此您不能转换为任务。
无论如何,你可以像这样在使用异步的地方编写行代码:
bool isEnabled = await Push.IsEnabledAsync().Result;
我需要有关 App Center 推送通知的帮助。我想知道用户是否在他的 iOS/Android 设备上启用或禁用了推送通知。此任务应在应用程序启动时完成。我遵循了这个 App Center 教程,但是当我检查推送通知是启用还是禁用时,我收到一条错误消息。
Error CS4033: The 'await' operator can only be used within an async method. Consider marking this method with the 'async' modifier and changing its return type to 'Task'.
怎么了?我如何才能知道用户是否启用或禁用了推送通知?
using Foundation;
using UIKit;
using Microsoft.AppCenter;
using Microsoft.AppCenter.Analytics;
using Microsoft.AppCenter.Crashes;
using Microsoft.AppCenter.Push;
namespace iosprojectnew.iOS
{
[Register("AppDelegate")]
class Program : UIApplicationDelegate
{
private static Game1 game;
internal static void RunGame()
{
game = new Game1();
game.Run();
}
static void Main(string[] args)
{
UIApplication.Main(args, null, "AppDelegate");
}
public override void FinishedLaunching(UIApplication app)
{
if (!AppCenter.Configured)
{
bool isEnabled = await Push.IsEnabledAsync();
Push.PushNotificationReceived += (sender, e) =>
{
// Add the notification message and title to the message
var summary = $"Push notification received:" +
$"\n\tNotification title: {e.Title}" +
$"\n\tMessage: {e.Message}";
// If there is custom data associated with the notification,
// print the entries
if (e.CustomData != null)
{
summary += "\n\tCustom data:\n";
foreach (var key in e.CustomData.Keys)
{
summary += $"\t\t{key} : {e.CustomData[key]}\n";
}
}
// Send the notification summary to debug output
System.Diagnostics.Debug.WriteLine(summary);
};
}
AppCenter.Start("...", typeof(Analytics), typeof(Crashes), typeof(Push));
RunGame();
}
}
}
使用 await
的方法必须标记为 async
。尝试在 void FinishedLaunching
之前添加 async
关键字:
public override async void FinishedLaunching(UIApplication app) { ... }
await 关键字只能用于异步方法,但此方法是重写方法,因此您不能转换为任务。
无论如何,你可以像这样在使用异步的地方编写行代码:
bool isEnabled = await Push.IsEnabledAsync().Result;