识别以xamarin形式自动生成行

时间:2019-10-07 04:44:12

标签: c# visual-studio xamarin xamarin.forms xamarin.android

我创建了一个应用程序,当用户输入完成(在数字小键盘中)时,该应用程序会动态(自动)创建行。第一行是以静态方式定义的,将根据第一行中的预定义列自动创建行后面的代码,然后自动创建各列。Xaml代码如下。

<StackLayout>
                <Label Text="SALES ORDER" FontSize="Medium" HorizontalTextAlignment="Center" TextColor="Black" BackgroundColor="White" FontAttributes="Bold" VerticalOptions="Center"></Label>
            </StackLayout>
            <Grid x:Name="thegrid" RowSpacing="7" ColumnSpacing="1" BackgroundColor="White">
                <Grid.RowDefinitions>
                    <RowDefinition Height="10" />
                    <RowDefinition Height="50" />
                    <RowDefinition Height="50" />
                    <RowDefinition Height="50" />
                    <RowDefinition Height="50" />
                    <RowDefinition Height="50" />
                    <RowDefinition Height="50" />
                    <RowDefinition Height="10" />
                </Grid.RowDefinitions>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="*" />
                    <ColumnDefinition Width="*" />
                    <ColumnDefinition Width="*" />
                    <ColumnDefinition Width="*" />
                </Grid.ColumnDefinitions>

                <Label  x:Name="B1" Text = "ROUTE CODE" Grid.Row="1" Grid.Column="0"  FontSize="15" BackgroundColor="Gray" HorizontalTextAlignment="Center" VerticalTextAlignment="Center" FontAttributes="Bold" TextColor="White"/>
                <Label  x:Name="B2" Text = "" Grid.Row="1" Grid.Column="1"  FontSize="15" BackgroundColor="White"  HorizontalTextAlignment="Center" VerticalTextAlignment="Center" FontAttributes="Bold" TextColor="Black"/>
                <Label x:Name="B3" Text = "SHOP CODE" Grid.Row="1" Grid.Column="2"  FontSize="15" BackgroundColor="Gray"  HorizontalTextAlignment="Center" VerticalTextAlignment="Center" FontAttributes="Bold" TextColor="White"/>
                <Label x:Name="B4" Text = "Admin" Grid.Row="1" Grid.Column="3"  FontSize="15" BackgroundColor="White"  HorizontalTextAlignment="Center" VerticalTextAlignment="Center" FontAttributes="Bold" TextColor="Black"/>

                <Label  x:Name="B5" Text = "INVOICE DATE" Grid.Row="2" Grid.Column="0"  FontSize="15" BackgroundColor="Gray" HorizontalTextAlignment="Center" VerticalTextAlignment="Center" FontAttributes="Bold" TextColor="White"/>
                <DatePicker x:Name="radDateTimePicker1" MinimumDate="01/01/1990" MaximumDate="01/01/2030" Grid.Row="2" Grid.Column="1" HorizontalOptions="Center" VerticalOptions="Center" />
                <Label  x:Name="B10" Text="{Binding Date, Source={x:Reference radDateTimePicker1},StringFormat='{0:d/M/yyyy HH:mm:ss}'}" Grid.Row="5" Grid.Column="1"  FontSize="15" BackgroundColor="White"  HorizontalTextAlignment="Center" VerticalTextAlignment="Center" FontAttributes="Bold" TextColor="White" />


               <!-- <Label  x:Name="B15" Text = "AREA" Grid.Row="4" Grid.Column="0"  FontSize="Medium" BackgroundColor="Gray" HorizontalTextAlignment="Center" VerticalTextAlignment="Center" FontAttributes="Bold" TextColor="White"/>
                <Label  x:Name="B25" Text = "UVA" Grid.Row="4" Grid.Column="1"  FontSize="Medium" BackgroundColor="White"  HorizontalTextAlignment="Center" VerticalTextAlignment="Center" FontAttributes="Bold" TextColor="Black"/>-->

                <Label  x:Name="B35" Text = "SHOP NAME" Grid.Row="5" Grid.Column="0"  FontSize="15" BackgroundColor="Gray" HorizontalTextAlignment="Center" VerticalTextAlignment="Center" FontAttributes="Bold" TextColor="White"/>
                <Label  x:Name="B45" Text = "RADHA TRADERS" Grid.Row="5" Grid.Column="1"  FontSize="15" BackgroundColor="White"  HorizontalTextAlignment="Center" VerticalTextAlignment="Center" FontAttributes="Bold" TextColor="Black"/>

                <Label  x:Name="B55" Text = "SALES ORDER" Grid.Row="3" Grid.Column="0"  FontSize="15" BackgroundColor="Gray" HorizontalTextAlignment="Center" VerticalTextAlignment="Center" FontAttributes="Bold" TextColor="White"/>
                <Label  x:Name="B65" Text = "HATTON" Grid.Row="3" Grid.Column="1"  FontSize="15" BackgroundColor="White"  HorizontalTextAlignment="Center" VerticalTextAlignment="Center" FontAttributes="Bold" TextColor="Black"/>




                <Label x:Name="B7" Text = "AGENCY NAME" Grid.Row="2" Grid.Column="2"  FontSize="15" BackgroundColor="Gray"  HorizontalTextAlignment="Center" VerticalTextAlignment="Center" FontAttributes="Bold" TextColor="White"/>
                <Picker x:Name="PickerListtwo" Grid.Row="2" Grid.Column="3" BackgroundColor="White" IsVisible="False" SelectedIndexChanged="PickerList_SelectedIndexChangedtwo">
                    <Picker.Items>
                        <x:String>Mohammed R</x:String>
                        <x:String>Sanath J</x:String>
                        <x:String>Rahul R</x:String>
                        <x:String>Mohammed I</x:String>
                        <x:String>Thuwan A</x:String>
                        <x:String>Praveen K</x:String>
                        <x:String>Roshan P</x:String>
                    </Picker.Items>
                </Picker>
                <Label x:Name="PickerLabeltwo" Text="CUSTOMER" FontSize="15" Grid.Row="2" Grid.Column="3" HorizontalOptions="Center" HorizontalTextAlignment="Center">
                    <Label.GestureRecognizers>
                        <TapGestureRecognizer Tapped="TapGestureRecognizer_Tappedtwo"/>
                    </Label.GestureRecognizers>
                </Label>

                <!--<Label  x:Name="B9" Text = "" Grid.Row="3" Grid.Column="0"  FontSize="Medium" BackgroundColor="White" HorizontalTextAlignment="Center" VerticalTextAlignment="Center" FontAttributes="Bold" TextColor="White"/>-->

                <Label x:Name="B11" Text = "AGENCY CODE" Grid.Row="3" Grid.Column="2"  FontSize="15" BackgroundColor="Gray"  HorizontalTextAlignment="Center" VerticalTextAlignment="Center" FontAttributes="Bold" TextColor="White" />
                <Label x:Name="outstanding" Text = "" Grid.Row="3" Grid.Column="3"  FontSize="15" BackgroundColor="White"  HorizontalTextAlignment="Center" VerticalTextAlignment="Center" FontAttributes="Bold" TextColor="Red"/>
                <Label x:Name="CRTOP" Text = "AREA" Grid.Row="4" Grid.Column="2"  FontSize="15" BackgroundColor="Gray"  HorizontalTextAlignment="Center" VerticalTextAlignment="Center" FontAttributes="Bold" TextColor="White"/>
                <Label x:Name="CRANS" Text = "" Grid.Row="4" Grid.Column="3"  FontSize="15" BackgroundColor="White"  HorizontalTextAlignment="Center" VerticalTextAlignment="Center" FontAttributes="Bold" TextColor="Red"/>
                <Label x:Name="CRBAL" Text = "OPEARTION" Grid.Row="5" Grid.Column="2"  FontSize="15" BackgroundColor="Gray"  HorizontalTextAlignment="Center" VerticalTextAlignment="Center" FontAttributes="Bold" TextColor="White"/>
                <Label x:Name="BALANS" Text = "" Grid.Row="5" Grid.Column="3"  FontSize="15" BackgroundColor="White"  HorizontalTextAlignment="Center" VerticalTextAlignment="Center" FontAttributes="Bold" TextColor="Red"/>
                <Label x:Name="ADDRESSN" Text = "ADDRESS" Grid.Row="6" Grid.Column="2"  FontSize="15" BackgroundColor="Gray"  HorizontalTextAlignment="Center" VerticalTextAlignment="Center" FontAttributes="Bold" TextColor="White"/>
                <Label x:Name="ADDRESSV" Text = "" Grid.Row="6" Grid.Column="3"  FontSize="15" BackgroundColor="White"  HorizontalTextAlignment="Center" VerticalTextAlignment="Center" FontAttributes="Bold" TextColor="Red"/>
            </Grid>

            <Grid x:Name="controlGrid" RowSpacing="2" ColumnSpacing="1.5" BackgroundColor="Black" >
                <Grid.RowDefinitions>
                    <RowDefinition Height="20" />
                    <RowDefinition Height="50" />
                    <RowDefinition Height="50" />
                    <!--<RowDefinition Height="50" />-->
                    <!--<RowDefinition Height="50" />
                    <RowDefinition Height="50" />

                    <RowDefinition Height="50" />
                    <RowDefinition Height="50" />
                    <RowDefinition Height="50" />
                    <RowDefinition Height="50" />
                    <RowDefinition Height="50" />
                    <RowDefinition Height="50" />-->


                </Grid.RowDefinitions>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="*" />
                    <ColumnDefinition Width="*" />
                    <ColumnDefinition Width="*" />
                    <ColumnDefinition Width="*" />
                    <ColumnDefinition Width="*" />

                </Grid.ColumnDefinitions>
                <Label  x:Name="Btn_1" Text = "PRODUCT" Grid.Row="1" Grid.Column="0"  FontSize="15" BackgroundColor="Gray" HorizontalTextAlignment="Center" VerticalTextAlignment="Center" FontAttributes="Bold" TextColor="White"/>
                <Label  x:Name="Btn_2" Text = "QTY" Grid.Row="1" Grid.Column="2"  FontSize="15" BackgroundColor="Gray"  HorizontalTextAlignment="Center" VerticalTextAlignment="Center" FontAttributes="Bold" TextColor="White"/>
                <Label x:Name="Btn_3" Text = "PRICE" Grid.Row="1" Grid.Column="1"  FontSize="15" BackgroundColor="Gray"  HorizontalTextAlignment="Center" VerticalTextAlignment="Center" FontAttributes="Bold" TextColor="White"/>
                <Label x:Name="Btn_4" Text = "FREE ISSUE" Grid.Row="1" Grid.Column="3"  FontSize="15" BackgroundColor="Gray"  HorizontalTextAlignment="Center" VerticalTextAlignment="Center" FontAttributes="Bold" TextColor="White"/>
                <Label x:Name="Btn_5" Text = "UNIT TOTAL" Grid.Row="1" Grid.Column="4"  FontSize="15" BackgroundColor="Gray"  HorizontalTextAlignment="Center" VerticalTextAlignment="Center" FontAttributes="Bold" TextColor="White"/>


                <Label x:Name="PickerLabelthree" Text="1 SELECT PRODUCT" FontSize="12" Grid.Row="2" Grid.Column="0" HorizontalOptions="FillAndExpand" BackgroundColor="White" TextColor="Black" HorizontalTextAlignment="Center" VerticalTextAlignment="Center">
                    <Label.GestureRecognizers>
                        <TapGestureRecognizer Tapped="TapGestureRecognizer_Tappedthree"/>
                    </Label.GestureRecognizers>
                </Label>

                <Entry  x:Name="Qtyone" TextChanged="SetRepairPercent" Keyboard="Numeric" Completed="Qtyone_Completed" Grid.Row="2" Grid.Column="2" BackgroundColor="White" FontSize="15" TextColor="Black"  />

                <Entry  x:Name="Priceoneout" TextChanged="SetRepairPercent" Keyboard="Numeric"  Grid.Row="2" Grid.Column="1" BackgroundColor="White" FontSize="15" TextColor="Black"  />

                <Label  x:Name="Totone" Grid.Row="2" Grid.Column="3"  BackgroundColor="White" FontSize="15" TextColor="Black" HorizontalTextAlignment="Center" VerticalTextAlignment="Center" />

                <Entry  x:Name="TotoneR" TextChanged="SetRepairPercent" Keyboard="Numeric"  Grid.Row="2" Grid.Column="4"  BackgroundColor="White" FontSize="15" TextColor="Black"  />

以及后面的自动生成行代码

private void Qtyone_Completed(object sender, EventArgs e)
        {
            AddARowToGridView();
           // TotoneR.Text = "13.5";
        }

        int rowCount;
        private void AddARowToGridView()
        {


            Label PickerLabelthree = new Label();
            PickerLabelthree.Text = "1 SELECT PRODUCT";
            PickerLabelthree.FontSize = 15;
            PickerLabelthree.BackgroundColor = Color.White;
            PickerLabelthree.TextColor = Color.Black;
            PickerLabelthree.HorizontalOptions = LayoutOptions.FillAndExpand;
            PickerLabelthree.HorizontalTextAlignment = TextAlignment.Center;
            PickerLabelthree.VerticalTextAlignment = TextAlignment.Center;
            // PickerLabelthree.Text = pickelabelthr;
            // PickerLabelthree.GestureRecognizers.Add(new TapGestureRecognizer((view) => OnLabelClicked()));
          //  SalesPopup.testing = PickerLabelthree.Text;

            PickerLabelthree.GestureRecognizers.Add(new TapGestureRecognizer
            {
                Command = new Command(() => OnLabelClicked()),
            });





            //PickerLabelthree.GestureRecognizers.Add() += TapGestureRecognizer_Tappedthree;


            Entry Qtyone = new Entry();
            Qtyone.Keyboard = Keyboard.Numeric;
            Qtyone.BackgroundColor = Color.White;
            Qtyone.FontSize = 12;
            Qtyone.TextColor = Color.Black;
            Qtyone.TextChanged += SetRepairPercent;
            Qtyone.Completed += Qtyone_Completed;





            Entry Priceoneout = new Entry();
            Priceoneout.TextChanged += SetRepairPercent;
            Priceoneout.Keyboard = Keyboard.Numeric;
            Priceoneout.BackgroundColor = Color.White;
            Priceoneout.FontSize = 12;
            Priceoneout.TextColor = Color.Black;

            Label Totone = new Label();
            Totone.BackgroundColor = Color.White;
            Totone.FontSize = 12;
            Totone.TextColor = Color.Black;
            Totone.HorizontalTextAlignment = TextAlignment.Center;
            Totone.VerticalTextAlignment = TextAlignment.Center;

            Entry TotoneR = new Entry();
            TotoneR.Keyboard = Keyboard.Numeric;
            TotoneR.BackgroundColor = Color.White;
            TotoneR.TextColor = Color.Black;
            TotoneR.FontSize = 12;
            TotoneR.TextChanged += SetRepairPercent;


            //double _qtyoneamount;
            //_qtyoneamount = this.Qtyoneans * this.Priceoneans;
            //TotoneR.Text = _qtyoneamount.ToString();




            controlGrid.Children.Add(PickerLabelthree, 0, Convert.ToInt32(rowCount));
            controlGrid.Children.Add(Priceoneout, 1, Convert.ToInt32(rowCount));
            controlGrid.Children.Add(Qtyone, 2, Convert.ToInt32(rowCount));
            controlGrid.Children.Add(Totone, 3, Convert.ToInt32(rowCount));
            controlGrid.Children.Add(TotoneR, 4, Convert.ToInt32(rowCount));

           // TotoneR.Text = "13.5";
            rowCount++;


        }

所以问题是,我无法识别新生成的行中自动创建的行中单元格的详细信息(以获取列单元格的引用)(因为原因是要识别新的列行单元格是我们需要计算价格和数量以计算最后一栏中的总价值)。非常感谢您的帮助,并感谢您的支持。

1 个答案:

答案 0 :(得分:0)

尽管最好改用ObservableCollection,但是您的解决方案不起作用的原因是因为您没有将子代添加到Main / UI线程中的网格中。 Qtyone_Completed事件处理程序会创建另一个线程来执行布局更改。您可以通过将这些行包含在这样的函数中

来使其工作
Device.BeginInvokeOnMainThread(() =>
{
    controlGrid.Children.Add(PickerLabelthree, 0, Convert.ToInt32(rowCount));
    controlGrid.Children.Add(Priceoneout, 1, Convert.ToInt32(rowCount));
    controlGrid.Children.Add(Qtyone, 2, Convert.ToInt32(rowCount));
    controlGrid.Children.Add(Totone, 3, Convert.ToInt32(rowCount));
    controlGrid.Children.Add(TotoneR, 4, Convert.ToInt32(rowCount));
});