【问题标题】:How to add control in stacklayout of contentview in contentpage?如何在 contentpage 的 contentview 的 stacklayout 中添加控件?
【发布时间】:2018-05-24 19:33:33
【问题描述】:

我正在使用 xamarin.forms。有两个项目 Android 和 IOS。

我有一个包含以下代码的 ContentView 页面

<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;
        }
    }

我想在设计时在 ContentPage 的内容视图的 StackLayout 中添加名为“sl”的控件。 我不想在运行时添加

请建议我如何在 Contentview stacklayout 中添加控件?

【问题讨论】:

  • 您找到解决方案了吗?我也有同样的要求

标签: c# xamarin xamarin.ios xamarin.forms xamarin.android


【解决方案1】:

如果您想在设计时添加控件,只需在 XAML 中声明它们,例如:

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

如果您想在运行时添加控件,则需要使用 C# 中的代码隐藏页面来完成。示例:

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

【讨论】:

  • 我在内容页面中这样写 但它不起作用请给我正确的例子。
  • 很难理解为什么它不起作用,因为您没有提供太多细节。狂拍。您将 IsVisible="False" 更改为 IsVisible="True"。确保您没有隐藏/删除代码中的 sl 控件,并确保其顶部没有其他控件。
  • 调试代码并检查事件是否触发两次,为什么要在不同的元素上添加两次相同的手势识别器?
【解决方案2】:

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>

ContentView 的 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>

【讨论】:

    猜你喜欢
    • 2023-02-14
    • 2016-10-18
    • 1970-01-01
    • 2017-03-04
    • 2020-11-28
    • 1970-01-01
    • 2016-12-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多