2017-09-23 150 views
0

我是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> 

我不知道如何讓產品名稱和發送的購物車頁面,所以在購物車頁面可以從數據庫並顯示搜索。

按鈕代碼:

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

您可能想要查看購物車中的參考應用程序是如何工作的,eShopOnContainers,github.com/dotnet-architecture/eShopOnContainers'開放eShopOnContainers,MobileApps .sln只包含客戶端移動應用程序項目的解決方案(僅限Xamarin移動應用程序)。它也可以基於mocks獨立工作。' – SushiHangover

回答

0

比方說,你會完成製作的圖像按鈕...

<Image 
    Source="btnadd.png" 
    HeightRequest="24"> 
    <Image.GestureRecognizers> 
     <TapGestureRecognizer CommandParameter="{Binding .}" Tapped="BtnAdd_OnTapped" /> 
    </Image.GestureRecognizers> 
</Image> 

正如你可以看到你傳遞的整個產品你點擊/ Click處理程序。之後,它很容易將它添加到您的購物車清單..

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 :) 
} 
+0

非常感謝,我通過將數據存儲到數據庫並從另一頁讀取數據。 hhhhhh,現在我有了一個新的名字shoppingcart〜 –

相關問題