我是Xamarin.form的初学者。当我点击按钮时,我想将产品添加到我的购物车。我试过并且不知道如何获得这个项目并发送到购物车页面。 这是我的xmal:
<ListView x:Name="productsListView" ItemSelected="OnProductSelected" HasUnevenRows="true" SeparatorVisibility="None" IsPullToRefreshEnabled="true" Refreshing="Handle_Refreshing">
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell >
<StackLayout Orientation="Horizontal" Padding="2">
<Image Source="{Binding ImageUrl}"/>
<StackLayout HorizontalOptions="StartAndExpand" >
<Label Text="{Binding ProductName}" />
<Label Text="{Binding Price}" TextColor="Gray"/>
</StackLayout>
<Button Text="+" Clicked="Add_To_ShoppingCart" HorizontalOptions="EndAndExpand"/>
</StackLayout>
<ViewCell.ContextActions>
<MenuItem Text="AddToNotify"/>
<MenuItem Text="Delete" IsDestructive="true" Clicked="OnDeleteProduct" CommandParameter="{Binding .}"/>
</ViewCell.ContextActions>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
我不知道如何获取productName并将其发送到购物车页面,因此购物车页面可以从数据库中搜索并显示它。
按钮代码:
async void Add_To_ShoppingCart(object sender, System.EventArgs e)
{
//var product = (sender as Button).CommandParameter as Product;
//var shoppingcartPage = new ShoppingCartPage(product);
//shoppingcartPage.BindingContext = product.ProductID;
//await Navigation.PushAsync(shoppingcartPage);
var product = new Product()
{
ProductName= Guid.NewGuid().ToString()
};
var shoppingcarpage = new ShoppingCartPage(product);
shoppingcarpage.BindingContext = product;
await Navigation.PushAsync(shoppingcarpage);
//var product = HomepageListView.
//await conn.InsertAsync(product);
//_products.Add(product);
}
我尝试过PushAsync,例如点击产品,然后转到产品详细信息页面。我不想创建这样的新页面。只需将商品添加到购物车即可。有什么建议吗?
答案 0 :(得分:0)
假设您将完成制作图像按钮...
<Image
Source="btnadd.png"
HeightRequest="24">
<Image.GestureRecognizers>
<TapGestureRecognizer CommandParameter="{Binding .}" Tapped="BtnAdd_OnTapped" />
</Image.GestureRecognizers>
</Image>
正如您所看到的,您正在将整个产品传递给您点击/单击处理程序。之后,可以轻松将其添加到购物车列表中。
public class CustomParam
{
public Product Parameter { get; set; }
}
private void BtnAdd_OnTapped(object sender, CustomParam e)
{
var product = e.Parameter;
//pls add me to cart now.. plsplspls :)
}