UIViewcontroller 实例获取 null 并且不调用文件进行查看
UIViewcontroller instance getting null & not invoking the file for view
我正在生成一个 PDF 文件,并在 UIViewcontroller 的帮助下调用 PDF 文件进行查看,但由于某些未知原因,我的 UIViewcontroller 变为空。
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实现的)
[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);
}
}
}
注意: 检查你的pdf文件路径是否正确。
我正在生成一个 PDF 文件,并在 UIViewcontroller 的帮助下调用 PDF 文件进行查看,但由于某些未知原因,我的 UIViewcontroller 变为空。
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实现的)
[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);
}
}
}
注意: 检查你的pdf文件路径是否正确。