私の質問:非同期メソッドからの入力を待つ方法はありますか?非同期メソッドの非同期メソッド
背景情報:私はQRコードをスキャンし、スキャンした値に基づいてデータをロードするC#アプリケーションを持っています。 上記はうまくいきましたが、今では、スキャンされた値が適切なカードであるかどうかをアプリが尋ねるようにしたいと考えています。
ザは、次のようにコードが適用される:
using ZXing.Mobile;
MobileBarcodeScanner scanner;
private bool Correct = false;
//Create a new instance of our scanner
scanner = new MobileBarcodeScanner(this.Dispatcher);
scanner.Dispatcher = this.Dispatcher;
await scanner.Scan().ContinueWith(t =>
{
if (t.Result != null)
HandleScanResult(t.Result);
});
if (Continue)
{
Continue = false;
Frame.Navigate(typeof(CharacterView));
}
HandleScanResult(Result)
は(ダウン脱脂)される:
async void HandleScanResult(ZXing.Result result)
{
int idScan = -1;
if (int.TryParse(result.Text, out idScan) && idScan != -1)
{
string ConfirmText = CardData.Names[idScan] + " was found, is this the card you wanted?";
MessageDialog ConfirmMessage = new MessageDialog(ConfirmText);
ConfirmMessage.Commands.Add(new UICommand("Yes") { Id = 0 });
ConfirmMessage.Commands.Add(new UICommand("No") { Id = 1 });
IUICommand action = await ConfirmMessage.ShowAsync();
if ((int) action.Id == 0)
Continue = true;
else
Continue = false;
}
}
問題はContinue
瞬間if (Continue)
は1日ブロックで呼び出される偽のままでありますメッセージボックスが非同期であり、メッセージボックスが完了する前にアプリケーションがif文に続行するために、
私は既にHandleScanResult()
にタスクの戻り値を与え、await HandleScanResult(t.Result);
を呼び出してみました。これにより、アプリケーションはHandleScanResult()
を待ってからifステートメントに進みます。 しかし、これは次のエラーが返されます。そのために行く前に、入力を待つ方法についての私の質問を
The 'await' operator can only be used within an async lambda expression. Consider marking this lambda expression with the 'async' modifier.
を。
ありがとうございました!私はいくつかのサンプルコードから 'ContinueWith'を持っていたので、なぜそれを使ったのですか?もう一度ありがとう、もし私がより多くの担当者を持っていたら+1したい;-) – Fons