2016-10-05 12 views
0

デバイスをiOS 10にアップデートした後、ドキュメントを正しく表示するためにQLPreviewControllerが停止しました。白い画面が表示されます。 私はアプリからサンプルシナリオを抽出しました。Xamarin QLPreviewController + NavigationPageがiOS 10で破損しました

これは、2つの異なるドキュメントをロードする必要があり二つのボタンを備えた単一のページが含まれます。

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(); 
       } 
      } 
     } 
    } 
} 

の場合:

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); } 
    } 
} 

が関与カスタムレンダラがあります:

<?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> 

をアプリケーションの設定メインページは以下のようになります。

MainPage = new NavigationPage(new QuickLookIOS10TestPage()); 

それはiOSの9.3上で作業を行いますが、iOSの10に私はNavigationPageを削除しない場合は、次の

MainPage = new QuickLookIOS10TestPage(); 

それは両方のiOSバージョンで動作します。

ボタンのクリックのためのコードは、コントロールのFilePathプロパティを設定するだけです。

Sample app demonstrating the problem

Xamarinは、私は同じ問題に直面してきた2.3.2.127

Xamarin Studioの6.1.1(15ビルド)

+0

あなたは、これはiOSの9上で動作するために使用さだと思うし、今のiOS 10の上にもうない場合は、あなたはおそらく彼らにbugreport.apple.comを知らせるべきです! – Raffael

+0

iOS、Xamarin、またはレンダラーコードの問題があるかどうかはまだ分かりません。私はそれを再現することはできませんSwiftとXcode – aguyngueran

答えて

0

フォーム。これは、iOS10でクイックルックでsomething was changed or even brokenように見えるが、解決策は非常に簡単です:

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

関連する問題