1

다음은 내가 팽창시키고 싶은 예제입니다. 그냥 FrameLayout이고 Layout axml 파일이 있습니다 ...이 FrameLayout (단편)의 부모로부터 레이아웃을 부 풀릴 수 있습니다 ... 이걸 원해요. 다른 조각의 사용자 정의 FrameLayout. 메서드 initialize()에서 컴파일러는 오류 CS1604 : 읽기 전용 (CS1604) (MyProject.Mono)이고 오류 CS0266 : 암시 적으로 'Android.Widget.FrameLayout'형식을 'MyProject .Mono.ViewArtistAlbumTrack '. 명시 적 변환이 존재한다 (당신은 캐스트를 누락?) (CS0266) (Navi.Mono)Xamarin에서 CustomLayout : FrameLayout을 자체 생성자로 확장하고 클래스 메서드를 소유 할 수 있습니까?

 /// <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() 

    } 

}

+0

XML에서 'FrameLayout' 또는 사용자 정의 유형으로 정의하고 있습니까? 내 axml에 – ScouseChris

+0

을 사용자 정의 유형 (ViewArtistAlbumTrack)으로 저장 한 다음 나중에 ViewArtistAlbumTrack으로 캐스팅하려고하는 FrameLayout으로 사용합니다. – carlesls

답변

1

을 나는 당신의 문제는이 라인을 것 같아요 :

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

그 레이아웃을 레이아웃에로드하는 올바른 방법이 아닙니다. 보기가 일부 하위보기가있는 사용자 정의 FrameLayout 인 경우 FrameLayout으로 XML을 확장하고 그 안에있는보기에 대한 참조를 가져올 수 있습니다.

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

그게 옳은 대답을 해결 ...하지만 그것은 더 많은 레이아웃과 전망을 만들 강제로 ... 그것은 또 다른 옵션입니다. – carlesls

+0

보기가 어쨌든 만들어 질 것입니다.이 설정을 유지하는 것이 가장 좋습니다. 여분의'FrameLayout' 인스턴스를 원하지 않는다면 뷰를 팽창 할 때'this'를 컨테이너로 사용할 수도 있습니다. – ScouseChris