在Xamarin.Forms中使用zxing.net.mobile进行扫描时立即导航

时间:2016-09-27 12:39:40

标签: c# xamarin.forms zxing xamarin-studio

我在这里发布我的完整页面,如下所示。这将打开扫描仪并扫描完成的页面(首次成功扫描尝试)将调用Web服务。在调用Web服务时,我需要调用PopAsync()方法后退一步。我的问题是,在下面的代码中,因为我开始扫描大部分时间扫描程序扫描多次,而Web服务也调用了那么多时间。我想在单次扫描时调用一次Web服务并导航回来。

public partial class ComingIn : ContentPage
 {
  ZXingScannerView zxing;
  ZXingDefaultOverlay overlay;

  public ComingIn ():base()
  {
   ColorResources._isBusy = true;
   ActivityIndicator indicator = new ActivityIndicator 
   {
    Color=Color.Red,
    IsRunning=false,
    IsVisible=false,
   };

   zxing = new ZXingScannerView {
    HorizontalOptions = LayoutOptions.FillAndExpand,
    VerticalOptions = LayoutOptions.FillAndExpand
   };

   zxing.OnScanResult += (result) => 
    Device.BeginInvokeOnMainThread (async () => 
     {
      //get geolocation
      try{
       var locator = CrossGeolocator.Current;
       locator.DesiredAccuracy = 50;
       var position = await locator.GetPositionAsync  (timeoutMilliseconds: 1500);

       comingIn_goingOut obj=new comingIn_goingOut();
       obj.empId=ColorResources.LIVE_USER_ID;
       obj.qrValue=result.Text;
       obj.latitude=position.Latitude.ToString();
       obj.longitude=position.Longitude.ToString();
       // Stop analysis until we navigate away so we don't keep reading barcodes
       zxing.IsAnalyzing = false;
       zxing.IsScanning=false;
       try 
       {
        var json = JsonConvert.SerializeObject (obj);
        var content = new StringContent (json, Encoding.UTF8, "application/json");
        HttpClient Client = new HttpClient ();
        HttpResponseMessage response = null;
        response = await Client.PostAsync (baseUrl+"comingIn", content);
        if (response.IsSuccessStatusCode) 
        {      
         var responseContent = await response.Content.ReadAsStringAsync ();
         var res = JsonConvert.DeserializeObject<string> (responseContent);
         await DisplayAlert("Message",res.ToString(),"OK");
         ColorResources.Already_In=true;
         ColorResources.Already_Out=false;
         await this.Navigation.PopAsync();
        }

       }
       catch(Exception ex)
       {
        await DisplayAlert("Alert",ex.ToString(),"OK");
       }
     }
     catch(Exception ex)
     {
          DispalyAlert("Alert",ex.ToString(),"OK");
     }
     });

   overlay = new ZXingDefaultOverlay 
   {    
    TopText = "Hold your phone up to the barcode",
    BottomText = "Scanning will happen automatically",
    ShowFlashButton = zxing.HasTorch,
   };

   overlay.FlashButtonClicked += (sender, e) => 
   {
    zxing.IsTorchOn = !zxing.IsTorchOn;
   };
   var grid = new Grid 
   {
    VerticalOptions = LayoutOptions.FillAndExpand,
    HorizontalOptions = LayoutOptions.FillAndExpand,
    HeightRequest=500,
   };
   grid.Children.Add (zxing);
   grid.Children.Add (overlay);


   Content = new StackLayout {
    Padding = new Thickness (0, 15, 0, 10), 
    VerticalOptions = LayoutOptions.Center,
    Children = {
     new Label {
      Text = "QR Scanner",
      TextColor=Color.Black,
      FontSize = 25,
      VerticalOptions = LayoutOptions.StartAndExpand,
      HorizontalOptions = LayoutOptions.Center,
     },
     indicator,
     grid
    }
   };
  }
}

0 个答案:

没有答案