Xamarin表单的选项卡式页面在每个选项卡式页面中都有数据表单输入页面

时间:2020-10-22 07:33:15

标签: c# xamarin xamarin.forms xamarin-forms-4

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

模型中的DTO FamilyTreeMaster

                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

                <?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>   




   

UserTabbedPage

                    <?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

                    <?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

            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

                    <?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

                 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?

1 个答案:

答案 0 :(得分:0)

您可以为Data Manager定义 Singleton类

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;
            }
        }

要在第一页和第二页中保存数据

设置

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 ;