2017-03-16 79 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系统,并保留其他默认。现在,如果块是一个抱怨。如何克服这个版本问题? –