是否有可能在自己的构造函数和自己的类方法中夸大Xamarin中的CustomLayout:FrameLayout?

时间:2014-10-14 08:37:44

标签: android xamarin custom-controls layout-inflater android-framelayout

这是我想膨胀的例子......它只是一个FrameLayout而且我有布局的axml文件......我可以从这个FrameLayout(一个片段)的父级中扩充布局......我想要这个自定义FrameLayout在其他片段中。在方法initialize()中,编译器说错误CS1604:无法分配给''因为它是只读的(CS1604)(MyProject.Mono)然后错误CS0266:无法隐式转换类型' Android.Widget.FrameLayout' to' MyProject.Mono.ViewArtistAlbumTrack'。存在显式转换(您是否缺少演员?)(CS0266)(Navi.Mono)

     /// <summary>
     /// View artist album track.
     /// This framelayout is especific for ...
     /// </summary>
       public class ViewArtistAlbumTrack : FrameLayout

{
    /// <summary>
    /// The adapter.
    /// </summary>
    public ExpandableAATitemAdapter adapter = null;
    /// <summary>
    /// a plain ExpandableListView
    /// </summary>
    public ExpandableListView expandable = null; 
    /// <summary>
    /// View that triggers something sliding up
    /// </summary>
    public Button albumdown = null; 
    public Button trackdown = null; 

    /// <summary>
    /// The list just AAT items. AAT is for Artist Album Track 
    /// </summary>
    public List<AATitem> list = null; 


    public ViewArtistAlbumTrack (Context context) :
    base (context)
    {
        this.trackdown = new Button(this.Context); 
        this.albumdown = new Button(this.Context);
        this.expandable = new ExpandableListView(this.Context); 
        Initialize ();


    }

    public ViewArtistAlbumTrack (Context context, IAttributeSet attrs) :
    base (context, attrs)
    {
        this.trackdown = new Button(this.Context); 
        this.albumdown = new Button(this.Context);
        this.expandable = new ExpandableListView(this.Context); 

        Initialize ();
    }

    public ViewArtistAlbumTrack (Context context, IAttributeSet attrs, int defStyle) :
    base (context, attrs, defStyle)
    {

        Initialize ();
    }


    public void Initialize ()
    {
        try
        {
            LayoutInflater inflater =     (LayoutInflater)this.Context.GetSystemService(Context.LayoutInflaterService);
            this = (FrameLayout)inflater.Inflate(Resource.Layout.ViewAAT, null);


            Console.WriteLine("done succesfully: "+ System.Reflection.MethodBase.GetCurrentMethod().DeclaringType.ToString()+ "  " 
                + System.Reflection.MethodBase.GetCurrentMethod().Name.ToString() ); 
        }
        catch(Exception exception)
        {
            Console.WriteLine ("exception at "
                + System.Reflection.MethodBase.GetCurrentMethod().DeclaringType.ToString()+ "  " 
                + System.Reflection.MethodBase.GetCurrentMethod().Name.ToString() + " : "
                + exception.Source.ToString() );
            Console.WriteLine ("exception at "
                + System.Reflection.MethodBase.GetCurrentMethod().DeclaringType.ToString()+ "  " 
                + System.Reflection.MethodBase.GetCurrentMethod().Name.ToString()+ " : "
                + exception.Message.ToString() );
            Console.WriteLine ("exception at "
                +  System.Reflection.MethodBase.GetCurrentMethod().DeclaringType.ToString()+ "  " 
                + System.Reflection.MethodBase.GetCurrentMethod().Name.ToString()+ " : "
                + exception.StackTrace.ToString() );
            Console.WriteLine ("exception at " 
                + System.Reflection.MethodBase.GetCurrentMethod().DeclaringType.ToString()+ "  " 
                + System.Reflection.MethodBase.GetCurrentMethod().Name.ToString()+ " : "
                + exception.TargetSite.ToString() );
        }// end of catch()

        //this.expandable.SetAdapter(new ExpandableAATitemAdapter(Application.Context, this.list));
    }

    protected override void  OnFinishInflate() 
    {
        try
        {

            this.trackdown = new Button(this.Context); 

            this.albumdown = new Button(this.Context);

            this.expandable = new ExpandableListView(this.Context); 

            this.expandable = FindViewById<ExpandableListView> (Resource.Id.aatexpandableid); 
            this.trackdown  = FindViewById<Button> (Resource.Id.aattracksbid);
            this.albumdown  = FindViewById<Button> (Resource.Id.aatabumsbid);

            this.trackdown.Visibility = ViewStates.Gone; 
            this.albumdown.Visibility = ViewStates.Gone; 
            Console.WriteLine("done succesfully: "+ System.Reflection.MethodBase.GetCurrentMethod().DeclaringType.ToString()+ "  " 
                + System.Reflection.MethodBase.GetCurrentMethod().Name.ToString() ); 

        }
        catch(Exception exception)
        {
            Console.WriteLine ("exception at "
                + System.Reflection.MethodBase.GetCurrentMethod().DeclaringType.ToString()+ "  " 
                + System.Reflection.MethodBase.GetCurrentMethod().Name.ToString() + " : "
                + exception.Source.ToString() );
            Console.WriteLine ("exception at "
                + System.Reflection.MethodBase.GetCurrentMethod().DeclaringType.ToString()+ "  " 
                + System.Reflection.MethodBase.GetCurrentMethod().Name.ToString()+ " : "
                + exception.Message.ToString() );
            Console.WriteLine ("exception at "
                +  System.Reflection.MethodBase.GetCurrentMethod().DeclaringType.ToString()+ "  " 
                + System.Reflection.MethodBase.GetCurrentMethod().Name.ToString()+ " : "
                + exception.StackTrace.ToString() );
            Console.WriteLine ("exception at " 
                + System.Reflection.MethodBase.GetCurrentMethod().DeclaringType.ToString()+ "  " 
                + System.Reflection.MethodBase.GetCurrentMethod().Name.ToString()+ " : "
                + exception.TargetSite.ToString() );
        }// end of catch()

    }

}

1 个答案:

答案 0 :(得分:1)

我认为你的问题可能在于这一行:

 this = (FrameLayout)inflater.Inflate(Resource.Layout.ViewAAT, null);

这不是将布局加载到视图中的正确方法。如果您的视图是带有一些子视图的自定义FrameLayout,那么您可以将xml扩展为FrameLayout并引用其中的视图,例如。

var frame = (FrameLayout)inflater.Inflate(Resource.Layout.ViewAAT, null);
this.trackdown = frame.FindViewById<Button>(...);
// find your other views here