简体   繁体   English

清除 Xamarin Forms 中的选择

[英]Clear a selection in Xamarin Forms

I'm trying to clear a selection in Xamarin Forms because the ListView keeps populating all my selections.我正在尝试清除 Xamarin Forms 中的选择,因为 ListView 不断填充我的所有选择。

How do I make it so that after i've selected an item, it will pass the selection to the next page and reset the selection?如何做到这一点,以便在我选择一个项目后,它将选择传递到下一页并重置选择? Right now it's going to the next page and then a second later it refreshes and clears the page.现在它会转到下一页,然后一秒钟后它会刷新并清除页面。

Here's the ProductPage.xaml.cs when they select a product.这是 ProductPage.xaml.cs 当他们 select 一个产品。

    async void OnItemSelected(object sender, SelectedItemChangedEventArgs e)
    {

        var item = (Product)e.SelectedItem;

        await Navigation.PushAsync(new ProductDetailPage(item));

        ((ListView)sender).SelectedItem = null;

    }

Here's the ProductDetailPage.xaml.cs这是 ProductDetailPage.xaml.cs

public ProductDetailPage(Product products)
        {

            InitializeComponent();

            BindingContext = products;
            
            MessagingCenter.Subscribe<ProductDetailPage>(this, "AddToCartProduct", (sender) =>
            {
                ShoppingCart.data.Add(products.title);

                DisplayAlert("Added to Cart!", products.title, "OK");

            });

        }

Here's my ViewShoppingCart.xaml.cs这是我的视图ShoppingCart.xaml.cs

public ViewShoppingCart()
        {
            InitializeComponent();

            

            MyListView.ItemsSource = ShoppingCart.data;

        }

Did you set the BindingMode for SelectedItem to TwoWay ?您是否将SelectedItemBindingMode设置为TwoWay

<ListView 
    ItemsSource="{Binding CustomList}"
    SelectedItem="{Binding CustomSelectedItemProperty, Mode=TwoWay}"
</ListView>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM