2017-12-11 15 views
0

私はxamarin.formsを使用しています。 AndroidとIOSの2つのプロジェクトがあります。コンテンツページのcontentviewのstacklayoutにコントロールを追加するには?

私は、設計時にContentPageでcontentviewの「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; 
     } 
    } 

を以下で1つのContentViewページを持っています。 実行時に追加したくない

どうすればコントロールをContentview stacklayoutに追加できますか?

+0

あなたは解決策を見つけましたか? <ラベルテキスト:私は、コンテンツページ でこのように書いた同じ要求 –

答えて

0

あなたがデザイン時にコントロールを追加したい場合は、単にあなたのXAMLでそれらを宣言し、例:

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

あなたは、実行時にコントロールを追加したい場合は、C#でのページの後ろにあなたのコードを使用してそれを実行する必要があります。例:ContentViewため

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

を持っています= "テスト"> しかし、 は私に適切な例を提供してください動作しません。 –

+0

多くの詳細を提供していないため、なぜ機能していないのか分かりません。野生のショット。 IsVisible = "False"にすると、IsVisible = "True"に変更されます。コードの背後にあるslコントロールを非表示にしたり、他のコントロールがないことを確認してください。 – EvZ

+0

私のコードを編集しています。完全な情報を提供する –

0

コード:ContentViewため

<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> 
関連する問題