Экземпляр UIViewcontroller становится нулевым и не вызывает файл для просмотра - PullRequest
1 голос
/ 10 мая 2019

Я создаю файл 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);
    }
}

1 Ответ

0 голосов
/ 10 мая 2019

Обратитесь к следующему коду (я реализую его с помощью 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-файла.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...