2017-12-11 14 views
0

xamarin.forms를 사용하고 있습니다. Android와 IOS의 두 가지 프로젝트가 있습니다.contentpage에서 contentview의 스택 레이아웃에 컨트롤을 추가하는 방법은 무엇입니까?

나는 코드

내가 디자인 타임에 ContentPage에서 내용 없음의 "SL"라는 StackLayout에 컨트롤을 추가 할
<ContentView.Content> 
    <StackLayout x:Name="slMain" Padding="1" BackgroundColor="#7ABA45"> 
     <StackLayout VerticalOptions="FillAndExpand" Padding="0,0,20,0" HeightRequest="50" HorizontalOptions="FillAndExpand"> 
      <Label x:Name="lblTitle" VerticalOptions="CenterAndExpand" /> 
     </StackLayout> 
     <StackLayout x:Name="sl" IsVisible="False" BackgroundColor="White"> 
     </StackLayout> 
    </StackLayout> 
</ContentView.Content> 


// In Behind code(.cs file) 

    public ExpandCollapseStackLayout() 
    { 
     InitializeComponent(); 
     var tapGestureRecognizer = new TapGestureRecognizer(); 
     tapGestureRecognizer.Tapped += (s, e) => 
     { 
      sl.IsVisible = !sl.IsVisible; 
     }; 
     lblTitle.GestureRecognizers.Add(tapGestureRecognizer); 
     slMain.GestureRecognizers.Add(tapGestureRecognizer); 
    } 

    public string Title 
    { 
     get 
     { 
      return lblTitle.Text; 
     } 
     set 
     { 
      lblTitle.Text = value; 
     } 
    } 

을 다음과 같이 한 내용 없음 페이지가 있습니다. 런타임에 추가하고 싶지 않습니다.

어떻게 컨트롤을 Contentview stacklayout에 추가 할 수 있습니까? 런타임에 컨트롤을 추가 할 경우, 당신은 C#으로 페이지 뒤에 코드를 사용하여 할 필요가

<StackLayout x:Name="sl" IsVisible="False" BackgroundColor="White"> 
    <!-- Add here your controls --> 
    <Label ... /> 
    <Button .. /> 
</StackLayout> 

:

+0

해결책을 찾았습니까? <레이블 텍스트 : 나는 콘텐츠 페이지 이 같이 썼다 동일한 요청 –

답변

0

당신은 단순히 당신의 XAML 예에서 그들을 선언 설계 시간에 컨트롤을 추가하려면 . 예 : 내용 없음에 대한

void AddControl() 
{ 
    var btn = new Button(); 
    sl.Children.Add(btn); 
} 
+0

이 = "테스트"> 하지만 나에게 적절한 예를 제공하십시오 작동하지 않습니다. –

+0

많은 세부 사항을 제공하지 않기 때문에 왜 작동하지 않는지 이해하기 어렵습니다. 와일드 샷. IsVisible = "False"로 설정하면 IsVisible = "True"로 변경됩니다. 코드 뒤에서 sl 컨트롤을 숨기거나 제거하지 말고 그 위에 다른 컨트롤이 없는지 확인하십시오. – EvZ

+0

코드를 편집 중입니다. 전체 정보 제공 –

0

코드 : 내용 없음에 대한

<ContentView.Content> 
    <StackLayout x:Name="slMain" Padding="1" BackgroundColor="#7ABA45" > 
     <StackLayout VerticalOptions="FillAndExpand" Padding="0,0,10,0" HeightRequest="50" HorizontalOptions="FillAndExpand" Orientation="Horizontal"> 
      <Label x:Name="lblTitle" VerticalOptions="CenterAndExpand" HorizontalOptions="FillAndExpand" Margin="10,0,0,0" TextColor="White" FontAttributes="Bold" /> 
      <Label Text="{ x:Static local:GrialShapesFont.ArrowDown }" HorizontalOptions="End" VerticalOptions="CenterAndExpand" TextColor="White" Style="{ StaticResource FontIconBase }" FontSize="26" /> 
     </StackLayout> 
     <Frame Padding="10" IsVisible="False" BackgroundColor="White" x:Name="ContentFrame" OutlineColor="Black" HasShadow="False"> 
     </Frame> 
    </StackLayout> 
</ContentView.Content> 

CS 코드 :

[ContentProperty("ContainerContent")] 
public partial class ExpandCollapseStackLayout : ContentView 
{ 
    public ExpandCollapseStackLayout() 
    { 
     InitializeComponent(); 
     var tapGestureRecognizer = new TapGestureRecognizer(); 
     tapGestureRecognizer.Tapped += (s, e) => 
     { 
      ContentFrame.IsVisible = !ContentFrame.IsVisible; 
     }; 
     lblTitle.GestureRecognizers.Add(tapGestureRecognizer); 
     slMain.GestureRecognizers.Add(tapGestureRecognizer); 
    } 

    public View ContainerContent 
    { 
     get { return ContentFrame.Content; } 
     set { ContentFrame.Content = value; } 
    } 
    public string Title 
    { 
     get 
     { 
      return lblTitle.Text; 
     } 
     set 
     { 
      lblTitle.Text = value; 
     } 
    } 
} 

ContentPage에 컨트롤 추가 :

<control:ExpandCollapseStackLayout Title="Demo" Padding="0,10,0,0"> 
       <control:ExpandCollapseStackLayout.ContainerContent > 
        <StackLayout> 
         <Label Text="Add Content Here"></Label> 
        </StackLayout> 
       </control:ExpandCollapseStackLayout.ContainerContent> 
</control:ExpandCollapseStackLayout>