2014-09-05 2 views
4

내 앱에서는 Xamarin.FormsAbsoluteLayout을 사용합니다. 맞춤 메뉴 막대가 있습니다. 메뉴 버튼을 클릭하면 내 AbsoluteLayout 인 주 콘텐츠 (View)가 대체되어야합니다.Xamarin.Forms의 AbsoluteLayout에서 어린이를 제거하는 방법은 무엇입니까?

지금까지는 새로운 자식을 추가하고 Children.Add()SetLayBounds()을 사용하여 레이아웃 경계를 설정함으로써 달성 할 수있었습니다. 그러나 이런 식으로 저는 점점 더 많은 아이들을 추가하고 결코 그들을 제거하지 않을 것입니다.

AbsoluteLayout에서 어린이를 삭제하는 적절한 방법은 무엇입니까?

  • layout.Children.Clear()
  • 당신이 알고 제공,

    • layout.Children.RemoveAt (position),
    • layout.Children.Remove (view) :

    답변

    9

    .ChildrenIList<View> (그리고 ICollection<View>, IEnumerable<View>, Ienumerable)를 구현 그래서 당신은 당신의 가장 편리하게 사용할 수 있습니다로보기의 색인

    layout.Children[position] = new MyView(); 
    

    을하지만 그건 Children.Add (...) 무시보다 당신에게 더 적은 옵션을 제공하고 SetLayoutBoundsSetLayoutFlags를 사용해야합니다 :, 당신은 또한 위치에있는 요소를 교체 할 수 있습니다.

    +0

    감사합니다. :-디 – kaolick

    0

    AbsoluteLayout.Children 컬렉션의 RemoveAt 메서드를 사용하는 다음 스 니펫을 시도해보십시오.

    또는 변수 참조가있는 경우 제거 (보기) 방법을 사용할 수 있습니다.

     StackLayout objStackLayout = new StackLayout() 
         { 
         }; 
         // 
         AbsoluteLayout objAbsoluteLayout = new AbsoluteLayout()    
         { 
         }; 
         // 
         BoxView objBox1 = new BoxView() 
         { 
          Color = Color.Red, 
          WidthRequest = 50, 
          HeightRequest = 50, 
         }; 
         objAbsoluteLayout.Children.Add(objBox1, new Point(100,100)); 
         System.Diagnostics.Debug.WriteLine("Children Count : " + objAbsoluteLayout.Children.Count); 
         // 
         BoxView objBox2 = new BoxView() 
         { 
          Color = Color.Green, 
          WidthRequest = 50, 
          HeightRequest = 50, 
         }; 
         objAbsoluteLayout.Children.Add(objBox2, new Point(200, 200)); 
         System.Diagnostics.Debug.WriteLine("Children Count : " + objAbsoluteLayout.Children.Count); 
         // 
         Button objButton1 = new Button() 
         { 
          Text = "Remove First Child" 
         }; 
         objButton1.Clicked += ((o2, e2) => 
          { 
           if (objAbsoluteLayout.Children.Count > 0) 
           { 
            // To Remove a View at a specific index use:- 
            objAbsoluteLayout.Children.RemoveAt(0); 
            // 
            DisplayAlert("Children Count", objAbsoluteLayout.Children.Count.ToString(), "OK"); 
           } 
           else 
           { 
            DisplayAlert("Invalid", "There are no more children that can be removed", "OK"); 
           } 
          }); 
    
         // 
         objStackLayout.Children.Add(objAbsoluteLayout); 
         objStackLayout.Children.Add(objButton1); 
    
    관련 문제