【问题标题】:zxing barcode scanning fragmentzxing条码扫描片段
【发布时间】:2017-06-14 01:20:01
【问题描述】:

我对移动开发非常陌生,我正在尝试使用 Xamarin(visual studio) 制作一个 android 应用程序。我正在尝试将 zxing 条码扫描仪集成到我的应用程序中,并且我发现了许多将其集成到应用程序中的示例,但它们都在使用 Activity。我想使用片段而不是活动。我试图让我的片段和扫描屏幕出现,但它没有扫描任何东西。有人可以指出正确的方向。谢谢。

更新文件
这是我的 Fragment2.cs

public class Fragment2 : Fragment
{
    public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
    {
       var view = inflater.Inflate(Resource.Layout.MySettingsView, container, false);



        ImageButton scanBtn = view.FindViewById<ImageButton>(Resource.Id.btnScan);
        TextView results = view.FindViewById<TextView>(Resource.Id.results);

        scanBtn.Click += async (sender, e) =>
        {
            MobileBarcodeScanner.Initialize(Activity.Application);
            var scanner = new MobileBarcodeScanner();
            var result = await scanner.Scan();

            if (result != null)
            {
                return;
            }

            Console.WriteLine($"Scanned Barcode: {result}");
            Activity.RunOnUiThread(() =>
            {
                results.Text = result.Text;
            });
        };

        return view;
    }
}

【问题讨论】:

    标签: c# android xamarin xamarin.android


    【解决方案1】:

    我正在改变一些事情:

    public override View OnCreateView (LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
    {
        // you don't need this you can safely remove it.
        //var ignored = base.OnCreateView (inflater, container, savedInstanceState);
    
        // changed this line to properly inflate the fragment's layout 
        var view = inflater.Inflate (Resource.Layout.fragment2_layout, container, false);
    
        ImageButton scanBtn = view.FindViewById<ImageButton> (Resource.Id. btnScan);
        TextView results = view.FindViewById<TextView> (Resource.Id. Results);
    
        scanBtn.Click += async (sender, e) => {
    
            // you don not create a new instance of the Android Application 
           // but get the one already created. From an activity you 
          // just call `Application` but from inside a fragment you need to get 
          // the fragment's activity then get the Application.
    
            MobileBarcodeScanner.Initialize (Activity.Application);
    
            var scanner = new MobileBarcodeScanner ();
    
            var result = await scanner.Scan ();
    
            // The if was inverted.
            if (result == null)
            {
                return;
            }
    
            Console.WriteLine ($"Scanned Barcode: {result}");
    
            // Using this you are sure it will run in the UI thread
            // as you will be updating an UI element.
            Activity.RunOnUiThread (() => {
                results.Text = result.Text;
            });
    
        };
    
        return view;
    }
    

    希望这会有所帮助!

    【讨论】:

    • 感谢您的回复。我尝试了您的代码,相机启动但没有扫描任何内容。控制台窗口中也没有任何内容。
    • 你添加了相机和手电筒的权限吗?
    • 是的,我确实添加了权限。它工作了一次,它不再工作了。此外,当它确实起作用时,结果也没有显示在 textview“结果”中。
    • 使用 Fragment2 OnCreateView 方法的代码更新原始帖子。
    • 我已经使用更新的OnCreateView 方法编辑了帖子
    【解决方案2】:

    @apineda 的代码运行良好,但我遇到了自动对焦问题。经过研究,我找到了解决该问题的方法。这是我的扫描片段。

    public class Fragment2 : Fragment
    {
    public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
    {
       var view = inflater.Inflate(Resource.Layout.MySettingsView, container, false);
    
    
    
        ImageButton scanBtn = view.FindViewById<ImageButton>(Resource.Id.btnScan);
        TextView results = view.FindViewById<TextView>(Resource.Id.results);
    
        scanBtn.Click += async (sender, e) =>
        {
            MobileBarcodeScanner.Initialize(Activity.Application);
            var scanner = new MobileBarcodeScanner();
            var result = (ZXing.Result)null;
    
            result = await scanner.Scan();
    
            if (result == null)
            {
                scanner.AutoFocus();
                return;
            }
    
            Console.WriteLine($"Scanned Barcode: {result}");
            Activity.RunOnUiThread(() =>
            {
                results.Text = result.Text;
            });
        };
    
        return view;
    }
    }
    

    【讨论】: