Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/334.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# Wait运算符只能在异步方法中使用,但该方法是异步的_C#_Asynchronous_Async Await - Fatal编程技术网

C# Wait运算符只能在异步方法中使用,但该方法是异步的

C# Wait运算符只能在异步方法中使用,但该方法是异步的,c#,asynchronous,async-await,C#,Asynchronous,Async Await,我想在VS12中使用MessageDialog。到目前为止,我一直使用: 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"

我想在VS12中使用MessageDialog。到目前为止,我一直使用:

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;
MessageDialog msgdlg=newmessagedialog(“选择颜色”、“如何异步1”);
msgdlg.DefaultCommandIndex=1;
添加(新的UICommand(“红色”,null,Colors.Red));
添加(新的UICommand(“绿色”,null,Colors.Green));
添加(新的UICommand(“Blue”,null,Colors.Blue));
IAsyncOperation asyncopy=msgdlg.ShowAsync();
asyncOp.Completed=OnMessageDialogShowAsyncCompleted;
现在我想消除回调,并使用带有wait的匿名方法。出于测试目的,我使用了:

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;
MessageDialog msgdlg=newmessagedialog(“选择颜色”、“3”);
添加(新的UICommand(“红色”,null,Colors.Red));
添加(新的UICommand(“绿色”,null,Colors.Green));
添加(新的UICommand(“Blue”,null,Colors.Blue));
//显示消息对话框
IAsyncOperation asyncopy=msgdlg.ShowAsync();
IUICommand命令=等待异步操作;
问题是,wait会产生错误,即使showsync()显然是异步的。“Acess”运算符只能在异步方法中使用。请考虑用“AsiNC”修饰符标记该方法,并将其返回类型改为“任务”。 这里有什么问题


好的,谢谢你的评论,我现在做这个:

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
        };
load+=async(发送方,参数)=>
{
#使用等待的区域(从C#5.0开始)
MessageDialog msgdlg=newmessagedialog(“选择颜色”,“#3”);
添加(新的UICommand(“红色”,null,Colors.Red));
添加(新的UICommand(“绿色”,null,Colors.Green));
添加(新的UICommand(“Blue”,null,Colors.Blue));
//显示消息对话框
IAsyncOperation asyncopy=msgdlg.ShowAsync();
IUICommand命令=等待异步操作;
#端区
};

现在它工作了-非常感谢

你应该使你的方法
异步
。你不能在
非异步
函数中使用
等待

你应该使你的方法
异步
。你不能在
非异步
函数中使用
等待


但是使用上述源代码的方法是构造函数(第页)

不能有异步构造函数。将异步工作移出构造函数。可能会将其移动到类似于
的加载事件中。我不知道您使用的是什么GUI框架,但它们总是有一个
Load
事件


但是使用上述源代码的方法是构造函数(第页)


不能有异步构造函数。将异步工作移出构造函数。可能会将其移动到类似于
的加载事件中。我不知道您使用的是什么GUI框架,但它们总是有一个加载事件。

调用这些代码的函数也应该是异步的。请参阅下面的MS官方示例中的详细信息。请注意,ForgotPassword的返回是异步任务,而不是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);
}
公共异步任务); 返回视图(“放弃密码确认”); } //如果我们走到这一步,有些东西失败了,重新显示形式 返回视图(模型); }
调用这些代码的函数也应该是异步的。请参阅下面的MS官方示例中的详细信息。请注意,放弃密码的返回是异步任务,而不是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);
}
公共异步任务); 返回视图(“放弃密码确认”); } //如果我们走到这一步,有些东西失败了,重新显示形式 返回视图(模型); }
请出示您的方法声明。大概它没有
async
修饰符。为什么不呢?一旦你开始异步,几乎你的整个调用堆栈都应该被标记为异步。一直以来都是异步的。“即使ShowAsync()显然是异步的”——这无关紧要。它抱怨的是您当前正在编写的方法,而不是您正在调用的任何方法。但是使用上述源代码的方法是构造函数(第页)。请显示您的方法声明。大概它没有
async
修饰符。为什么不呢?一旦你开始异步,几乎你的整个调用堆栈都应该被标记为异步。一直以来都是异步的。“即使ShowAsync()显然是异步的”——这无关紧要。它抱怨的是您当前正在编写的方法,而不是您正在调用的任何方法。但是使用上面源代码的方法是构造函数(第页)。但是使用上面源代码的方法是构造函数(第页)。但是使用上面源代码的方法是构造函数(第页)。好的,谢谢。我将代码移动到一个匿名方法中,以便加载。谢谢!好的,谢谢。我将代码移动到一个匿名方法中,以便加载。谢谢!