2016-10-05 6 views
0

После обновления устройства до iOS 10 QLPreviewController остановился, чтобы правильно отобразить документы. Он показывает белый экран. Я извлек образец сценария из приложения.Xamarin QLPreviewController + NavigationPage, поврежденный на iOS 10

Он содержит одну страницу, с двумя кнопками, которые должны загрузить два документа:

<?xml version="1.0" encoding="utf-8"?> 
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms" 
     xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" 
     xmlns:local="clr-namespace:QuickLookIOS10Test" 
     x:Class="QuickLookIOS10Test.QuickLookIOS10TestPage"> 
    <StackLayout Orientation="Vertical"> 
     <Button Text="Load first doc" Clicked="OnLoadFirstClicked"/> 
     <Button Text="Load second doc" Clicked="OnLoadSecondClicked"/> 
     <Button Text="Navigate forward" Clicked="OnForwardClicked"/> 

     <local:QLDocumentView 
      x:Name="DocumentView" 
      BackgroundColor="Silver" 
      HorizontalOptions="FillAndExpand" 
      VerticalOptions="FillAndExpand"/> 
    </StackLayout> 
</ContentPage> 

где:

public class QLDocumentView : View 
{ 
    public static readonly BindableProperty FilePathProperty = 
     BindableProperty.Create(nameof(FilePath), typeof(string), typeof(QLDocumentView), null); 

    public string FilePath 
    { 
     get { return (string)GetValue(FilePathProperty); } 
     set { SetValue(FilePathProperty, value); } 
    } 
} 

Существует обычай визуализатор участие:

public class QLDocumentViewRenderer : ViewRenderer<QLDocumentView, UIView> 
{ 
    private QLPreviewController controller; 

    public override SizeRequest GetDesiredSize(double widthConstraint, double heightConstraint) 
    { 
     //This is a fix to prevent incorrect scaling after rotating from portrait to landscape. 
     //No idea why does this work :(Bug #101639 
     return new SizeRequest(Size.Zero, Size.Zero); 
    } 

    protected override void OnElementChanged(ElementChangedEventArgs<QLDocumentView> e) 
    { 
     base.OnElementChanged(e); 

     if (Control == null) 
     { 
      controller = new QLPreviewController(); 
      SetNativeControl(controller.View); 
     } 

     RefreshView(); 
    } 

    protected override void OnElementPropertyChanged(object sender, 
                System.ComponentModel.PropertyChangedEventArgs e) 
    { 
     base.OnElementPropertyChanged(sender, e); 

     if (e.PropertyName == QLDocumentView.FilePathProperty.PropertyName) 
     { 
      RefreshView(); 
     } 
    } 

    private void RefreshView() 
    { 
     DisposeDataSource(); 

     if (Element?.FilePath != null) 
     { 
      controller.DataSource = new DocumentQLPreviewControllerDataSource(Element.FilePath); 
     } 

     controller.ReloadData(); 
    } 

    protected override void Dispose(bool disposing) 
    { 
     base.Dispose(disposing); 

     if (disposing) 
     { 
      DisposeDataSource(); 
      DisposeController(); 
     } 
    } 

    private void DisposeDataSource() 
    { 
     var dataSource = controller.DataSource; 
     controller.DataSource = null; 
     dataSource?.Dispose(); 
    } 

    private void DisposeController() 
    { 
     controller?.Dispose(); 
     controller = null; 
    } 

    private class DocumentQLPreviewControllerDataSource : QLPreviewControllerDataSource 
    { 
     private readonly string fileName; 

     public DocumentQLPreviewControllerDataSource(string fileName) 
     { 
      this.fileName = fileName; 
     } 

     public override nint PreviewItemCount(QLPreviewController controller) 
     { 
      return 1; 
     } 

     public override IQLPreviewItem GetPreviewItem(QLPreviewController controller, nint index) 
     { 
      NSUrl url = NSUrl.FromFilename(fileName); 
      return new QlItem(url); 
     } 

     private sealed class QlItem : QLPreviewItem 
     { 
      private readonly NSUrl itemUrl; 

      public QlItem(NSUrl uri) 
      { 
       itemUrl = uri; 
      } 

      public override string ItemTitle { get { return string.Empty; } } 

      public override NSUrl ItemUrl { get { return itemUrl; } } 

      protected override void Dispose(bool disposing) 
      { 
       base.Dispose(disposing); 

       if (disposing) 
       { 
        this.itemUrl?.Dispose(); 
       } 
      } 
     } 
    } 
} 

Если приложение настраивает основную страницу, как показано ниже:

MainPage = new NavigationPage(new QuickLookIOS10TestPage()); 

он работает на прошивке 9.3, но не на прошивке 10. Если удалить NavigationPage:

MainPage = new QuickLookIOS10TestPage(); 

он работает на оба версиях IOS.

Код за нажатием кнопки просто устанавливает свойство FilePath элемента управления.

Sample app demonstrating the problem

Xamarin Forms 2.3.2.127

Xamarin студия 6.1.1 (сборка 15)

+0

Если вы думаете, это используется для работы на прошивке 9 и теперь не больше на 10 прошивке, вы должны, вероятно, сообщить им об этом на bugreport.apple.com! – Raffael

+0

Я до сих пор не знаю, является ли это iOS, Xamarin или моей проблемой кода рендеринга. Я не могу воспроизвести его с помощью Swift и XCode – aguyngueran

ответ

0

Я столкнулся с той же проблемой. Похоже, something was changed or even broken в QuickLook в iOS10, но решение довольно простое:

public class PdfViewerControlRenderer : ViewRenderer<PdfViewerControl, UIView> 
{ 
    private readonly bool IsOniOS10; 

    private UIViewController _controller; 
    private QLPreviewController _qlPreviewController; 

    public PdfViewerControlRenderer() 
    { 
     IsOniOS10 = UIDevice.CurrentDevice.CheckSystemVersion(10, 0); 
    } 

    protected override void OnElementChanged(ElementChangedEventArgs<PdfViewerControl> e) 
    { 
     if (e.NewElement != null) 
     { 
      _controller = new UIViewController(); 
      _qlPreviewController = new QLPreviewController(); 

      //... 
      // Set QuickLook datasource here 
      //... 

      if (!IsOniOS10) 
      { 
       _controller.AddChildViewController(_qlPreviewController); 
       _controller.View.AddSubview(_qlPreviewController.View); 
       _qlPreviewController.DidMoveToParentViewController(_controller); 
      } 
      SetNativeControl(_controller.View); 
     } 
    } 

    public override void LayoutSubviews() 
    { 
     base.LayoutSubviews(); 
     _controller.View.Frame = Bounds; 
     _controller.View.AutoresizingMask = UIViewAutoresizing.FlexibleWidth | UIViewAutoresizing.FlexibleHeight; 
     _qlPreviewController.View.Frame = Bounds; 
     if (IsOniOS10) 
     { 
      _controller.View.AddSubview(_qlPreviewController.View); 
      _qlPreviewController.DidMoveToParentViewController(_controller); 
     } 
    } 
} 

Результат: QuickLook on iOS10

+0

Вот трюк, который мне нужен :) – aguyngueran

Смежные вопросы