C# UIViewcontroller实例获取null&;不调用文件进行查看
我正在生成一个PDF文件,在UIViewcontroller的帮助下,我调用PDF文件进行查看,但由于未知原因,我的UIViewcontroller变为nullC# UIViewcontroller实例获取null&;不调用文件进行查看,c#,xamarin,xamarin.forms,uiviewcontroller,xamarin.ios,C#,Xamarin,Xamarin.forms,Uiviewcontroller,Xamarin.ios,我正在生成一个PDF文件,在UIViewcontroller的帮助下,我调用PDF文件进行查看,但由于未知原因,我的UIViewcontroller变为null string path = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments); string filePath = Path.Combine(path,filename); //Create a file and writ
string path =
Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
string filePath = Path.Combine(path,filename);
//Create a file and write the stream into it.
FileStream fileStream = File.Open(filePath, FileMode.Create);
stream.Position = 0;
stream.CopyTo(fileStream);
fileStream.Flush();
fileStream.Close();
//Invoke the saved document for viewing
UIViewController currentController =
UIApplication.SharedApplication.KeyWindow.RootViewController;
while (currentController.PresentedViewController != null)
currentController = currentController.PresentedViewController;
UIView currentView = currentController.View;
QLPreviewController qlPreview = new QLPreviewController();
QLPreviewItem item = new QLPreviewItemBundle(filename, filePath);
qlPreview.DataSource = new PreviewControllerDS(item);
currentController.PresentViewController(qlPreview, true, null);
这是我的AppDelegate.cs文件请检查
[Register("AppDelegate")]
public partial class AppDelegate :
global::Xamarin.Forms.Platform.iOS.FormsApplicationDelegate
{
//
// This method is invoked when the application has loaded and is ready to run. In this
// method you should instantiate the window, load the UI into it and then make the window
// visible.
//
// You have 17 seconds to return from this method, or iOS will terminate your application.
//
public override bool FinishedLaunching(UIApplication app, NSDictionary options)
{
global::Xamarin.Forms.Forms.Init();
//CarouselViewRenderer.Init();
FFImageLoading.Forms.Platform.CachedImageRenderer.Init();
LoadApplication(new App());
UIApplication.SharedApplication.StatusBarHidden = true;
return base.FinishedLaunching(app, options);
}
}
参考以下代码(我通过DependencyService实现它)
注意:检查pdf的文件路径是否正确。参考以下代码(我通过DependencyService实现)
注意:检查您的pdf文件路径是否正确。您在AppDelegate.cs中设置了rootviewcontroller吗?@LucasZhang MSFT再次检查我的问题我已编辑代码您在AppDelegate.cs中设置了rootviewcontroller吗?@LucasZhang MSFT再次检查我的问题我已编辑代码它引发了类似这样的异常“您正在调用一个只能从ui线程调用的uikit方法”相同的代码和平也在其他按钮上执行,但该按钮仅查看我的pdf,但在这里它不起作用,但此代码先写入文件,然后再查看。感谢我们得到了解决方案。问题出在我们的代码中。它引发了类似这样的异常”您正在调用一个只能从ui线程调用的uikit方法“同样的代码和平也在其他按钮上执行,但该按钮仅查看我的pdf,但在这里它不起作用,但此代码先写入文件,然后再查看。感谢我们得到了解决方案。问题出在我们的代码中。
[assembly: Dependency(typeof(OpenPdfFile))]
namespace xxx.iOS
{
public class OpenPdfFile : IGetRootViewController
{
public void GetRootViewController()
{
string path = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
string filePath = Path.Combine(path,filename);
//Create a file and write the stream into it.
FileStream fileStream = File.Open(filePath, FileMode.Create);
stream.Position = 0;
stream.CopyTo(fileStream);
fileStream.Flush();
fileStream.Close();
var previewController = new QLPreviewController();
previewController.DataSource = new QuickLookSource("file://"+filePath);
UIViewController currentController =
UIApplication.SharedApplication.KeyWindow.RootViewController;
while (currentController.PresentedViewController != null)
currentController = currentController.PresentedViewController;
currentController.PresentViewController(previewController,true,null);
}
}
public class QuickLookSource : QLPreviewControllerDataSource
{
string filePath;
public QuickLookSource(string path)
{
filePath = path;
}
public override nint PreviewItemCount(QLPreviewController controller)
{
return 1;
}
public override IQLPreviewItem GetPreviewItem(QLPreviewController controller, nint index)
{
return new PreviewItem(filePath);
}
}
public class PreviewItem : QLPreviewItem
{
NSUrl fileUrl;
public override NSUrl ItemUrl => fileUrl;
public override string ItemTitle => fileUrl.LastPathComponent;
public PreviewItem(string url)
{
fileUrl = new NSUrl(url,true);
}
}
}