如何将动态元素添加到现有表单

时间:2017-12-28 20:52:17

标签: c# xamarin xamarin.ios

我想在Xamarin中向现有表单添加其他元素,并更改现有表单上的现有元素。以下是需要在按钮Name = "GenerateBtn"

之前添加到现有表单的元素的代码
Entry accNumber = new Entry()
{
   Text = "number"
}; 

我可以专门添加到RelativeLayout还是StackLayout足够?这是xaml文件:

    <StackLayout 
            x:name = "First"
            VerticalOptions ="Center" HorizontalOptions="Center">

       <Entry
            Text="First name"/>
        <Entry
            Text="Last name"/>

          <RelativeLayout
                x:Name = "cardInfo"
                VerticalOptions ="Center" HorizontalOptions="Center"
                RelativeLayout.YConstraint="{ConstraintExpression Type=RelativeToParent, Property=Y, Factor=0, Constant=220}" 
                HeightRequest="60" 
                RelativeLayout.WidthConstraint="{ConstraintExpression Type=RelativeToParent, Property=Width, Factor=1}" >

         <Button 
            x:Name = "Account"
            Text="account" 
            Clicked="Handle_Clicked"
            HeightRequest="50"
            WidthRequest="120"
            RelativeLayout.YConstraint="{ConstraintExpression Type=RelativeToParent, Property=Y, Factor=0, Constant=5}" 
            RelativeLayout.XConstraint="{ConstraintExpression Type=RelativeToParent, Property=X, Factor=0, Constant=5}"/> 

          <Button
            x:Name = "Btn2"
            Text="button2" 
            Clicked="Handle_Clicked"
            HeightRequest="50"
            WidthRequest="120"
            RelativeLayout.YConstraint="{ConstraintExpression Type=RelativeToParent, Property=Y, Factor=0, Constant=5}" 
            RelativeLayout.XConstraint="{ConstraintExpression Type=RelativeToParent, Property=Width, Factor=0.5, Constant=5}" />

       </RelativeLayout>
        <Button 
            x:Name = "GenerateBtn"
            Text="Generate" 
            Clicked="Handle_Clicked"/>

     </StackLayout>

1 个答案:

答案 0 :(得分:0)

您需要先将Button(Name = "GenerateBtn")包裹在StackLayout内,然后将Entry作为子视图添加到Stacklayout中。

在Xmal

<StackLayout x:Name="stack" Orientation="Horizontal">
    <Button  x:Name = "GenerateBtn" Text="Generate"/>
</StackLayout>

背后的代码

stack.Children.Insert(0,new Entry { Text = "number" });