2017-10-07 160 views
0

我試圖讀取Xamarin.Forms中的QRCode。我在XF有一個共享的項目。我爲ZXing.Net添加了nuget包。一切工作在iOS項目中。我在Android項目中遇到錯誤。我通過Android SDK Monitor獲取的錯誤,表明掃描器爲空並且無法訪問時出現問題。我猜測,有些東西我沒有在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); 
    } 
} 

回答

0

您是否定義了所有Android項目?

Xamarin表格

對於Xamarin表單,需要更多的設置。您需要在平臺特定的應用程序項目中的每個平臺上初始化庫。

的Android

在Android上,在您的主要活動的OnCreate(..)實現,調用:

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

ZXing.Net.Mobile爲Xamarin.Forms還負責將新的Android許可請求模型但您需要將以下覆蓋實施添加到您的主要活動中:

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

應該自動包含相機權限對於AndroidManifest.xml中的你,如果你想使用Torch API,你仍然需要自己添加Flashlight權限。您可以使用以下裝配級別屬性來執行此操作:

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

謝謝。是的,所有這些都已完成。我把它放在我粘貼出來的示例代碼之上。 :-)請保留可能出現的問題! 它也覺得有可能是我在錯誤的線程上做的事情,但不能完全找到它。我以前有過一堆Device.BeginInvokeOnMainThread調用,但它對我沒有任何幫助。 –

+0

所以,我回去工作了。它似乎是,如果你嘗試在這個OnScanResult方法中做一些處理工作,那麼點擊用戶界面,無論什麼東西在Android上發生,導致組件崩潰。我回去並通過MessageCenter實施了一些消息傳遞以開始處理,並且這似乎導致事情按預期工作。我昨天在IM上與Redth談了一點,但他現在處於空閒狀態。當他回來時,我會看看他是否知道任何事情。在此之前,我似乎現在正在工作。 –

+0

感謝您的想法@Alessandro。他們真的幫助。 :-) –