2017-12-26 1 views
-1

C# Xamarin 프로젝트에서 작업 중입니다. 이 프로젝트에서는 QR 코드 스캐너를 구현하기 위해 ZXing.Net.Mobile 클래스 라이브러리를 사용했습니다.스캔이 완료된 후 Xamarin ZXing 스캐너 쓰기 로직

사용자가 QR 코드를 스캔하면 URL이 표시됩니다. 어떤 웹 서비스에 데이터를 보내는 데 사용됩니다. 내 문제는 : 방법의 중간에 connectToBackend 스레드 BeginInvokeOnMainThread 만료됩니다. 결과적으로 connectToBackend 실행이 완료되지 않습니다. 내 서버 요청을 처리 할 수 ​​있도록이 스레드 시나리오를 처리하는 방법에 대한 도움이 필요합니다.

public void ShowScannerPage() { 

     ZXingScannerPage scanPage = new ZXingScannerPage(); 

     scanPage.OnScanResult += (result) => { 

      // stop scanning 
      scanPage.IsScanning = false; 

      ZXing.BarcodeFormat barcodeFormat = result.BarcodeFormat; 
      string type = barcodeFormat.ToString(); 


    // This thread finishes before the method, connectToBackend, inside has time to finish 
      Xamarin.Forms.Device.BeginInvokeOnMainThread(() => { 

       //_pageService.PopAsync(); 
       _pageService.PopAsync(); 


       App.UserDialogManager.ShowAlertDialog("The Barcode type is : " + type, "The text is : " + result.Text, " OK"); 

     // problem here 
       connectToBackend(result.Text); 
      }); 

    // If I put connectToBackend here there is still a problem 

     }; 

     _pageService.PushAsync(scanPage); 

    } 

자세한 정보를 제공하기 위해 MVVM 접근 방식을 사용하고 있습니다. 페이지가 있고 사용자가 스캔 버튼을 클릭하면 ShowScannerPage 메서드는 ZXing.Net 모바일 라이브러리를 사용하여 모바일에서 스캐너보기를 엽니 다. 아래에 수업을 붙여 놨어.

public class WorkoutViewModel { 

    public ICommand ScanCommand { get; private set; } 

    public readonly IPageService _pageService; 

    public WorkoutViewModel(IPageService pageService) { 

     this._pageService = pageService; 

     ScanCommand = new Command(ShowScannerPage); 

    } 

    public void ShowScannerPage() { 

     ZXingScannerPage scanPage = new ZXingScannerPage(); 

     scanPage.OnScanResult += (result) => { 

      // stop scanning 
      scanPage.IsScanning = false; 

      ZXing.BarcodeFormat barcodeFormat = result.BarcodeFormat; 
      string type = barcodeFormat.ToString(); 


    // This thread finishes before the method, connectToBackend, inside has time to finish 
      Xamarin.Forms.Device.BeginInvokeOnMainThread(() => { 

       //_pageService.PopAsync(); 
       _pageService.PopAsync(); 


       App.UserDialogManager.ShowAlertDialog("The Barcode type is : " + type, "The text is : " + result.Text, " OK"); 

     // problem here 
       connectToBackend(result.Text); 
      }); 

    // If I put connectToBackend here there is still a problem 

     }; 

     _pageService.PushAsync(scanPage); 

    } 

    public async void connectToBackend(String nodes) { 

     // call api ... 

    } 

}

답변

0

당신은 await 키워드가없는 비동기 기능 (async void connectToBackend(String nodes))를 호출한다.

"async"이벤트를 scanPage.OnScanResult += (result) => {으로 정의하면 asyncconnectToBackend 기능을 사용할 수 있다고 생각합니다.

생각해 보면 BeginInvokeOnMainThread은 필요하지 않습니다.

관련 문제