1

我将 ZXing MobileScanner 库与我的 iOS 版 xamarin 项目(MonoTouch)一起使用。我设置了扫描,它工作正常,读取我的二维码并得到正确的结果。阅读 QRCode 后,我想展示另一个 ViewController。我在第二个控制器中使用扫描结果设置了一个属性,然后想要显示控制器。

第二个视图未显示在屏幕上。没有错误,没有反馈,根本没有显示。我猜,MobileScanner 建立了自己的 View(可以在 lib 的源代码中看到)并将其添加到 NavigationController - 这会导致我的 Controller 保持“落后”。只需单击按钮即可重定向到我的控制器进行测试工作正常。我还尝试通过调用scanner.Cancel()来“处理”视图,但这会导致

Warning: Attempt to dismiss from view controller <UINavigationController: 0x19a0c60> while a presentation or dismiss is in progress!

这是我的代码,感谢任何有关如何显示我的视图的帮助。

public override void ViewDidLoad ()
    {
        base.ViewDidLoad ();

        // Perform any additional setup after loading the view, typically from a nib.

        scanner = new MobileBarcodeScanner(this.NavigationController);

        this.btnScan.TouchUpInside += (sender, e) => {

            //Tell our scanner to use the default overlay
            scanner.UseCustomOverlay = false;
            //We can customize the top and bottom text of the default overlay
            scanner.TopText = "Ticket vor den Scanner halten";
            scanner.BottomText = "Code wird automatisch eingelesen";

            //Start scanning
            scanner.Scan ().ContinueWith((t) => 
                                         {
                //Our scanning finished callback
                if (t.Status == System.Threading.Tasks.TaskStatus.RanToCompletion){
                    string msg = "";
                    ZXing.Result result = t.Result;

                    if (result != null && !string.IsNullOrEmpty (result.Text)) {

                        scanner.Cancel();

                        if(this.ticketScreen == null) {
                            this.ticketScreen = new TicketScreen();
                        } 

                        this.ticketScreen.ticketUrl = result.Text;
                        this.NavigationController.PushViewController(this.ticketScreen, true);

                    } else {
                        msg = "Code nicht erkannt!";
                        this.InvokeOnMainThread(() => {
                            var av = new UIAlertView("Fehler!", msg, null, "OK", null);
                            av.Show();
                        });
                    }
                }
            });


        };


    }
4

1 回答 1

0

我使用this.InvokeOnMainThread(()它在我的代码中运行良好。您的解决方案是:

公共覆盖无效 ViewDidLoad () { base.ViewDidLoad ();

    // Perform any additional setup after loading the view, typically from a nib.

    scanner = new MobileBarcodeScanner(this.NavigationController);

    this.btnScan.TouchUpInside += (sender, e) => {

        //Tell our scanner to use the default overlay
        scanner.UseCustomOverlay = false;
        //We can customize the top and bottom text of the default overlay
        scanner.TopText = "Ticket vor den Scanner halten";
        scanner.BottomText = "Code wird automatisch eingelesen";

        //Start scanning
        scanner.Scan ().ContinueWith((t) => 
                                     {
            //Our scanning finished callback
            if (t.Status == System.Threading.Tasks.TaskStatus.RanToCompletion){
                string msg = "";
                ZXing.Result result = t.Result;

                if (result != null && !string.IsNullOrEmpty (result.Text)) {

                    scanner.Cancel();

        this.InvokeOnMainThread(() => 
            {
              if(this.ticketScreen == null) {
                    this.ticketScreen = new TicketScreen();
                } 

                this.ticketScreen.ticketUrl = result.Text;
                this.NavigationController.PushViewController(this.ticketScreen, true);

        });



                } else {
                    msg = "Code nicht erkannt!";
                    this.InvokeOnMainThread(() => {
                        var av = new UIAlertView("Fehler!", msg, null, "OK", null);
                        av.Show();
                    });
                }
            }
        });


    };


} 
于 2013-07-25T12:21:16.053 回答