MVVMCross ZXing后退按钮

时间:2017-02-22 12:48:19

标签: xamarin xamarin.android mvvmcross zxing

使用zxing条形码扫描仪时,MVVMCross的后退按钮出现问题。不幸的是,当我按下后退键时,出现错误:Java.Lang.NullPointerException:尝试在空对象引用上调用虚方法'long android.graphics.Paint.getNativeInstance()'

当我评论metohs scan()时,我们一切都很好。 有人知道出了什么问题吗?

这是我的片段扫描视图类:

Keyboards > Properties > Events Tab > Information section

在我的viewmdoel中,我有一个简单的方法来关闭当前的viewmodel:

  public class ScannView :  MvxFragmentActivity, IBarcodeFragmentOptions
{
    protected ScannViewModel MainViewModel
    {
        get { return ViewModel as ScannViewModel; }
    }
   public static ZXingScannerFragment scanFragment;
    protected override void OnResume()
    {
        base.OnResume();
        try
        {
            if (scanFragment == null)
            {
                scanFragment = new ZXingScannerFragment();

                SupportFragmentManager.BeginTransaction()
                    .Replace(Resource.Id.frameScanner, scanFragment)
                    .Commit();
            }
            scan();
        }
        catch (Exception ex)
        {
            Debug.WriteLine(ex);
        }
    }

    protected override void OnPause()
    {
        try
        {


            scanFragment?.StopScanning();
            base.OnPause();
        }catch(Exception ex)
        {
            Console.WriteLine(ex);
        }
    }
    protected override void OnRestart()
    {
        base.OnRestart();
    }
    public void ToogleFlashLight(bool on)
    {
        if (scanFragment != null)
            scanFragment.SetTorch(on);
    }

   public void scan()
    {

        try
        {
            //  var results = await CrossPermissions.Current.RequestPermissionsAsync(Plugin.Permissions.Abstractions.Permission.Camera);
            //  var status = results[Plugin.Permissions.Abstractions.Permission.Camera];
            //   if (status == Plugin.Permissions.Abstractions.PermissionStatus.Granted)
            //   {
            var opts = new MobileBarcodeScanningOptions
            {
                PossibleFormats = new List<ZXing.BarcodeFormat> {
                    ZXing.BarcodeFormat.QR_CODE
                },
                CameraResolutionSelector = availableResolutions => {

                    foreach (var ar in availableResolutions)
                    {
                        Console.WriteLine("Resolution: " + ar.Width + "x" + ar.Height);
                    }
                    return null;
                }
            };
            scanFragment?.StartScanning(opts,result =>
               {
                   if (result == null || string.IsNullOrEmpty(result.Text))
                   {
                       RunOnUiThread(() => Toast.MakeText(this, "Anulowanie skanowanie", ToastLength.Long).Show());
                       return;
                   }
                   MainViewModel.ScannedCode = result.Text;  //ChangePropertyToEmpty();
                   RunOnUiThread(() => Toast.MakeText(this, "Zeskanowano: " + result.Text, ToastLength.Short).Show());

               });
          //  }
        }catch(Exception ex)
        {
            Debug.WriteLine(ex);
        }
    }
    protected override void OnViewModelSet()
    {
        MobileBarcodeScanner.Initialize(Application);
        base.OnViewModelSet();
        SetContentView(Resource.Layout.layout_scann);

    }

}

0 个答案:

没有答案