扫描程序为空且无法访问



我正在尝试在Xamarin.Forms中读取QRCode。 我在XF中有一个共享项目。 我已经为 ZXing.Net 添加了 nuget 包。 一切都在iOS项目中工作。 我在安卓项目中遇到错误。 我通过Android SDK监视器收到的错误表明存在扫描程序为空且无法访问的问题。 我猜我在Android方面没有正确设置一些东西。 有人在我的代码中看到任何不当之处吗? 谢谢你的时间。

ScanPage class:
public class ScanPage : ContentPage
{
    ZXing.Net.Mobile.Forms.ZXingScannerView zxing;
    ZXingDefaultOverlay overlay;
    bool isConnected = false;
    string basicUrl = "golfeventscores.azurewebsites.net";
    public ScanPage ()
    {
        zxing = new ZXing.Net.Mobile.Forms.ZXingScannerView
        {
            HorizontalOptions = LayoutOptions.FillAndExpand,
            VerticalOptions = LayoutOptions.FillAndExpand,
            AutomationId = "zxingScannerView",
        };
        zxing.OnScanResult += async (ZXing.Result result) => {
            zxing.IsAnalyzing = false;
            zxing.IsScanning = false;
            var teamToken = result.Text;
            //MessagingCenter.Send<string>(teamToken, "SelectTeamMembers");
            isConnected = await Plugin.Connectivity.CrossConnectivity.Current.IsRemoteReachable(basicUrl);
            if (isConnected)
            {
                await GetTeamData(teamToken);
            }
            else
            {
                await DisplayAlert("Connectivity", "There is a problem with internet connectivity. Please try and reload this screen.", "Ok");
            }
        };
        overlay = new ZXingDefaultOverlay
        {
            TopText = "Hold your phone up to the barcode",
            BottomText = "Scanning will happen automatically",
            ShowFlashButton = zxing.HasTorch,
            AutomationId = "zxingDefaultOverlay",
        };
        overlay.FlashButtonClicked += (sender, e) => {
            zxing.IsTorchOn = !zxing.IsTorchOn;
        };
        var grid = new Grid
        {
            VerticalOptions = LayoutOptions.FillAndExpand,
            HorizontalOptions = LayoutOptions.FillAndExpand,
        };
        grid.Children.Add(zxing);
        grid.Children.Add(overlay);
        // The root page of your application
        Content = grid;
    }
    protected override void OnAppearing()
    {
        base.OnAppearing();
        zxing.IsScanning = true;
    }
    protected override void OnDisappearing()
    {
        zxing.IsScanning = false;
        base.OnDisappearing();
    }
    async System.Threading.Tasks.Task GetTeamData(string Token)
    {
        try
        {
            var scanResult = await WebServices.ws.TokenLookup(Token);
            if (scanResult.Result == true)
            {
                if (scanResult.IsScoreBoard == true)
                {
                    var uri = new System.Uri(scanResult.ScoreboardUrl);
                    Device.BeginInvokeOnMainThread(() =>
                    {
                        Device.OpenUri(uri);
                        Navigation.PopToRootAsync();
                    });
                }
                if (scanResult.IsCharity == true)
                {
                    if (scanResult.TeamPlayers.Count > 0)
                    {
                        var player = scanResult.TeamPlayers.First();
                        var playerId = player.PlayerTeamId;
                        var urlResult = await WebServices.ws.ServerUrl(Token, playerId);
                        if (urlResult.ValidRequest && (!String.IsNullOrEmpty(urlResult.Url)))
                        {
                            var uri = new System.Uri(urlResult.Url);
                            Device.OpenUri(uri);
                            await Navigation.PopToRootAsync();
                        }
                    }
                    else{
                        await DisplayAlert("Scanning", "There was a problem downloading the Charity Team Info.", "OK");
                    }
                }
                else
                {
                    if (scanResult.IsLargeGame != true)
                    {
                        var select = new Pages.SelectTeamMembers(Token);
                        await Navigation.PushAsync(select);
                    }
                    else
                    {
                        await DisplayAlert("Large Game", "Don't have the large team game setup with scanning.", "Ok");
                    }
                }
            }
            else
            {
                await DisplayAlert("Server Problem", "There was some type of server error. Please try again or call Wally.", "Ok");
            }
        }
        catch(System.Exception sysExc)
        {
            //nothing seems to be caught
        }
    }
}
MainActivity.cs contents:
[Activity (Label = "TD Scan", Icon = "@drawable/icon", Theme="@style/MainTheme", MainLauncher = true, ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation)]
public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsAppCompatActivity
{
    protected override void OnCreate (Bundle bundle)
    {
        TabLayoutResource = Resource.Layout.Tabbar;
        ToolbarResource = Resource.Layout.Toolbar; 
        base.OnCreate (bundle);
        global::Xamarin.Forms.Forms.Init (this, bundle);
        ZXing.Net.Mobile.Forms.Android.Platform.Init();
        LoadApplication (new GolfGameScanApp.App ());
    }
    public override void OnRequestPermissionsResult(int requestCode, string[] permissions, Permission[] grantResults)
    {
        ZXing.Net.Mobile.Android.PermissionsHandler.OnRequestPermissionsResult(requestCode, permissions, grantResults);
    }
}

您是否在 Android Project 中定义了所有内容?

Xamarin Forms

对于 Xamarin 窗体,需要更多的设置。您需要在特定于平台的应用项目中初始化每个平台上的库。

人造人

在 Android 上,在主活动的 OnCreate (..) 实现中,调用:

ZXing.Net.Mobile.Forms.Android.Platform.Init();

ZXing.Net.Mobile for Xamarin.Forms 也为你处理新的 Android 权限请求模型,但你还需要将以下覆盖实现添加到你的主活动中:

public override void OnRequestPermissionsResult(int requestCode, string[] permissions, Permission[] grantResults)
{
    global::ZXing.Net.Mobile.Forms.Android.PermissionsHandler.OnRequestPermissionsResult (requestCode, permissions, grantResults);           
}

相机权限应该自动包含在Android清单中.xml但是如果你想使用Torch API,你仍然需要自己添加手电筒权限。可以使用以下程序集级别属性来执行此操作:

[assembly: UsesPermission (Android.Manifest.Permission.Flashlight)]

相关内容

  • 没有找到相关文章

最新更新