你好
我遇到了一个非常烦人的问题,我需要的很简单,但是我遇到了一个意想不到的问题,这是我正在尝试做的事情:
我在ViewCell中有一个包含2个标签的列表视图,两个标签都显示相同的字段,但是区别在于,第一个是可见的,字体是正常的,第二个是不可见的,字体是粗体的,我想做的是当我单击该项目以将名为“ Selected”的属性从false更改为true,反之亦然时,则显示/隐藏粗体标签以通知用户该项目已选中。
为了实现这一点,我编写了代码,然后将简单的应用程序部署到了android设备上,它的工作就像一个魅力,但是在那之后,我注意到,如果我将列表视图滚动到屏幕上未显示的项目,并单击该项目,第一个普通标签消失了,这是预期的行为,但第二个粗体标签未显示
以下是我的代码:
testVisibilityProblem.xaml
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:prism="clr-namespace:Prism.Mvvm;assembly=Prism.Forms"
xmlns:local="clr-namespace:CPALMSStandardsViewer;assembly=CPALMSStandardsViewer"
xmlns:b="clr-namespace:Prism.Behaviors;assembly=Prism.Forms"
xmlns:converters="clr-namespace:CPALMSStandardsViewer.Converters"
xmlns:Helpers="clr-namespace:CPALMSStandardsViewer.Helper"
prism:ViewModelLocator.AutowireViewModel="True"
x:Class="CPALMSStandardsViewer.Views.testVisibilityProblem">
<Grid>
<ListView Margin="10,0,10,0"
BackgroundColor="White"
SeparatorColor="Gray"
ItemsSource="{Binding CustomEntities}"
VerticalOptions="Fill"
SelectionMode="None"
HasUnevenRows="True">
<ListView.Behaviors>
<b:EventToCommandBehavior EventName="ItemTapped"
Command="{Binding SelectCustomEntity}"
EventArgsConverter="{converters:ItemTappedEventArgsConverter}" />
</ListView.Behaviors>
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<Grid>
<Label Grid.Column="0" Grid.Row="0" Text="{Binding Name}" TextColor="Black" IsVisible="{Binding IsSelected, Converter={Helpers:InverseBoolConverter}}"></Label>
<Label Grid.Column="0" Grid.Row="0" Text="{Binding Name}" TextColor="Black" FontAttributes="Bold" IsVisible="{Binding IsSelected}"></Label>
</Grid>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</Grid>
</ContentPage>
testVisibilityProblemViewModel.cs
using CPALMSStandardsViewer.Models;
using Prism.Commands;
using Prism.Mvvm;
using Prism.Navigation;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
namespace CPALMSStandardsViewer.ViewModels
{
public class testVisibilityProblemViewModel : ViewModelBase
{
public ObservableCollection<CustomEntity> CustomEntities { get; set; }
public testVisibilityProblemViewModel(INavigationService navigationService)
: base(navigationService)
{
Title = "Test Visibility Problem!";
CustomEntities = new ObservableCollection<CustomEntity>();
for (int i = 0; i < 50; i++)
{
CustomEntities.Add(new CustomEntity() { Name = "Item " + i, IsSelected = false });
}
}
private DelegateCommand<CustomEntity> _SelectCustomEntity;
public DelegateCommand<CustomEntity> SelectCustomEntity => _SelectCustomEntity ?? (_SelectCustomEntity = new DelegateCommand<CustomEntity>(ExecuteSelectCustomEntityCommand));
private void ExecuteSelectCustomEntityCommand(CustomEntity paramData)
{
paramData.IsSelected = !paramData.IsSelected;
}
}
}
CustomEntity.cs
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Runtime.CompilerServices;
using System.Text;
namespace CPALMSStandardsViewer.Models
{
public class CustomEntity : INotifyPropertyChanged
{
public virtual string Name { get; set; }
private bool _IsSelected;
public bool IsSelected { get { return _IsSelected; } set { _IsSelected = value; OnPropertyChanged("IsSelected"); } }
public event PropertyChangedEventHandler PropertyChanged;
public void OnPropertyChanged([CallerMemberName] string name = "")
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(name));
}
}
}
关于这个问题,我进行了很多搜索,发现所有与OnPropertyChanged()有关的东西,有人错过了可观察的集合,另一个错过了使用OnPropertyChanged()的机会,另一个人以错误的方式使用了它。
但是我的问题不同,这与绑定无关,因为顶级项目按预期工作,而且正如我已经提到的那样,第一个标签被隐藏了,这是预期的,我的问题是当您将IsVisible从false更改为true时,屏幕上未呈现的项目,它根本无法正常工作。
我可能知道这个问题,但是我做了很多尝试来解决它,但是没有一个解决方案对我有用。
请注意,UWP正常工作,我的问题出在android平台上。
请帮助!