如何使用Xamarin表单以编程方式在现有网格上添加按钮或标签?

时间:2018-05-16 09:19:51

标签: c# wpf xaml xamarin xamarin.forms

我是Xamarin Form的新手。我的要求是。我想根据现有Grid(如下面的代码)Xamarin.Form的要求,动态地添加按钮或类似自定义按钮或标签等(代码隐藏)。但我不知道在现有Grid上添加项目的方法。我已经尝试过很多样本但没有成功。

我的XAML代码如下..

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
                     xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
                      xmlns:Status="clr-namespace:SourceCode.Mobile.UI.StatusDetails"                
                     x:Class="SourceCode.Mobile.UI.ConsumableScreen">
    <ContentPage.Content>

        <Grid x:Name="MainGrid" Padding="0" Margin="0,0,0,0" RowSpacing="1" ColumnSpacing="1" BackgroundColor="White">
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="*"></ColumnDefinition>
                <ColumnDefinition Width="*"></ColumnDefinition>
                <ColumnDefinition Width="*"></ColumnDefinition>
            </Grid.ColumnDefinitions>

            <!--   These below button should be from code behind dynamically -->

            <Button  Grid.Row="0" Grid.Column="0" Text="Device A" WidthRequest="150" HeightRequest="50" HorizontalOptions="Center" VerticalOptions="Center" BackgroundColor="White" Clicked="Button_1_Clicked"></Button>
            <Button  Grid.Row="0" Grid.Column="1" Text="Device B" WidthRequest="150" HeightRequest="50" HorizontalOptions="Center" VerticalOptions="Center" BackgroundColor="White" Clicked="Button_2_Clicked"></Button>
            <Button  Grid.Row="0" Grid.Column="2" Text="Device C" WidthRequest="150" HeightRequest="50" HorizontalOptions="Center" VerticalOptions="Center" BackgroundColor="White" Clicked="Button_3_Clicked"></Button>
        </Grid>

    </ContentPage.Content>
</ContentPage>

背后的代码

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using Xamarin.Forms;
using Xamarin.Forms.Xaml;

namespace SourceCode.Mobile.UI
{
    [XamlCompilation(XamlCompilationOptions.Compile)]
    public partial class ConsumableScreen : ContentPage
    {            

        public ConsumableScreen( )
        {
            InitializeComponent();  
        }   

    }
}

任何人都可以帮助我如何在代码背后的Button现有单元格上创建Label或任何其他控件,例如TextBoxGrid等。

提前致谢。

Susheel

3 个答案:

答案 0 :(得分:1)

试试这个:

Button button = new Button();
button.Text = "Device B";
button.Clicked = Button_2_Clicked;
button.WidthRequest = 150.0;
button.HeightRequest = 50.0;
button.HorizontalOptions = Xamarin.Forms.LayoutOptions.Center;
button.VerticalOptions = Xamarin.Forms.LayoutOptions.Center;
button.Color = Xamarin.Forms.Color.White;
Grid.SetRow(button, 0);
Grid.SetColumn(button, 1);
MainGrid.Children.Add(button);

答案 1 :(得分:0)

将此代码添加到例如xaml.cs类的构造函数

MainGrid.Children.Add(new Label()
        {
            Text = "My new label"
        },0,0);

0,0结尾是网格中的列数和行数。

MainGrid是Grid的名称,您在xaml文件中添加了

更多信息请参见文档:https://docs.microsoft.com/en-au/xamarin/xamarin-forms/creating-mobile-apps-xamarin-forms/summaries/chapter17

答案 2 :(得分:0)

您也可以使用此标签为现有网格添加标签

var circle = new Konva.Circle({
    clearBeforeDraw: true,
    x: 75,
    y: 75,
    radius: 10,
    fill: "red",
    stroke: "black",
    strokeWidth: 4,
    containment: rect,
    draggable: true,
    name: "fillShape",
    dragBoundFunc: function(pos) {
        const x = Math.min(250-12, Math.max(pos.x, 150+12));
        const y = Math.min(200-12, Math.max(pos.y, 150+12));
        return {x, y};
   }
});
相关问题