未等待 Await Action()
Await Action() is not awaited
我正在使用 ZXing.Net.Mobile 库扫描二维码。我想将负责扫描的代码放在 class 中,这样我就可以从中调用一个方法,它会 return 结果。类似的东西:
var scanner = new ZXing.Mobile.MobileBarcodeScanner();
var result = await scanner.Scan();
if (result != null)
Console.WriteLine("Scanned Barcode: " + result.Text);
这是来自官方存储库的示例,但它不起作用(至少对我而言)。
所以我创建了一个 class 并将 DID 工作的代码放在别处。这是 class 代码:
public class QrHandler
{
public ZXingScannerPage scanPage { get; set; }
public string Result { get; set; }
public async Task<string> Scan()
{
var options = new ZXing.Mobile.MobileBarcodeScanningOptions
{
PossibleFormats = new List<ZXing.BarcodeFormat>
{
ZXing.BarcodeFormat.QR_CODE
},
TryHarder = false,
AutoRotate = false,
TryInverted = false,
};
scanPage = new ZXingScannerPage();
scanPage.AutoFocus();
scanPage.OnScanResult += (result) =>
{
scanPage.IsScanning = false;
Device.BeginInvokeOnMainThread(async () =>
{
Application.Current.MainPage.Navigation.PopAsync();
try
{
Result = result.Text;
}
catch (Exception ex)
{
Result = ex.Message;
}
});
};
await Application.Current.MainPage.Navigation.PushAsync(scanPage);
return Result;
}
}
我正在通过如下方法调用它:
public async Task<string> Validate()
{
string _Result = "OK";
QrHandler q = new QrHandler();
_Result = await q.Scan();
return _Result;
}
问题是 Validate() returns _Result 在 q.Scan() returns 任何值之前。 q.Scan() 完成后不应该触发它吗?
在执行 OnScanResult
事件处理程序之前,您不能从 Scan()
return。
不清楚此事件何时引发,但只要它在您预期的时候实际引发,您可以使用 SemaphoreSlim
:
在 Scan()
方法中异步阻止
public async Task<string> Scan()
{
var options = new ZXing.Mobile.MobileBarcodeScanningOptions
{
PossibleFormats = new List<ZXing.BarcodeFormat>
{
ZXing.BarcodeFormat.QR_CODE
},
TryHarder = false,
AutoRotate = false,
TryInverted = false,
};
scanPage = new ZXingScannerPage();
scanPage.AutoFocus();
using (SemaphoreSlim semaphoreSlim = new SemaphoreSlim(0, 1))
{
scanPage.OnScanResult += (result) =>
{
scanPage.IsScanning = false;
Device.InvokeOnMainThread(async () =>
{
await Application.Current.MainPage.Navigation.PopAsync();
try
{
Result = result.Text;
}
catch (Exception ex)
{
Result = ex.Message;
}
semaphore.Release();
});
};
await Application.Current.MainPage.Navigation.PushAsync(scanPage);
await semaphoreSlim.WaitAsync();
}
return Result;
}
我正在使用 ZXing.Net.Mobile 库扫描二维码。我想将负责扫描的代码放在 class 中,这样我就可以从中调用一个方法,它会 return 结果。类似的东西:
var scanner = new ZXing.Mobile.MobileBarcodeScanner();
var result = await scanner.Scan();
if (result != null)
Console.WriteLine("Scanned Barcode: " + result.Text);
这是来自官方存储库的示例,但它不起作用(至少对我而言)。 所以我创建了一个 class 并将 DID 工作的代码放在别处。这是 class 代码:
public class QrHandler
{
public ZXingScannerPage scanPage { get; set; }
public string Result { get; set; }
public async Task<string> Scan()
{
var options = new ZXing.Mobile.MobileBarcodeScanningOptions
{
PossibleFormats = new List<ZXing.BarcodeFormat>
{
ZXing.BarcodeFormat.QR_CODE
},
TryHarder = false,
AutoRotate = false,
TryInverted = false,
};
scanPage = new ZXingScannerPage();
scanPage.AutoFocus();
scanPage.OnScanResult += (result) =>
{
scanPage.IsScanning = false;
Device.BeginInvokeOnMainThread(async () =>
{
Application.Current.MainPage.Navigation.PopAsync();
try
{
Result = result.Text;
}
catch (Exception ex)
{
Result = ex.Message;
}
});
};
await Application.Current.MainPage.Navigation.PushAsync(scanPage);
return Result;
}
}
我正在通过如下方法调用它:
public async Task<string> Validate()
{
string _Result = "OK";
QrHandler q = new QrHandler();
_Result = await q.Scan();
return _Result;
}
问题是 Validate() returns _Result 在 q.Scan() returns 任何值之前。 q.Scan() 完成后不应该触发它吗?
在执行 OnScanResult
事件处理程序之前,您不能从 Scan()
return。
不清楚此事件何时引发,但只要它在您预期的时候实际引发,您可以使用 SemaphoreSlim
:
Scan()
方法中异步阻止
public async Task<string> Scan()
{
var options = new ZXing.Mobile.MobileBarcodeScanningOptions
{
PossibleFormats = new List<ZXing.BarcodeFormat>
{
ZXing.BarcodeFormat.QR_CODE
},
TryHarder = false,
AutoRotate = false,
TryInverted = false,
};
scanPage = new ZXingScannerPage();
scanPage.AutoFocus();
using (SemaphoreSlim semaphoreSlim = new SemaphoreSlim(0, 1))
{
scanPage.OnScanResult += (result) =>
{
scanPage.IsScanning = false;
Device.InvokeOnMainThread(async () =>
{
await Application.Current.MainPage.Navigation.PopAsync();
try
{
Result = result.Text;
}
catch (Exception ex)
{
Result = ex.Message;
}
semaphore.Release();
});
};
await Application.Current.MainPage.Navigation.PushAsync(scanPage);
await semaphoreSlim.WaitAsync();
}
return Result;
}