MAUI .NullReferenceException (Native iOS QLPreviewController)

652 views Asked by At

I am having a problem running after through the QLPreviewController interface.

view.PushViewController(previewController, true);

QLPreviewController starts up and displays what it should. At this point,

UIApplication.Main(args, null, typeof(AppDelegate));

throws an error


{System.NullReferenceException: Object reference not set to an instance of an object.
at Microsoft.Maui.Controls.Handlers.Compatibility.NavigationRenderer.SecondaryToolbar.LayoutToolbarItems(NFloat toolbarWidth, NFloat toolbarHeight, NFloat padding)
at Microsoft.Maui.Controls.Handlers.Compatibility.NavigationRenderer.SecondaryToolbar.LayoutSubviews()
at UIKit.UIApplication.UIApplicationMain(Int32 argc, String\[\] argv, IntPtr principalClassName, IntPtr delegateClassName)
at UIKit.UIApplication.Main(String\[\] args, Type principalClass, Type delegateClass)
at MAUISample.Program.Main(String\[\] args) in ...MAUISampleProject}

Does anyone know how to fix it or what I might be doing wrong ?

Greetings Matlas

I also tried to open the pdf file through UIDocumentInteractionController. The problem looks the same the file opens but the same error is thrown.

2

There are 2 answers

0
code_disciple On BEST ANSWER

Referencing this question solved the issue for me for MAUI iOS .NET 8.0.1: How to find current UIViewController in Xamarin

public async Task<string> SaveFileAsync(byte[] content, string localFilename)
{
   string documentsPath = Environment.GetFolderPath(Environment.SpecialFolder.Personal);
   string filePath = Path.Combine(documentsPath, localFilename);
   await File.WriteAllBytesAsync(filePath, content); // writes to local storage
   return filePath;
}

public void OpenPdf(string filePath)
{
    string documentsPath = Environment.GetFolderPath(Environment.SpecialFolder.Personal);
    var PreviewController = UIDocumentInteractionController.FromUrl(NSUrl.FromFilename(filePath));
    var vc = GetTopViewController();
    PreviewController.Delegate = new PDFViewer(vc);
    PreviewController.PresentPreview(true);
}

public static UIViewController GetTopViewController()
{
    var window = UIApplication.SharedApplication.KeyWindow;
    var vc = window.RootViewController;
    while (vc.PresentedViewController != null)
        vc = vc.PresentedViewController;
    if (vc is UINavigationController navController)
        vc = navController.ViewControllers.Last();
    return vc;
}

public class PDFViewer : UIDocumentInteractionControllerDelegate
{
    UIViewController ownerVC;

    public PDFViewer(UIViewController vc)
    {
        ownerVC = vc;
    }

    public override UIViewController ViewControllerForPreview(UIDocumentInteractionController controller)
    {
        return ownerVC;
    }

    public override UIView ViewForPreview(UIDocumentInteractionController controller)
    {
        return ownerVC?.View ?? new UIView();
    }
}
1
Brady Guy On

I have a similar problem and discovered that the root cause was actually in appshell.xml.cs I had to step through the debugger using step into. I would have thought that the exception would have been thrown in appshell and lead me directly there but I didn't discover this until I followed the code by stepping into everything. The problem is related to my code but the exception location is misleading or it was for me.