本文介绍了带有标签页的 Xamarin 表单在每个标签页中都有数据表单输入页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 Xamarin 的新手.我有一个带有选项卡式页面的 Xamarin 表单应用程序,它有 3 个选项卡,每个选项卡都有一个数据输入表单,所有数据输入表单都应该传递给 webapi DTO 实体类.我如何从 3 个选项卡中的 3 个页面收集所有数据,并在单击第三个选项卡上的提交按钮时发布数据.实现这一点的最佳方法是什么?我的代码在下面

I'm new to Xamarin. I've a Xamarin forrms app with Tabbed Pages and it has 3 tabs with each tab having a data entry form and all data entry forms should be passed to a webapi DTO entity class. How do i collect all the data from the 3 pages in the 3 tabs and post the data when i click the submit button on the 3rd tab. What is the best way to implement this? My codes are below

模型中的 DTO FamilyTreeMaster

DTO FamilyTreeMaster in Models

                using System;
                    using System.Collections.Generic;
                    using System.Text;

                    namespace FamilyTreeApp.Models
                    {
                public class DTOFamilyTreeMaster
                {
                    public string Title { get; set; }
                    public string FirstName { get; set; }
                    public string LastName { get; set; }
                    public Nullable<int> Age { get; set; }
                    public string Profession { get; set; }
                    public string FatherFirstName { get; set; }
                    public string FatherLastName { get; set; }
                    public string MotherFirstName { get; set; }
                    public string MotherLastName { get; set; }
                    public string Address1 { get; set; }
                    public string Address2 { get; set; }
                    public string City { get; set; }
                    public string State { get; set; }
                    public string Zip { get; set; }
                    public string Country { get; set; }
                    public string Phone { get; set; }
                    public string Spouse1_FirstName { get; set; }
                    public string Spouse1_LastName { get; set; }
                    public string Spouse2_FirstName { get; set; }
                    public string Spouse2_LastName { get; set; }
                    public string Spouse3_FirstName { get; set; }
                    public string Spouse3_LastName { get; set; }
                    public string Spouse4_FirstName { get; set; }
                    public string Spouse4_LastName { get; set; }
                    public string Spouse5_FirstName { get; set; }
                    public string Spouse5_LastName { get; set; }
                    public string Son1_FirstName { get; set; }
                    public string Son1_LastName { get; set; }
                    public string Son2_FirstName { get; set; }
                    public string Son2_LastName { get; set; }
                    public string Son3_FirstName { get; set; }
                    public string Son3_LastName { get; set; }
                    public string Son4_FirstName { get; set; }
                    public string Son4_LastName { get; set; }
                    public string Son5_FirstName { get; set; }
                    public string Son5_LastName { get; set; }
                    public string Son6_FirstName { get; set; }
                    public string Son6_LastName { get; set; }
                    public string Son7_FirstName { get; set; }
                    public string Son7_LastName { get; set; }
                    public string Son8_FirstName { get; set; }
                    public string Son8_LastName { get; set; }
                    public string Son9_FirstName { get; set; }
                    public string Son9_LastName { get; set; }
                    public string Son10_FirstName { get; set; }
                    public string Son10_LastName { get; set; }
                    public string Daughter1_FirstName { get; set; }
                    public string Daughter1_LastName { get; set; }
                    public string Daughter2_FirstName { get; set; }
                    public string Daughter2_LastName { get; set; }
                    public string Daughter3_FirstName { get; set; }
                    public string Daughter3_LastName { get; set; }
                    public string Daughter4_FirstName { get; set; }
                    public string Daughter4_LastName { get; set; }
                    public string Daughter5_FirstName { get; set; }
                    public string Daughter5_LastName { get; set; }
                    public string Daughter6_FirstName { get; set; }
                    public string Daughter6_LastName { get; set; }
                    public string Daughter7_FirstName { get; set; }
                    public string Daughter7_LastName { get; set; }
                    public string Daughter8_FirstName { get; set; }
                    public string Daughter8_LastName { get; set; }
                    public string Daughter9_FirstName { get; set; }
                    public string Daughter9_LastName { get; set; }
                    public string Daughter10_FirstName { get; set; }
                    public string Daughter10_LastName { get; set; }
                    public Nullable<int> MyPosition { get; set; }
                    public Nullable<int> MyParentPosition { get; set; }
                }
            }

视图中的标签页

UserInfoTabbedPage

UserInfoTabbedPage

                <?xml version="1.0" encoding="utf-8" ?>
            <TabbedPage Visual="Material"
                            xmlns="http://xamarin.com/schemas/2014/forms"
                                     xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" xmlns:local="clr-namespace:FamilyTreeApp.Pages"
                                    x:Class="FamilyTreeApp.Pages.UserInfoTabbedPage">
                <local:UserTabbedPage  Title ="User"
                                                 Icon ="user.png"/>
                <local:UserSpousePage Title ="Family"
                                                 Icon ="sharp_group_add_black_18.png"/>
                <local:UserChildrenTabbedPage class="material-icons"  Title ="User"
                                                 Icon ="user.png"/>

            </TabbedPage>





用户标签页

                    <?xml version="1.0" encoding="utf-8" ?>
                <ContentPage Visual="Material"
                                xmlns="http://xamarin.com/schemas/2014/forms"
                                         xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
                                         x:Class="FamilyTreeApp.Pages.UserTabbedPage">
                    <TableView Intent="Form">
                        <TableRoot>
                            <TableSection Title="Basic">
                                <EntryCell Placeholder="Title" Text="{Binding Title}"></EntryCell>
                                <EntryCell Placeholder="First Name" Text="{Binding FirstName}"></EntryCell>
                                <EntryCell Placeholder="Last Name" Text="{Binding LastName}"></EntryCell>
                                <EntryCell Placeholder="Nick Name" Text="{Binding NickName}"></EntryCell>
                                <EntryCell Placeholder="Age" Text="{Binding Age}"></EntryCell>
                                <EntryCell Placeholder="Profession" Text="{Binding Profession}"></EntryCell>
                            </TableSection>
                            <TableSection Title="Contact">
                                <EntryCell Placeholder="Address 1" Text="{Binding Address1}"></EntryCell>
                                <EntryCell Placeholder="Address 2" Text="{Binding Address2}"></EntryCell>
                                <EntryCell Placeholder="City" Text="{Binding City}"></EntryCell>
                                <EntryCell Placeholder="State" Text="{Binding State}"></EntryCell>
                                <EntryCell Placeholder="Zip" Text="{Binding Zip}" Keyboard="Numeric"></EntryCell>
                                <EntryCell Placeholder="Country" Text="{Binding Country}"></EntryCell>
                                <EntryCell Placeholder="Phone" Text="{Binding Phone}" Keyboard="Numeric"></EntryCell>

                            </TableSection>
                        </TableRoot>
                    </TableView>

                </ContentPage>


UserSpousePage

UserSpousePage

                    <?xml version="1.0" encoding="utf-8" ?>
                <ContentPage Visual="Material"
                            xmlns="http://xamarin.com/schemas/2014/forms"
                                     xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
                                     x:Class="FamilyTreeApp.Pages.UserSpousePage"
                                     xmlns:ic="clr-namespace:ImageCircle.Forms.Plugin.Abstractions;assembly=ImageCircle.Forms.Plugin"
                                     >
                <TableView Intent="Form">
                    <TableRoot>
                        <TableSection Title="Parents">
                            <EntryCell  Placeholder="Father First Name" Text="{Binding FatherFirstName}"></EntryCell>
                            <EntryCell Placeholder="Father Last Name" Text="{Binding FatherLastName}"></EntryCell>
                            <EntryCell Placeholder="Mother First Name" Text="{Binding MotherFirstName}"></EntryCell>
                            <EntryCell Placeholder="Mother Last Name" Text="{Binding MotherLastName}"></EntryCell>

                        </TableSection>

                        <TableSection x:Name="Spouses" Title="Spouse(s)">
                            <ViewCell>
                                <StackLayout>
                                    <Grid>

                                        <Grid.ColumnDefinitions>
                                            <ColumnDefinition Width="20"/>
                                            <ColumnDefinition Width="2*"/>
                                        </Grid.ColumnDefinitions>

                                        <Label VerticalOptions="Center" HorizontalOptions="Center" Grid.Row="0" Grid.Column="0"
                                                                           Text="{Binding Source={x:Reference stepper},Path=Value}"/>

                                        <Stepper Grid.Row="0" Grid.Column="1" x:Name="stepper" Increment="1" Maximum="5" Minimum="1" >
                                        </Stepper>
                                        <ic:CircleImage  Grid.Row="0" Grid.Column="2"  Source="sharp_group_add_black_18.png">
                                            <Image.GestureRecognizers>
                                                <TapGestureRecognizer Tapped="OnStepperValueChanged" />
                                            </Image.GestureRecognizers>

                                        </ic:CircleImage>

                                    </Grid>

                                </StackLayout>

                            </ViewCell>
                        </TableSection>
                    </TableRoot>
                </TableView>

                </ContentPage>

UserSpousePage.xaml.cs

UserSpousePage.xaml.cs

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

                namespace FamilyTreeApp.Pages
                {
                    [XamlCompilation(XamlCompilationOptions.Compile)]
                    public partial class UserSpousePage : ContentPage
                    {
                        public int countSpouses = 1;

                        public UserSpousePage()
                        {

                            InitializeComponent();
                            //CreateEntryCells(10);

                        }

                        private void CreateEntryCells(int numCell, string textToBeCreated)
                        {
                            for (int i = countSpouses; i<= numCell; i++)
                            {
                                EntryCell entInput = new EntryCell
                                {
                                    Placeholder = textToBeCreated + countSpouses
                                };

                                string txt = "{Binding" + textToBeCreated + countSpouses + "_FirstName}";
                                //string txtLastName = "{Binding" + textToBeCreated + countSpouses + "_LastName}";

                                entInput.SetBinding(EntryCell.TextProperty, txt);

                                Spouses.Add(entInput);
                                ++countSpouses;

                            }

                                  }


                        private void OnStepperValueChanged(object sender, System.EventArgs e)
                        {
                            CreateEntryCells(int.Parse(stepper.Value.ToString()),"Spouse Full Name");

                        }
                    }
                }

UserChildrenTabbedPage

UserChildrenTabbedPage

                    <?xml version="1.0" encoding="utf-8" ?>
                <ContentPage Visual="Material" xmlns="http://xamarin.com/schemas/2014/forms"
                                 xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" xmlns:ic="clr-namespace:ImageCircle.Forms.Plugin.Abstractions;assembly=ImageCircle.Forms.Plugin"
                                 x:Class="FamilyTreeApp.Pages.UserChildrenTabbedPage">
                    <AbsoluteLayout HorizontalOptions="FillAndExpand"
                                        VerticalOptions="FillAndExpand">

                <TableView Intent="Form">
                    <TableRoot>

                        <TableSection x:Name="Sons" Title="Son(s)">
                            <ViewCell>
                                <StackLayout>
                                    <Grid>

                                        <Grid.ColumnDefinitions>
                                            <ColumnDefinition Width="20"/>
                                            <ColumnDefinition Width="2*"/>
                                        </Grid.ColumnDefinitions>

                                        <Label VerticalOptions="Center" HorizontalOptions="Center" Grid.Row="0" Grid.Column="0" Text="{Binding Source={x:Reference stepperSon},Path=Value}"/>

                                        <Stepper Grid.Row="0" Grid.Column="1" x:Name="stepperSon" Increment="1" Maximum="10" Minimum="1" >
                                        </Stepper>
                                        <ic:CircleImage  Grid.Row="0" Grid.Column="2"  Source="sharp_group_add_black_18.png">
                                            <Image.GestureRecognizers>
                                                <TapGestureRecognizer Tapped="OnstepperSonValueChanged" />
                                            </Image.GestureRecognizers>
                                        </ic:CircleImage>
                                    </Grid>
                                </StackLayout>
                            </ViewCell>
                        </TableSection>
                        <TableSection x:Name="Daughters" Title="Daughter(s)">
                            <ViewCell>
                                <StackLayout>
                                    <Grid>
                                        <Grid.ColumnDefinitions>
                                            <ColumnDefinition Width="20"/>
                                            <ColumnDefinition Width="2*"/>
                                        </Grid.ColumnDefinitions>
                                        <Label VerticalOptions="Center" HorizontalOptions="Center" Grid.Row="0" Grid.Column="0" Text="{Binding Source={x:Reference stepperDaughter},Path=Value}"/>

                                        <Stepper Grid.Row="0" Grid.Column="1" x:Name="stepperDaughter" Increment="1" Maximum="10" Minimum="1" >
                                        </Stepper>
                                        <ic:CircleImage  Grid.Row="0" Grid.Column="2"  Source="sharp_group_add_black_18.png">
                                            <Image.GestureRecognizers>
                                                <TapGestureRecognizer Tapped="OnstepperDaughterValueChanged" />
                                            </Image.GestureRecognizers>
                                        </ic:CircleImage>
                                    </Grid>
                                </StackLayout>
                            </ViewCell>
                        </TableSection>

                    </TableRoot>
                </TableView>

                <StackLayout
                         AbsoluteLayout.LayoutFlags="All"
                         AbsoluteLayout.LayoutBounds="1,1,0.25,0.25">

                    <StackLayout HorizontalOptions="End" VerticalOptions="End" Margin="0,0,0,0">
                        <Button Clicked="OnSubmit" Text="Submit" HorizontalOptions="End"
                            ></Button>
                    </StackLayout>

                </StackLayout>

                </AbsoluteLayout>
                </ContentPage>

UserChildrenTabbedPage.xaml.cs

UserChildrenTabbedPage.xaml.cs

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

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

                namespace FamilyTreeApp.Pages
                {
                    [XamlCompilation(XamlCompilationOptions.Compile)]
                    public partial class UserChildrenTabbedPage : ContentPage
                    {
                        public int countSons = 1;
                        public int countDaughters = 1;

                        public UserChildrenTabbedPage()
                        {
                            InitializeComponent();
                        }

                        private void CreateEntryCells(int numCell, string textToBeCreated)
                        {
                            if (textToBeCreated.ToLower().Contains("son"))
                            {
                                for (int i = countSons; i<= numCell; i++)
                                {
                                    EntryCell entInput = new EntryCell
                                    {
                                        Placeholder = textToBeCreated + countSons
                                        //Text = "{Binding Spouse' + 1 + 'FirstName}"
                                        //HorizontalOptions = LayoutOptions.FillAndExpand
                                    };

                                    string txt = "{Binding" + textToBeCreated + countSons + "_FirstName}";
                                    entInput.SetBinding(EntryCell.TextProperty, txt);

                                    Sons.Add(entInput);
                                    ++countSons;

                                }

                            }
                            else
                            {
                                for (int i = countDaughters; i <= numCell; i++)
                                {
                                    EntryCell entInput = new EntryCell
                                    {
                                        Placeholder = textToBeCreated + countDaughters
                                        //Text = "{Binding Spouse' + 1 + 'FirstName}"
                                        //HorizontalOptions = LayoutOptions.FillAndExpand
                                    };

                                    string txt = "{Binding" + textToBeCreated + countDaughters + "_FirstName}";
                                    entInput.SetBinding(EntryCell.TextProperty, txt);

                                    Daughters.Add(entInput);
                                    ++countDaughters;

                                }


                            }
                        }

                        private void OnstepperDaughterValueChanged(object sender, EventArgs e)
                        {
                            CreateEntryCells(int.Parse(stepperDaughter.Value.ToString()), "Daughter");

                        }

                        private void OnstepperSonValueChanged(object sender, EventArgs e)
                        {

                            CreateEntryCells(int.Parse(stepperSon.Value.ToString()), "Son");

                        }

                        private void OnSubmit(object sender, EventArgs e)
                        {
                        }
                    }
                }

.如何通过 DTO 对象将所有标签数据发布到 web api?

.How to post all the tabbed data to the web api through DTO object?

推荐答案

您可以为数据管理器定义一个单例类.

You could define a Singleton Class for Data Manager .

public class DataManager
    {
        private static DataManager instance;


        // save data of three forms,or you could define them as any other type
        public Dictionary<string, string> Forms1 { get; set; }
        public Dictionary<string, string> Forms2 { get; set; }
        public Dictionary<string, string> Forms3 { get; set; }



        private DataManager() { }

        public static DataManager Instance
        {
            get
            {
                if (instance == null)
                {
                    instance = new DataManager();


                }
                return instance;
            }
        }

当你想保存第一页和第二页的数据时

When you want to save the data in the first and second Page

DataManager.Instance.Forms1 = new Dictionary<string, string>();
for (int i = countDaughters; i <= numCell; i++)
{
   DataManager.Instance.Forms1.Add("xxx","xxx");
}

获取

var Form1 = DataManager.Instance.Forms1 ;

这篇关于带有标签页的 Xamarin 表单在每个标签页中都有数据表单输入页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-15 04:45