2016-09-22 9 views
1

This docをレンダリングしていないが、私は、ドキュメントごとにしようとしたのAndroid、iOS搭載などのカスタムPageRendererを作成する方法について説明しますが、それはAndroidのために働いていない理由を知りません。ただし、iOSでは動作します。Xamarin.Forms AndroidのカスタムContentPage PageRendererがビュー

共有ContentPageクラス:Android用

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

カスタムPageRendererクラス:

[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に移動すると3つのラジオボタンが表示(レンダリング)されるラジオグループビューです。しかし、現在のところ、LabelはContentPage(つまり "Hello ContentPage")からレンダリングされ、他のビューは表示されません(またはプラットフォーム固有のカスタムSecondPageRendererクラスからレンダリングされます)。 – user2618875

+0

Androidでは、ページレンダラーはビューグループに過ぎません。カスタムビュー、何が起こるかを見てください – 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をオーバーライドする必要があるのか​​説明できますか? Docsはそれについて何も言及していません(または私がよくわからない他のレシピで説明されるかもしれません)。 BTW OnLayoutは期待通りに仕事をします。 – user2618875

+0

@ user2618875、私は確かに言えません。私はまた、その動作がデフォルトになることを期待しています。私は多くのサンプルで同様の取り扱いを見つけました。例えば。 [Blogpost](http://blog.masterdevs.com/xf-day-10/) – Florian

関連する問題