2017-04-19 86 views
0

我讀過this線程和一些其他類似的錯誤,但不幸的是我仍然不知道如何解決我的問題。不支持Xamarin PushAsync方法

我應該開一家ZXScannerPage,這樣我可以讀取QR碼的方法

protected override async void OnAppearing() 
    { 
     base.OnAppearing(); 

     var scanPage = new ZXingScannerPage(); 
     scanPage.OnScanResult += (result) => { 
      // Stop scanning 
      scanPage.IsScanning = false; 

      // Pop the page and show the result 
      Device.BeginInvokeOnMainThread(() => { 
       Navigation.PopAsync(); 
       DisplayAlert("Scanned Barcode", result.Text, "OK"); 
      }); 
     }; 

     // Navigate to our scanner page 
     await Navigation.PushAsync(scanPage); // Here is the error    
    } 

我需要await Navigation.PushAsync(scanPage);呼叫

MainPage = new NavigationPage(<Something goes here>); 

前使用此功能,但我不能確定,其中這應該去,我應該喂什麼參數

回答

1

PushAsync方法不支持,因爲該應用程序的MainPage不是NavigationPage

創建覆蓋OnAppearing方法的頁面。在這個方法中使用你的代碼。

當應用程序在App.xaml.cs啓動或App.cs取決於項目類型,調用構造函數

MainPage = new NavigationPage(new YourPage()); 

這將調用OnAppearing方法在自己的網頁和代碼按下掃描儀一頁。

編輯 U可以使用你的scannerPage像

var scanPage = new ZXingScannerPage(); 
scanPage.OnScanResult += (result) => { 
      // Stop scanning 
      scanPage.IsScanning = false; 

      // Pop the page and show the result 
      Device.BeginInvokeOnMainThread(() => { 
       Navigation.PopAsync(); 
       DisplayAlert("Scanned Barcode", result.Text, "OK"); 
      }); 
     }; 
MainPage = new NavigationPage(scanPage); 

在這種情況下,掃描完成Navigation.PopAsync()後,不會在導航堆棧工作,因爲只有一個頁面(除NavigationPage)。

+0

好的,謝謝,所以我將需要創建一個名爲YourPage.cs的新類/ –

+0

@BarneyCmberss是的。添加新的頁面,而不是類(或者只是創建類並從頁面繼承)。 – puko

+0

這應該是我''scanPage'這是在'var scanPage = new ZXingScannerPage();' –

0

我想你想知道如何初始化和使用導航頁面的功能,

使用PushAsync之前和PopAsync功能,你需要初始化一個新的導航頁面,在你的應用中的一些網頁。

MainPage = new NavigationPage(Something goes here);

可以使用一些基本的頁面在你的應用,即登錄頁或歡迎頁

public class App : Application 
{ 
    public App() 
    { 
    var nPage = new NavigationPage(new WelcomePage()); // or new LoginPage() 
    MainPage = nPage; 
    } 
} 

現在你已經初始化的導航頁面的一些基本頁面中設置的導航頁面在App類的主頁,您可以推送或流行其他頁面,即您的掃描頁面。