2

I am using an IDialog in my bot and one of my methods that is executed by the Bot Framework via context.Wait() had two arguments, as usual:

public async Task MainScreenSelectionReceived(IDialogContext context, 
                       IAwaitable<IMessageActivity> argument)

I want to add a third, optional argument to this method, which I will specify if I run this method directly from some place in my code (as opposed to when the Bot Framework runs it after context.Wait() and receiving a message from user).

So I change the method as follows:

public async Task MainScreenSelectionReceived(IDialogContext context, 
                       IAwaitable<IMessageActivity> argument, 
                       bool doNotShowPrompt = false)

Because of that, now all the context.Wait calls are shown as invalid:

invalid context.wait

That error disappears if I remove the third argument from the method declaration.

The message shown by Visual Studio is:

The type arguments for method IDialogStack.Wait(ResumeAfter resume) cannot be inferred from the usage. Try specifying the type arguments explicitly.

I assume that means I should call context.Wait as context.Wait<SOMETHING>, but I have no idea what to write instead of SOMETHING.

4

2 回答 2

2

进行重载,而不是添加可选参数。您的方法签名现在不再满足所需的委托。

例如:

public async Task MainScreenSelectionReceived(IDialogContext context, 
                       IAwaitable<IMessageActivity> argument, bool doNotShowPrompt) 
{
    //Original code here
}

public async Task MainScreenSelectionReceived(IDialogContext context, 
                       IAwaitable<IMessageActivity> argument) 
{
    return await MainScreenSelectionReceived(context, argument, false);
}
于 2016-12-14T02:58:40.370 回答
2

看一下被传递给的委托context.Wait()的声明方式:

public delegate Task ResumeAfter<in T>(IDialogContext context, IAwaitable<T> result);

您可以看到此方法期望通过具有确切签名的委托MainScreenSelectionReceived(IDialogContext context, IAwaitable<IMessageActivity> argument)

您可以创建一个直接调用的重载方法:

MainScreenSelectionReceived(IDialogContext context, IAwaitable<IMessageActivity> argument)
     => MainScreenSelectionReceived(context, argument, false);

MainScreenSelectionReceived(IDialogContext context, IAwaitable<IMessageActivity> argument,
bool doNotShowPrompt)
{
    // Do stuff here
}

或者将 lambda 表达式传递给context.Wait()方法:

context.Wait((c, a) => MainScreenSelectionReceived(c, a, false));
于 2016-12-14T03:01:25.303 回答