这是我想充气的示例...它只是一个FrameLayout,并且我有Layout axml文件...我可以从这个FrameLayout的父级(一个片段)充气布局...我想要这个Custom FrameLayout在其他片段中。在方法initialize()中,编译器显示错误CS1604:无法分配给”,因为它是只读的(CS1604)(MyProject.Mono),然后出现错误CS0266:无法将类型“ Android.Widget.FrameLayout”隐式转换为“ 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()

    }


}

最佳答案

我认为您的问题可能与以下内容有关:

 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

关于android - 是否可以在Xamarin中以自己的构造函数和类方法为CustomLayout:FrameLayout充气?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/26356214/

10-09 06:05
查看更多