2017-06-21 9 views
0

午後SO。XAMLのAndroidビューの作成と使用

私はこの質問への答えがそこにあると確信しています - しかし、私は**** ingの手掛かりがありません。

以前はAndroidプラットフォームとWindowsプラットフォーム用に開発しましたが、Xamarin.Formsを使用してXAMLからAndroidレイアウト/アクティビティをどのように参照できるかについてはわかりません。 ネイティブコントロールとサブクラスコントロールを使用している例が見つかりましたが、XAMLがビュー全体を参照するように異なる概念を結びつけているようです。任意の助けをいただければ幸いです

...

using Android.Content; 
using Android.Runtime; 
using Android.Util; 
using Android.Views; 

namespace PlatformControls.Droid 
{ 
    [Register("xxxxControl")] 
    public class xxxxControl : View 
    { 
     Context mContext; 

     #region Private Members 
     #endregion 

     #region Constructors 

     public xxxxControl(Context context) 
      : base(context) 
     { 
      init(context); 
      LayoutInflater mInflator = LayoutInflater.From(context); 
      View layout = mInflator.Inflate(Resource.Layout.GanttCellControlLayout, null, false); 
      // cannot addView() !? 
      // dont know what to do - other than attempt to drown myself in the toilet sink 
     } 

     public xxxxControl(Context context, IAttributeSet attrs) 
      : base(context, attrs) 
     { 
      init(context); 
     } 

     public xxxxControl(Context context, IAttributeSet attrs, int d) 
      : base(context) 
     { 
      init(context); 

     } 

     #endregion 

     #region Public Properties 
     #endregion 

     #region Methods 
     #endregion 

     private void init(Context context) 
     { 
      mContext = context; 
     } 
    } 
} 

答えて

1

たぶんカスタムレンダラを必要とするために使用されるが、それはもう今のネイティブの埋め込みが可能であることの場合ではないようです。 このようなソリューションは、上記のコードでは、オブジェクトMYVIEWの種類を正確に非常に明白ではないようです何これに対する相対新人として言えば

<?xml version="1.0" encoding="UTF-8"?> 
<ContentView xmlns="http://xamarin.com/schemas/2014/forms" 
      xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" 
      xmlns:androidLocal="clr-namespace:NativeControls.Droid;assembly=NativeControls.Droid;targetPlatform=Android" 
      xmlns:formsAndroid="clr-namespace:Xamarin.Forms;assembly=Xamarin.Forms.Platform.Android;targetPlatform=Android" 
      x:Class="Wrappers.CP.CustomControlWrapper"> 
    <ContentView.Content> 
     <androidLocal:MyView x:Arguments="{x:Static formsAndroid:Forms.Context}" 
       Text ="I am rendered by an Android custom control"/> 
    </ContentView.Content> 
</ContentView> 

可能です。 これは、LinearLayoutから継承する必要があると思われる回答です。この

// Get the UI controls from the loaded layout: 
    Inflate(Context, Resource.Layout.MyView, this); 

    this.myText = FindViewById<TextView>(Resource.Id.SomeContent); 

    // Add controls to the layout here if needed 
    var layout = FindViewById<LinearLayout>(Resource.Id.MyView_outerlayout); 

よう そして、コード何かがレイアウトのコントロールへの参照を取得するか、追加のコントロールを追加することができるようにレイアウトを取得するには、上記の例ではinit関数に追加することができます。

関連する問題