Xamarin -使用提交按钮呈现表单中的对象数组

3npbholx  于 2022-12-07  发布在  其他
关注(0)|答案(1)|浏览(149)

我是新的Xamarin,我想开发一个屏幕,基本上呈现多个部分,像这个屏幕UI Screen,并在点击提交按钮,我希望所有输入的对象在服务器端。
绑定到窗体的类将是一个产品列表。

Public class Product
{
   public int Id { get; set;}
   public string ProductName { get; set;}
   public int Quantity { get; set;}
}

用户将输入每个产品系列的数量。

点击提交按钮,我将如何获得整个对象(包括输入的数量)在服务器端。

rekjcdws

rekjcdws1#

可以,您可以使用CollectionViewListView来实现此功能。
我用ListViewData Binding实现了这个功能,可以参考下面的代码:
1.创建视图模型(ProductViewModel.cs

public class ProductViewModel
{

    // add SubmitCommand  for submit button
    public ICommand SubmitCommand { protected set; get; } 
    public ObservableCollection<Product> Items { get; set; }

    public ProductViewModel() {
        Items = new ObservableCollection<Product>();

        Items.Add(new Product {Id = 1, ProductName = "Tomato", Quantity = 1 });
        Items.Add(new Product {Id = 2, ProductName = "Romaine Lettuce", Quantity = 2 });
        Items.Add(new Product {Id= 3, ProductName = "Zucchini", Quantity = 3 });

        SubmitCommand = new Command(() => { 
           // here we will get the latest value of the binded property
            foreach(var item in Items) {
                System.Diagnostics.Debug.WriteLine(" ProductName is: " + item.ProductName+ " <----> Quantity is:  " + item.Quantity);
            }
        });    
    }
}

2.创建内容页(ProducePage
ProducePage.xaml

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" xmlns:viewmodels="clr-namespace:FormsListViewSample.ViewModels"
             x:Class="FormsListViewSample.ProducePage">

    <ContentPage.BindingContext>
        <viewmodels:ProductViewModel></viewmodels:ProductViewModel>
    </ContentPage.BindingContext>
    <ContentPage.Content>
        <StackLayout>

            <Button  Text="result" Command="{Binding SubmitCommand}"  />

            <ListView  x:Name="lstView"  ItemsSource="{Binding Items}"   RowHeight="60" HasUnevenRows="False">
                <ListView.ItemTemplate>
                    <DataTemplate>
                        <ViewCell>
                            <StackLayout Orientation="Horizontal" HorizontalOptions="Fill" BackgroundColor="Olive">
                                <StackLayout Orientation="Vertical">
                                    <Label Text="{Binding Id}" LineBreakMode="WordWrap"  FontSize="20" HorizontalOptions="FillAndExpand"  />

                                    <Label Text="{Binding ProductName}" LineBreakMode="WordWrap"  FontSize="20" HorizontalOptions="FillAndExpand"  />

                                </StackLayout>
                                <Entry Text="{Binding Quantity}"   FontSize="20" HorizontalOptions="FillAndExpand"  />

                            </StackLayout>

                        </ViewCell>
                    </DataTemplate>
                </ListView.ItemTemplate>
            </ListView>

        </StackLayout>
    </ContentPage.Content>
</ContentPage>

相关问题