Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/326.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# Xamarin表单:滚动后点击listview中的项目时消失_C#_Xamarin.forms_Xamarin.forms.listview - Fatal编程技术网

C# Xamarin表单:滚动后点击listview中的项目时消失

C# Xamarin表单:滚动后点击listview中的项目时消失,c#,xamarin.forms,xamarin.forms.listview,C#,Xamarin.forms,Xamarin.forms.listview,我有一个带有viewcell的listview。我想在点击的项目上切换stacklayout背景色,以使其处于选中或不选中状态。问题是,滚动后,该项目在点击时消失滚动后点击打开 这是一个显示问题的GIF 我试图删除项目源列表并再次填充,但当然无效。 我尝试过制作所有类型的CachingStrategy,但背景颜色只在保留上发生了变化 Xamarin表单版本:4.0.0.425677 XAML代码 <StackLayout Margin="20" BackgroundColor="Whit

我有一个带有viewcell的listview。我想在点击的项目上切换stacklayout背景色,以使其处于选中或不选中状态。问题是,滚动后,该项目在点击时消失滚动后点击打开

这是一个显示问题的GIF

我试图删除项目源列表并再次填充,但当然无效。 我尝试过制作所有类型的CachingStrategy,但背景颜色只在保留上发生了变化

Xamarin表单版本:4.0.0.425677

XAML代码

<StackLayout Margin="20" BackgroundColor="White">

        <Grid>
        <ListView x:Name="lstProducts" ItemTapped="lstProducts_ItemTapped" HasUnevenRows="True" ItemsSource="{Binding ProductsList}" 
                  CachingStrategy="RetainElement">
            <ListView.ItemTemplate>
                <DataTemplate>
                    <ViewCell>
                        <StackLayout Orientation="Vertical">
                                <StackLayout Orientation="Horizontal" BackgroundColor="{Binding SelectedStateColor}">
                                <Image Source="{Binding ImageUrl}" WidthRequest="40" HeightRequest="40" HorizontalOptions="Start" />
                                <StackLayout Orientation="Vertical" HorizontalOptions="StartAndExpand">
                                    <Label Text="{Binding title}" FontSize="Large" TextColor="Black" />
                                    <Label Text="{Binding description}" FontSize="Small" TextColor="Gray" />
                                </StackLayout>

                            </StackLayout>
                        </StackLayout>
                    </ViewCell>
                </DataTemplate>
            </ListView.ItemTemplate>
        </ListView>
        </Grid>
        <Button Text="موافق" Clicked="Submit_Clicked" Style="{StaticResource PurpleButton}" Margin="10,0,10,10" />
    </StackLayout>
视图模型

public class Product
{
        public string title { get; set; }
        public string description { get; set; }
        public Color SelectedStateColor
        {
            get
            {
                if (IsSelected == true)
                    return Color.LightGreen;
                else
                    return Color.White;
            }
        }
        public bool IsSelected { get; set; }
}
public class ProductsViewModel : INotifyPropertyChanged
{

public event PropertyChangedEventHandler PropertyChanged;

        protected void OnPropertyChanged([CallerMemberName] string propertyName = null)
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
        }

private ObservableCollection<Product> _productsList = new ObservableCollection<Product>();
        public ObservableCollection<Product> ProductsList
        {
            get { return _productsList; }
            set { _productsList = value;OnPropertyChanged(); }
        }
}

您好,您可以将Xamarin表单更新到最新版本(4.2.0.848062),然后再试一次。你们是否在IOS设备上运行它,不管它是否发生?对不起,我太愚蠢了。只需将xamarin表单更新到4.2.0.709249即可解决此问题。对不起,我的错误很高兴它能起作用,有时候解决办法太不可思议了,我们都有这样的经历。您可以更新它的答案,然后将有助于其他人了解解决方案*^您好,您可以将Xamarin Forms更新到最新版本(4.2.0.848062),然后再试一次。你们是否在IOS设备上运行它,不管它是否发生?对不起,我太愚蠢了。只需将xamarin表单更新到4.2.0.709249即可解决此问题。对不起,我的错误很高兴它能起作用,有时候解决办法太不可思议了,我们都有这样的经历。您可以更新它的答案,然后将有助于其他人了解解决方案*^
public class ProductsPage : ContentPage
{

public ProductsPage()
{
BindingContext = new ProductsViewModel();
            InitializeComponent ();

}
ProductsViewModel Context=> (ProductsViewModel)BindingContext;

private void lstProducts_ItemTapped(object sender, ItemTappedEventArgs e)
        {
            Product product = (Product)e.Item;
            product.IsSelected = !product.IsSelected;


            Context.ProductsList[Context.ProductsList.IndexOf(product)] = product;
        }

}