条形码扫描仪页面导航问题

时间:2018-08-27 21:07:18

标签: c# xamarin xamarin.forms zxing.net

我有遵循的本条形码阅读器/扫描器教程。在ScannerPage中,当我单击一个按钮时,它将打开scanner,并且在读取后,它将使用API​​将结果传递到数据库,并返回到ScannerPage。 我遇到此错误Page must not already have a parent,但我不知道为什么。怎么解决呢?

private async  void Button_Clicked(object sender, EventArgs e)
    {
        scanPage = new ZXingScannerPage();
        await Navigation.PushAsync(scanPage);
        scanPage.OnScanResult += (result) => 
        {
            Device.BeginInvokeOnMainThread( async() => 
            {
                await Navigation.PopAsync();
                //send something using WEB API
            });
        };
        await Navigation.PushAsync(scanPage);
    }

2 个答案:

答案 0 :(得分:1)

您两次按下scanPage

    scanPage = new ZXingScannerPage();

    // this is not needed
    // await Navigation.PushAsync(scanPage);

    scanPage.OnScanResult += (result) => 
    {
        Device.BeginInvokeOnMainThread( async() => 
        {
            await Navigation.PopAsync();
            //send something using WEB API
        });
    };

    // navigate here, AFTER you have defined your OnScanResult handler
    await Navigation.PushAsync(scanPage);

答案 1 :(得分:0)

您两次成为await Navigation.PushAsync(scanPage);

将其更改为:

private async  void Button_Clicked(object sender, EventArgs e)
{
    scanPage = new ZXingScannerPage();
    scanPage.OnScanResult += (result) => 
    {
        Device.BeginInvokeOnMainThread( async() => 
        {
            await Navigation.PopAsync();
            //send something using WEB API
        });
    };
    await Navigation.PushAsync(scanPage);
}