Quantcast
Channel: Recent Threads — Xamarin Community Forums
Viewing all articles
Browse latest Browse all 204402

Xamarin Add another Section to Json

$
0
0

wondering if anybody can help with a solution I am looking for. In my Xamarin project, I have a 'add another line' button which adds another stack layout underneath it with the same controls as the first one. I want to save the new added one to JSON but not sure where to start, has anybody done this before?

I have added the first stack to json but i need it to reckonise the new ones on the button press. (sorry if it does not make sense)

Page.xaml Code:

`<Grid Margin="0, 30, 0, 30">
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="*"/>
                <ColumnDefinition Width="*"/>
                <ColumnDefinition Width="*"/>
                <ColumnDefinition Width="*"/>
                <ColumnDefinition Width="*"/>
            </Grid.ColumnDefinitions>
            <Grid.RowDefinitions>
                <RowDefinition Height="*"/>
            </Grid.RowDefinitions>
            <Label x:Name="NameLabel" Text="Name" TextColor="Black" FontSize="20" Grid.Column="0" Grid.Row="0" />
            <Label x:Name="ArrivalLabel" Text="Arrival" TextColor="Black" FontSize="20" Grid.Column="1" Grid.Row="0" />
            <Label x:Name="DescriptionLabel" Text="Description" TextColor="Black" FontSize="20" Grid.Column="2" Grid.Row="0" />
            <Label x:Name="DepartLabel" Text="Depart" TextColor="Black" FontSize="20" Grid.Column="3" Grid.Row="0" />
            <Label x:Name="SignLabel" Text="Sign" TextColor="Black" FontSize="20" Grid.Column="4" Grid.Row="0" />
        </Grid>
        <StackLayout x:Name="AddMoreNotes">
            <StackLayout>
                <Grid>
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="*"></ColumnDefinition>
                        <ColumnDefinition Width="*"></ColumnDefinition>
                        <ColumnDefinition Width="*"></ColumnDefinition>
                        <ColumnDefinition Width="*"></ColumnDefinition>
                        <ColumnDefinition Width="*"></ColumnDefinition>
                    </Grid.ColumnDefinitions>
                    <Grid.RowDefinitions>
                        <RowDefinition Height="*"></RowDefinition>
                    </Grid.RowDefinitions>
                    <Entry x:Name="Name" FontSize="20" Grid.Column="0" Grid.Row="0" />
                    <TimePicker x:Name="Arrival" FontSize="20" Grid.Column="1" Grid.Row="0" />
                    <Entry x:Name="Description" FontSize="20" Grid.Column="2" Grid.Row="0" />
                    <TimePicker x:Name="Depart" FontSize="20" Grid.Column="3" Grid.Row="0" />
                    <Entry x:Name="Sign" FontSize="20" Grid.Column="4" Grid.Row="0"/>
                </Grid>
            </StackLayout>
            <!--Add More View Spawns Here Do Not Change-->
        </StackLayout>

        <StackLayout Orientation="Horizontal" HorizontalOptions="End">
            <Button x:Name="AddMoreButton" Text="Add More" FontSize="16" BorderRadius="6" Clicked="AddMoreButton_Clicked" />
        </StackLayout>`

Page.xaml.cs Code:

    `private void AddMoreButton_Clicked(object sender, EventArgs e)
        {
            AddMoreNotes.Children.Add(new AttendanceAddMoreView()
            {

            });
        }

        private void SubmitButton_Clicked(object sender, EventArgs e)
        {
            var attendanceChecklist = new AttendanceChecklist();
            var AttentionOf1 = new List<AttentionOf>();
            var AttendanceSection1 = new List<AttendanceSection>();

            attendanceChecklist.TheDate = TheDate.Date;
            attendanceChecklist.AttendanceNumber = AttendanceNumber.Text;
            attendanceChecklist.EmployeeName = EmployeeName.Text;

            AttentionOf1.Add(new AttentionOf()
            {
                FirstName = FirstName.Text,
                LastName = LastName.Text,
                AddressLine1 = AddressLine1.Text,
                AddressLine2 = AddressLine2.Text,
                CityOrTown = CityOrTown.Text,
                County = County.Text,
                Postcode = Postcode.Text,
                ContractTitleOrRef = ContractTitleOrRef.Text,
                CustomerName = CustomerName.Text,
                SiteContact = SiteContact.Text,
            });

            AttendanceSection1.Add(new AttendanceSection()
            {
                Name = Name.Text,
                Arrival = Arrival.Time,
                Description = Description.Text,
                Depart = Depart.Time,
                Sign = Sign.Text
            });

            attendanceChecklist.attentionOf = AttentionOf1;
            attendanceChecklist.attendanceSection = AttendanceSection1;
            var json = JsonConvert.SerializeObject(attendanceChecklist, Newtonsoft.Json.Formatting.Indented);

            var documents = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
            var filename = Path.Combine(documents, "AttendanceNote.json");
            File.WriteAllText(filename, json);
            }`

Viewing all articles
Browse latest Browse all 204402

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>