zxing barcode scanning fragment

Viewed 1034

I am very new to mobile development and I trying to make an android app using Xamarin(visual studio). I am trying to integrate zxing barcode scanner into my app and I have found many examples of it's integration into app but they are all using Activity. I want to use fragments instead of Activity. I tried to make my fragment and the scanning screen do come up but it is not scanning anything. Can someone please point into the right direction. Thank you.

Updated file
Here is my 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;
    }
}
2 Answers
Related