2017-03-16 144 views
2

我想做一個簡單的應用程序來測試打印WebView頁面。Xamarin Android沒有這樣的方法WebView.CreatePrintDocumentAdapter

下面是我的簡單代碼

WebView webView = new WebView(this); 
webView.LoadUrl("https://www.google.com"); 
PrintDocumentAdapter adapter = webView.CreatePrintDocumentAdapter("test"); 
PrintManager printMgr = (PrintManager)GetSystemService(PrintService); 
printMgr.Print("printTest", adapter, null); 
myWebView = webView; 

然而,可以在WebView.CreatePrintDocumentAdapter("test")方法,這將在下面的書面生成異常。

Java.Lang.NoSuchMethodError: no method with name='createPrintDocumentAdapter' signature='(Ljava/lang/String;)Landroid/print/PrintDocumentAdapter;' in class Landroid/webkit/WebView; 

請注意,我已經在Android清單中包含了Internet和網絡狀態權限。這是什麼造成的?如果此異常阻止它,我如何創建打印功能?

,我使用的設備是三星SM-G7102(Android 4.4的 - API 19)

任何幫助,將不勝感激。

回答

3

CreatePrintDocumentAdapter(String documentName)在API中加入21

您可以在運行時做一個VERSION.SdkInt檢查,並調用相應的方法:

if (Android.OS.Build.VERSION.SdkInt >= Android.OS.BuildVersionCodes.Lollipop) 
    adapter = webView.CreatePrintDocumentAdapter("test"); 
else 
    adapter = webView.CreatePrintDocumentAdapter(); 

重:https://developer.android.com/reference/android/print/PrintDocumentAdapter.html

+0

我不能添加這些,在' else'塊抱怨說'WebView.CreatePrintDocumentAdapter()已經過時:'deprecated''。任何解決方案? –

+1

@DarrenChristopher檢查項目/應用程序API級別設置什麼:https://developer.xamarin.com/guides/android/application_fundamentals/understanding_android_api_levels/ – SushiHangover

+0

我只是改變了編譯的Android版本Android 4.4系統,並保留其他默認。現在,如果塊是一個抱怨。如何克服這個版本問題? –