2016-09-22 1 views
1

This doc은 Android, iOS 등을 위해 맞춤형 PageRenderer를 만드는 방법을 설명합니다. 문서별로 시도했지만 Android 용으로 작동하지 않는 이유는 알 수 없습니다. 그러나 iOS에서 작동합니다. 안드로이드에 대한Xamarin.Forms Android 사용자 정의 ContentPage PageRenderer보기를 렌더링하지 않습니다.

public class SecondPage : ContentPage 
    { 
     public SecondPage() 
     { 
      Content = new StackLayout 
      { 
       Children = { 
        new Label { Text = "Hello ContentPage" } 
       } 
      }; 
     } 
    } 

사용자 정의 PageRenderer 클래스 :

공유 ContentPage 클래스

[assembly: ExportRenderer(typeof(SecondPage), typeof(SecondPageRenderer))] 
namespace RenderFormsTest.Droid 
{ 
    public class SecondPageRenderer : PageRenderer 
    { 
     protected override void OnElementChanged(ElementChangedEventArgs<Page> e) 
     { 
      base.OnElementChanged(e); 

      if (e.OldElement != null || Element == null) 
      { 
       return; 
      } 

      InitView(); 
     } 

     void InitView() 
     { 
      var context = Forms.Context; 
      string[] os = { "Android", "iOS", "Windows" }; 
      var ll = new LinearLayout(context); 
      ll.LayoutParameters = new LayoutParams(LayoutParams.MatchParent, LayoutParams.MatchParent); 
      ll.SetBackgroundColor(Android.Graphics.Color.IndianRed); 

      var rg = new RadioGroup(context); 
      for (int index = 0; index < os.Length; index++) 
      { 
       rg.AddView(new RadioButton(context) { Text = os[index] }); 
      } 

      ll.AddView(rg); 

      AddView(ll); 
     } 
    } 
} 

당신이 무엇이 잘못되었는지 말해 주실 래요?

+0

실제로 어떤 일이 발생했는지, 실제로 어떤 일이 일어 났는지 알려주세요. –

+0

** @ GeraldVersluis ** 예상 결과는 SeconPage로 이동할 때 세 개의 라디오 버튼이 표시 (표시)되어야하는 RadioGroup보기입니다. 하지만 현재는 "Hello ContentPage"와 같은 ContentPage에서 렌더링 된 Label을 볼 수 있으며 다른 뷰는 표시되지 않습니다 (또는 플랫폼 별 사용자 정의 SecondPageRenderer 클래스에서 렌더링 됨). – user2618875

+0

페이지 렌더러는 뷰 그룹 일 뿐이며, 사용자 정의보기, 어떤 일이 발생하는지 확인하십시오. – Bonelol

답변

2

비슷한 문제가있었습니다. 레이아웃이 잘못되었습니다. PageRenderer.OnLayout에 설정해보세요.

[assembly: ExportRenderer(typeof(SecondPage), typeof(SecondPageRenderer))] 
namespace RenderFormsTest.Droid 
{ 
    protected Android.Views.View NativeView; 

    public class SecondPageRenderer : PageRenderer 
    { 
     protected override void OnElementChanged(ElementChangedEventArgs<Page> e) 
     { 
      base.OnElementChanged(e); 

      if (e.OldElement != null || Element == null) 
      { 
       if (Element == null) 
        NativeView = null; 

       return; 
      } 

      InitView(); 
     } 

     void InitView() 
     { 
      var context = Forms.Context; 
      string[] os = { "Android", "iOS", "Windows" }; 
      var ll = new LinearLayout(context); 
      ll.LayoutParameters = new LayoutParams(LayoutParams.MatchParent, LayoutParams.MatchParent); 
      ll.SetBackgroundColor(Android.Graphics.Color.IndianRed); 

      var rg = new RadioGroup(context); 
      for (int index = 0; index < os.Length; index++) 
      { 
       rg.AddView(new RadioButton(context) { Text = os[index] });  
      } 

      ll.AddView(rg); 

      AddView(ll); 

      NativeView = ll; 
     } 

     protected override void OnLayout(bool changed, int l, int t, int r, int b) 
     { 
      base.OnLayout(changed, l, t, r, b); 

      if (NativeView != null) 
      { 
       var msw = MeasureSpec.MakeMeasureSpec(r - l, MeasureSpecMode.Exactly); 
       var msh = MeasureSpec.MakeMeasureSpec(b - t, MeasureSpecMode.Exactly); 

       NativeView.Measure(msw, msh); 
       NativeView.Layout(0, 0, r - l, b - t); 
      } 
     } 
    } 
} 
+0

** @ Florian **, 왜 OnLayout을 재정의해야하는지 설명 할 수 있습니까? 문서 도구에 대한 내용은 언급되어 있지 않습니다. 또는 확실치 않은 다른 방법으로 설명 될 수 있습니다. BTW OnLayout이 예상대로 작업을 수행합니다. – user2618875

+0

@ user2618875, 나는 확실히 말할 수 없다. 나는 또한 그 행동이 디폴트가 될 것으로 기대할 것이다. 나는 많은 샘플에서 비슷한 취급을 발견했다. 예 : [블로그 게시물] (http://blog.masterdevs.com/xf-day-10/) – Florian

관련 문제