【问题标题】:MonoTouch ZXING Camera Won't OpenMonoTouch ZXING 相机打不开
【发布时间】:2012-07-04 16:41:56
【问题描述】:

我正在使用此应用扫描 ZXING 条码:

https://github.com/jmawebtech/BarcodeReader-MonoTouch

如果我进入应用程序,扫描条形码,按主页按钮,重新进入应用程序,然后单击扫描,我看到一个看起来像相机快门永远不会打开的黑屏。我在这张票上附上了一张图片。

如果我按下取消,然后返回扫描,我会看到相机再次打开。

为什么有时候相机总是打不开?

【问题讨论】:

    标签: c# xamarin.ios zxing


    【解决方案1】:

    我必须将此代码添加到 Info.plist:

    UIApplicationExitsOnSuspend 是

    对于 App Delegate,我必须添加以下代码:

        public override void OnResignActivation (UIApplication application)
        {
            UIApplication.SharedApplication.PerformSelector(new Selector("terminateWithSuccess"), null, 0f);
        }
    

    此软件中使用的录像机无法在后台线程上运行。

    【讨论】:

    • 您可以解决此问题,而不会导致您的应用在发送到后台时终止。请看我的回答
    【解决方案2】:

    我通过创建自己的扫描方法解决了这个问题,该方法保留了对先前显示的ViewController 的引用,并在显示新的之前将其处理掉。

    public static class BarcodeScanner
    {
        private static ZxingCameraViewController currentBarcodeScanner;
    
        public static Task<Result> Scan(UIViewController hostController, MobileBarcodeScanner scanner, MobileBarcodeScanningOptions options)
        {
            return Task.Factory.StartNew(delegate
            {
                Result result = null;
    
                var scanResultResetEvent = new ManualResetEvent(false);
    
                hostController.InvokeOnMainThread(delegate
                {
                    // Release previously displayed barcode scanner
                    if (currentBarcodeScanner != null)
                    {
                        currentBarcodeScanner.Dispose();
                        currentBarcodeScanner = null;
                    }
    
                    currentBarcodeScanner = new ZxingCameraViewController(options, scanner);
    
                    // Handle barcode scan event
                    currentBarcodeScanner.BarCodeEvent += delegate(BarCodeEventArgs e)
                    {
                        currentBarcodeScanner.DismissViewController();
                        result = e.BarcodeResult;
                        scanResultResetEvent.Set();
                    };
    
                    // Handle barcode scan cancel event
                    currentBarcodeScanner.Canceled += delegate
                    {
                        currentBarcodeScanner.DismissViewController();
                        scanResultResetEvent.Set();
                    };
    
                    // Display the camera view controller
                    hostController.PresentViewController(currentBarcodeScanner, true, delegate{});
                });
    
                // Wait for scan to complete
                scanResultResetEvent.WaitOne();
    
                return result;
            });
        }
    }
    

    然后你就这样使用它

    BarcodeScanner.Scan(this)
        .ContinueWith(t => InvokeOnMainThread(() =>
        {
            if (t.Result == null)
            {
                new UIAlertView("Scan Cancelled", "The barcode scan was cancelled", null, null, "OK").Show();
            }
            else
            {
                new UIAlertView("Scan Complete", "Result from barcode scan was " + t.Result, null, null, "OK").Show();
            }
        }))
    

    【讨论】:

      【解决方案3】:

      你不必极端地改变你的应用程序而不是在后台运行。

      我通过在 AppDelegate 上创建一个 viewcontroller 属性解决了这个问题,每次我启动 cameraviewcontroller 打开相机时,我都会指向这个属性。我叫它cvc。

      我通过以下方式引用了该属性:

      AppDelegate ad = (AppDelegate)UIApplication.SharedApplication.Delegate;

      然后在 AppDelegate 我有这个代码:

          public override void OnResignActivation (UIApplication application)
          {
              cvc.PerformSelector(new Selector("terminateWithSuccess"), null, 0f);
          }
      

      感谢您的启动

      【讨论】:

      • 您能否详细说明您所做的事情?谢谢。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-07-26
      • 1970-01-01
      相关资源
      最近更新 更多