简体   繁体   English

await运算符只能在async方法中使用-但方法是异步的

[英]await operator can only be used within an async method - but method is asynchron

I want to use a MessageDialog in VS12. 我想在VS12中使用MessageDialog。 Until now I have used: 到目前为止,我已经使用:

MessageDialog msgdlg = new MessageDialog("Choose a color", "How To Async #1");
msgdlg.DefaultCommandIndex = 1;
msgdlg.Commands.Add(new UICommand("Red", null, Colors.Red));
msgdlg.Commands.Add(new UICommand("Green", null, Colors.Green));
msgdlg.Commands.Add(new UICommand("Blue", null, Colors.Blue));

IAsyncOperation<IUICommand> asyncOp = msgdlg.ShowAsync();
asyncOp.Completed = OnMessageDialogShowAsyncCompleted;

Now I want to eliminate the callbacks and use an anonymous methode with await. 现在,我想消除回调并在等待中使用匿名方法。 For test purpose i used: 为了测试目的,我使用了:

MessageDialog msgdlg = new MessageDialog("Choose a color", "#3");
msgdlg.Commands.Add(new UICommand("Red", null, Colors.Red));
msgdlg.Commands.Add(new UICommand("Green", null, Colors.Green));
msgdlg.Commands.Add(new UICommand("Blue", null, Colors.Blue));

// Show the MessageDialog
IAsyncOperation<IUICommand> asyncOp = msgdlg.ShowAsync();
IUICommand command = await asyncOp;

Problem is, await produces an error, even if ShowAsync() is obviously asynchron. 问题是,即使ShowAsync()显然是异步的,await也会产生错误。 "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'." “'await'运算符只能在异步方法中使用。请考虑使用'async'修饰符标记该方法,并将其返回类型更改为'Task'。”

Whats the problem here? 这里有什么问题?


Ok, thanks to your comments I now doing this: 好的,感谢您的评论,我现在这样做:

Loaded += async (sender, args) =>
        {
            #region Using await (from C# 5.0 on)
            MessageDialog msgdlg = new MessageDialog("Choose a color", "#3");
            msgdlg.Commands.Add(new UICommand("Red", null, Colors.Red));
            msgdlg.Commands.Add(new UICommand("Green", null, Colors.Green));
            msgdlg.Commands.Add(new UICommand("Blue", null, Colors.Blue));

            // Show the MessageDialog
            IAsyncOperation<IUICommand> asyncOp = msgdlg.ShowAsync();
            IUICommand command = await asyncOp;

            #endregion
        };

Now it works - thanks a lot! 现在可以正常工作-非常感谢!

But the method which uses the source from above is a constructor (page). 但是,从上方使用源代码的方法是构造函数(页面)。

You cannot have asynchronous constructors. 您不能具有异步构造函数。 Move the async work out of the constructor. 将异步工作移出构造函数。 Maybe move it into a Load -like event. 也许将其移动到类似Load的事件中。 I don't know what GUI framework you're using but they always have a Load event. 我不知道您使用的是什么GUI框架,但它们始终有Load事件。

您应该使方法async 。不能在non-async函数内部使用await

The function which calls those codes should be async as well. 调用这些代码的函数也应该是异步的。 See details in this MS official sample bellow. 请在下面的MS官方样本中查看详细信息。 Attention that the return of ForgotPassword is async Task< ActionResult > instead of ActionResult. 注意,ForgotPassword的返回是异步Task <ActionResult>而不是ActionResult。

public async Task<ActionResult> ForgotPassword(ForgotPasswordViewModel model)
{
    if (ModelState.IsValid)
    {
        var user = await UserManager.FindByNameAsync(model.Email);
        if (user == null || !(await UserManager.IsEmailConfirmedAsync(user.Id)))
        {
        // Don't reveal that the user does not exist or is not confirmed
        return View("ForgotPasswordConfirmation");
    }

    var code = await UserManager.GeneratePasswordResetTokenAsync(user.Id);
    var callbackUrl = Url.Action("ResetPassword", "Account", 
    new { UserId = user.Id, code = code }, protocol: Request.Url.Scheme);
    await UserManager.SendEmailAsync(user.Id, "Reset Password", 
    "Please reset your password by clicking here: <a href=\"" + callbackUrl + "\">link</a>");        
    return View("ForgotPasswordConfirmation");
}

// If we got this far, something failed, redisplay form
return View(model);
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 &#39;await&#39;运算符只能在async&gt;方法中使用 - The 'await' operator can only be used within an async > method await 运算符只能在异步方法中使用 - The await operator can only be used within an async method “ await”运算符只能在异步方法中使用[MySQL连接器] - The 'await' operator can only be used within an async method [MySQL connector] C# await 运算符只能在异步方法中使用,但方法是异步的 - C# The await operator can only be used within an async method but method is async Visual Studio App Center:“ await”运算符只能在异步方法中使用 - Visual Studio App Center: The 'await' operator can only be used within an async method &#39;await&#39;运算符只能在具有c#异常的异步方法中使用吗? - 'await' operator can only be used within an async method with c# exception? “await”运算符只能在异步方法中使用。 考虑使用“async”修饰符标记此方法并更改其返回类型 - The 'await' operator can only be used within an async method. Consider marking this method with the 'async' modifier and changing its return type 等待操作符只能在标有&#39;async&#39;修饰符的方法或lambda中使用 - The await operator can only be used in a method or lambda marked with the 'async' modifier &#39;await&#39;运算符只能在异步lambda表达式错误中使用 - The 'await' operator can only be used within an async lambda expression error 'await' 运算符只能在异步 lambda 表达式中使用 - The 'await' operator can only be used within an async lambda expression
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM