【发布时间】:2019-12-03 05:05:59
【问题描述】:
我正在尝试使用 Xamarin.Forms 创建一个移动应用程序,以使用户能够添加卖家和产品。 我有三个班级:
- 卖家:希望销售产品的注册用户。
- 产品:卖家想要出售的物品。
- Sellers:有一个包含卖家列表的属性,CurrentSellerID 和 CurrentProductID。 CurrentSellerID 和 CurrentProductID 是处理卖家 ID 和产品 ID 的唯一编号的计数器。每当添加新卖家和新产品时,这些计数器应增加并分配给相应的 ID。
卖家类别:
public class Seller
{
public string Name { get; set; }
public string MobileNumber { get; set; }
public string Email { get; set; }
public int SellerID { get; set; }
public List<Product> ProductList { get; set; }
public Seller(string inName, string inMobile, string inEmail, int inID)
{
Name = inName;
MobileNumber = inMobile;
Email = inEmail;
SellerID = inID;
ProductList = new List<Product>();
}
}
产品类别:
public class Product
{
public int ProductID { get; set; }
public string Title { get; set; }
public string ImageName { get; set; }
public double Price { get; set; }
public int Quantity { get; set; }
public string Description { get; set; }
public Product(int inID, string inTitle, string inImage, double inPrice, int inQuantity, string inDesc)
{
ProductID = inID;
Title = inTitle;
ImageName = inImage;
Price = inPrice;
Quantity = inQuantity;
Description = inDesc;
}
}
}
Sellers 类(包含生成测试数据的方法):
public class Sellers
{
public static int CurrentSellerID = 0;
public static int CurrentProductID = 0;
public Sellers()
{
SellerList = new ObservableCollection<Seller>();
}
public ObservableCollection<Seller> SellerList;
public static Sellers GenerateTestData()
{
string[] firstNames = new string[] { "Rob", "Jim" };
string[] lastsNames = new string[] { "Smith", "Jones" };
string[] mobileNumbers = new string[] { "07492941804", "07402276152" };
string[] emailAddress = new string[] { "robSmith@gmail.com", "jimJones@gmail.com" };
string[] productTitles = new string[] { "Mobile", "RAM", "CPU", "HDD", "SSD", "RAM" };
string[] imageNames = new string[] { "img1.png", "img2.png", "img3.png", "img4.png", "img5.png", "img6.png" };
double[] productPrice = new double[] { 100.00, 75.00, 328.25, 43.50, 65.00, 100 };
int[] productQuantity = new int[] { 100, 20, 10, 5, 30, 60 };
string[] productDesc = new string[] { "iPhone X, 128GB Storage, Black", "DDR4-3000 MHz", "Intel i5-9600K", "1TB Hard Drive", "250GB SSD, 500MB/s", "DDR4-2666MHz" };
Sellers result = new Sellers();
string name = firstNames[0] + lastsNames[0];
Seller newSeller = new Seller(name, mobileNumbers[0], emailAddress[0], Sellers.CurrentSellerID);
for (int x = 0; x < 3; x++)
{
Product newProduct = new Product(Sellers.CurrentProductID, productTitles[x], imageNames[x], productPrice[x], productQuantity[x], productDesc[x]);
newSeller.ProductList.Add(newProduct);
CurrentProductID++;
}
result.SellerList.Add(newSeller);
CurrentSellerID++;
return result;
}
}
在App类中,创建一个实例并调用测试数据生成函数:
public static Sellers ActiveSellersList;
public App()
{
InitializeComponent();
ActiveSellersList = Sellers.GenerateTestData();
}
我有一个页面应该使用数据绑定来显示所有产品。这称为“ViewProductsPage”。 ListView (lstProductDetails) 用于显示列表中的产品。
<ListView x:Name="lstProductDetails">
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<StackLayout Orientation="Horizontal" HorizontalOptions="Fill">
<Image Source="{Binding ImageName}" HorizontalOptions="Start"
AbsoluteLayout.LayoutBounds="0, 0, 600, 50 " Aspect="AspectFill"/>
<StackLayout BackgroundColor="#eee" Orientation="Vertical"
VerticalOptions="StartAndExpand">
<Label Text = "{Binding Title}" FontSize="14" TextColor="#f35e20"
AbsoluteLayout.LayoutBounds="100, 0.25, 400, 40"/>
<Label Text = "{Binding Price}" FontSize="10" TextColor="Blue"
AbsoluteLayout.LayoutBounds="100, 35, 200, 25"/>
<Label Text = "{Binding Quantity}" FontSize="10" TextColor="Blue"
AbsoluteLayout.LayoutBounds="100, 35, 200, 25"/>
<Label Text = "{Binding Description}" FontSize="10" TextColor="Blue"
AbsoluteLayout.LayoutBounds="100, 35, 200, 25"/>
</StackLayout>
</StackLayout>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
我遇到的问题是绑定所有产品。我不确定如何显示 ProductList。这是我在 ViewProductsPage 的构造函数中的代码。
this.lstProductDetails.ItemsSource = App.ActiveSellersList.ProductList;
此代码不起作用,因为卖家不包含 ProductList 的定义。
如何显示所有卖家的产品列表?谢谢。
【问题讨论】:
标签: c# xaml xamarin xamarin.forms