XAML 通过Xamarin代码在运行时创建网格

6mw9ycah  于 2023-01-10  发布在  其他
关注(0)|答案(1)|浏览(102)

我有代码作为每个附件,建立一个Xamrin网格虚拟代码。问题是,我还没有能够解决的是事件,将被触发时,输入数据到一个单元格。
下面是输出:Output 1Output 2
这篇文章的目的是创建一个事件,当数据输入到网格中运行时创建的任何单元格时,可以调用该事件来执行操作。

<?xml version="1.0" encoding="utf-8" ?>
    <ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
                 xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
                 x:Class="VarGridSample.MainPage">

        <ScrollView>
            <StackLayout
                Spacing="25"
                Padding="30,0"
                VerticalOptions="Center">

                <Label
                    Text="Variable Grid"
                    FontSize="32"
                    HorizontalOptions="Center" />

                <Label
                    Text="I am attempting to build a Grid at runtime to allow for variable rows and          columns. The Grid has been initially defined in the Content Page. The Grid is built in the code based on the number of rows and columns requested. Entry controls are built for each cell. The   problem is how to build the Events for each cell...ANY IDEAS?"
                    FontSize="18"
                    HorizontalOptions="Center" />

                <Entry x:Name="Rows"
                    Text="{Binding Rows }"
                    HorizontalOptions="Center" Placeholder="Enter Total Rows" WidthRequest="150" />
                <Entry x:Name="Cols"
                    Text="{Binding Rows }"
                    HorizontalOptions="Center" Placeholder="Enter Total Columns" WidthRequest="150"  />
                <Button x:Name="BuildGrid"
                    Text="Build Grid" HorizontalOptions="Center" Clicked="BuildGrid_Clicked" />
                <Grid x:Name="VarGrid">

                </Grid>
            </StackLayout>
        </ScrollView>
    </ContentPage>
    • 以下是C#代码**
using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using Xamarin.Forms;

    namespace VarGridSample
    {
        public partial class MainPage : ContentPage
       {
            private int rows { get; set; }
            private int cols { get; set; }
            private int cellcntr = 0;
            public MainPage()
            {
                InitializeComponent();
            }

            private void BuildGrid_Clicked(object sender, EventArgs e)
            {

                rows = Int32.Parse(Rows.Text);
                cols = Int32.Parse(Cols.Text);
                for (int rowIndex = 0; rowIndex < rows; rowIndex++)
                {
                    VarGrid.RowDefinitions.Add(new RowDefinition());
                }
                for (int rowIndex = 0; rowIndex < rows; rowIndex++)
                {
                    for (int colIndex = 0; colIndex < cols; colIndex++)
                    {
                        var entry = new Entry
                        {
                        
                            Text = "cell" + (cellcntr).ToString(),
                            VerticalOptions = LayoutOptions.Center,
                            HorizontalOptions = LayoutOptions.Center
                         };
                        VarGrid.Children.Add(entry, colIndex, rowIndex);
                        cellcntr++;
                    }

                }
            }
        }
    }
l0oc07j2

l0oc07j21#

您可以将其添加到MainPage.xaml.cs:

private void BuildGrid_Clicked(object sender, EventArgs e)
        {
            rows = Int32.Parse(Rows.Text);
            cols = Int32.Parse(Cols.Text);
            for (int rowIndex = 0; rowIndex < rows; rowIndex++)
            {
                VarGrid.RowDefinitions.Add(new RowDefinition());
            }
            for (int rowIndex = 0; rowIndex < rows; rowIndex++)
            {
                for (int colIndex = 0; colIndex < cols; colIndex++)
                {
                    var entry = new Entry
                    {
                        Text = "cell" + (cellcntr).ToString(),
                        VerticalOptions = LayoutOptions.Center,
                        HorizontalOptions = LayoutOptions.Center,
                    };
                    
                    //add the event
                    entry.TextChanged += Entry_TextChanged;

                    VarGrid.Children.Add(entry, colIndex, rowIndex);
                    cellcntr++;
                }
           }
        }

        private void Entry_TextChanged(object sender, TextChangedEventArgs e)
        {
            Console.WriteLine(e.OldTextValue);
            var entry = (Entry)sender;
            var parent= (Grid)entry.Parent;
            Console.WriteLine(parent.Children.Count);
        }

相关问题