0
我試圖創建一個監聽器到Windows的通知,下面就this example on Micr*soft's blog如何等待RequestAccessAsync?
非常第二代碼剪斷的步驟,理應得到用戶授權,不編譯說:
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'.
雖然我不熟悉C#的異步/等待機制,該方法的問題叫做RequestAccess 異步,並且由Visual Studio打開的源在方法上方有一個[RemoteAsync]
。
這是一個完整的代碼剪斷:
// Get the listener
UserNotificationListener listener = UserNotificationListener.Current;
// And request access to the user's notifications (must be called from UI thread)
UserNotificationListenerAccessStatus accessStatus = await listener.RequestAccessAsync();
switch (accessStatus)
{
// This means the user has granted access.
case UserNotificationListenerAccessStatus.Allowed:
// Yay! Proceed as normal
break;
// This means the user has denied access.
// Any further calls to RequestAccessAsync will instantly
// return Denied. The user must go to the Windows settings
// and manually allow access.
case UserNotificationListenerAccessStatus.Denied:
// Show UI explaining that listener features will not
// work until user allows access.
break;
// This means the user closed the prompt without
// selecting either allow or deny. Further calls to
// RequestAccessAsync will show the dialog again.
case UserNotificationListenerAccessStatus.Unspecified:
// Show UI that allows the user to bring up the prompt again
break;
}
我怎樣才能解決這個問題?
哪裏是你的方法的簽名?你把它標記爲「異步」嗎? –
你需要展示更多的代碼,你需要展示保存你的代碼示例的方法, –
感謝那些問題。這段代碼是在一個類的構造函數中,(正如我剛學到的,不能是異步的) – RSFalcon7