【发布时间】:2016-02-23 13:53:46
【问题描述】:
我目前正在使用带有 PCL 的 xamarin 表单来访问相机并扫描条形码并将其显示在 UserDialog 中。我可以使用依赖服务轻松做到这一点。我遇到的问题要回去了。我想通过按 userDialog 上的取消按钮返回 PCL。我正在使用消息中心返回 PCL 主页,消息确实返回,但 UI 保持不变,即相机屏幕保持不变。
下面是我的代码
void HandleScanResult(ZXing.Result result)
{
if (result != null && !string.IsNullOrEmpty(result.Text))
{
CrossVibrate.Current.Vibration(500);
}
Xamarin.Forms.Device.BeginInvokeOnMainThread(async () =>
{
resultText.Text = await SaveScannedRecord(result.Text);
PromptResult promptResult = await UserDialogs.Instance.PromptAsync
("Hello Friends","Question","SCAN","CANCEL",resultText.Text,InputType.Name);
if (promptResult.Ok)
{
}
else
{
//CODE TO GO BACK
var home = new Home();
RunOnUiThread(() => { xamarinForm.MessagingCenter.Send<Home>(home, "scannedResult"); });
}
});
}
【问题讨论】:
-
向另一个页面发送消息只是发送消息:您需要在收到消息时执行一些操作以关闭扫描仪页面。你如何做到这一点取决于你首先如何显示它。您还将 RunOnUIThread 嵌套在 BeginInvokeOnMainThread 中,这是不必要的。最后,您应该可以直接从 PCL 调用 ZXIng。有关执行此操作的 Forms 示例应用程序,请参见 github 页面。
-
如何关闭扫描仪页面。
-
视情况而定 - 您还没有显示实际显示页面的代码。
-
我使用依赖服务从 PCL 类(Home 类)调用它。 IBarcodeScanner 扫描器 = DependencyService.Get
();扫描仪.Scan(config.ScannerKey, config.ServiceUrl); -
在示例中,它似乎会自动关闭扫描视图。我不确定你做错了什么。我会尝试等待 Scan 方法,或使用包的 Forms 版本。
标签: xamarin xamarin.forms