首页 > 解决方案 > 我试图通过将模型的 Isenabled 属性与 Entry 的 Isenabled 属性绑定来从用户获取输入后禁用该条目

问题描述

当列表开始时工作看起来正确但是一旦列表填充了更多元素,即当它需要滚动更多元素时,该条目将禁用 UI 中的所有剩余值,但是当我检查该特定的模型值时进入它仍然适用于Isenabled财产

这是我的 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"
x:Class="MyFirstApp.Demo">
<ContentPage.Content>
<StackLayout>
<StackLayout>
<Button Text="Add" Clicked="Button_Clicked"/>
</StackLayout>
<ListView x:Name="DimensionListView" HasUnevenRows="True" ItemsSource="{Binding List}" >
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell >
<Grid>
<Grid x:Name="DimensionsGrid"  RowSpacing="0" ColumnSpacing="0" Grid.Row="0" Grid.Column="0" Margin="0,0,0,0" >
<Grid.RowDefinitions>
<RowDefinition />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="1*" />
<ColumnDefinition Width="1*" />
<ColumnDefinition Width="1*"/>
<ColumnDefinition Width="1*"/>
<ColumnDefinition Width ="0.9*"/>
</Grid.ColumnDefinitions>
<Entry  Text="{Binding Pieces }" IsEnabled="{Binding Isenabled}" Keyboard="Numeric" Placeholder="0"  Grid.Column="0"   Grid.Row="0" BackgroundColor="White"   TextColor="Black" FontSize="Small" Margin="0,0,0,0" HorizontalTextAlignment="Center"/>
<Entry  Text="{Binding Length}" IsEnabled="{Binding Isenabled}" Keyboard="Numeric"  Placeholder="0"   Grid.Column="1"   Grid.Row="0" BackgroundColor="White"   TextColor="Black" FontSize="Small" Margin="0,0,0,0" HorizontalTextAlignment="Center"/>
<Entry  Text="{Binding Height }" IsEnabled="{Binding Isenabled}" Keyboard="Numeric" Placeholder="0"  Grid.Column="2"   Grid.Row="0" BackgroundColor="White"   TextColor="Black" FontSize="Small" Margin="0,0,0,0" HorizontalTextAlignment="Center"/>
<Entry  Text="{Binding Width}" IsEnabled="{Binding Isenabled}" Keyboard="Numeric" Placeholder="0"   Grid.Column="3"   Grid.Row="0" BackgroundColor="White"   TextColor="Black" FontSize="Small" Margin="0,0,0,0" HorizontalTextAlignment="Center"/>
</Grid>
</Grid>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</StackLayout>
</ContentPage.Content>
</ContentPage>

我的代码背后:

namespace MyFirstApp
{
[XamlCompilation(XamlCompilationOptions.Compile)]
public partial class Demo : ContentPage
{
ShipmentViewModel ViewList = new ShipmentViewModel();
public Demo()
{
InitializeComponent();
ViewList.ShimpentDimsList = new ObservableCollection<VolumeCalculator>() { new VolumeCalculator() { Height = 0, Length = 0, Pieces = 0, Width = 0, Isenabled = true } };
DimensionListView.ItemsSource = ViewList.ShimpentDimsList;
}
private void Button_Clicked(object sender, EventArgs e)
{
ViewList.ShimpentDimsList.LastOrDefault().Isenabled = false;
ViewList.ShimpentDimsList.Add(new VolumeCalculator() { Pieces = 0, Height = 0, Width = 0, Length = 0, Isenabled = true });
ObservableCollection<VolumeCalculator> TempList = ViewList.ShimpentDimsList;
}
}
}

我的模型:

namespace MyFirstApp
{
class VolumeCalculator : INotifyPropertyChanged
{
private int pieces;
public int Pieces {
get { return pieces; }
set
{
pieces = value;
OnPropertyChanged("Pieces");
}
}
private int width;
public int Width
{
get { return width; }
set
{
width = value;
OnPropertyChanged("Width");
}
}
private int height;
public int Height
{
get { return height; }
set
{
height = value;
OnPropertyChanged("Height");
}
}
private int length;
public int Length
{
get { return length; }
set
{
length = value;
OnPropertyChanged("Length");
}
}
private bool isenabled ;
public bool Isenabled {
get { return isenabled; }
set
{
isenabled = value;
OnPropertyChanged("Isenabled"); 
}
}


        public event PropertyChangedEventHandler PropertyChanged;
        protected virtual void OnPropertyChanged(string propertyName)
        {
            PropertyChangedEventHandler handler = PropertyChanged;
            if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));
        }
    }
}

我的视图模式:

namespace MyFirstApp
{
class ShipmentViewModel : INotifyPropertyChanged
{
private ObservableCollection<VolumeCalculator> shimpentDimsList;
public ObservableCollection<VolumeCalculator> ShimpentDimsList
{
get { return shimpentDimsList; }
set {
shimpentDimsList = value;
OnPropertyChanged("ShimpentDimsList");
}
}
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void OnPropertyChanged(string propertyName)
{
PropertyChangedEventHandler handler = PropertyChanged;
if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));
}
}
}

标签: xamarin.forms

解决方案


原因: 它是由 ListView 的缓存策略引起的。为了节省内存,每个平台的原生 ListView 等效项都具有用于重用行的内置功能。只有屏幕上可见的单元格被加载到内存中,内容被加载到现有的单元格中。这种模式可以防止应用程序实例化数千个对象,从而节省时间和内存。

解决方案: 在您的情况下,您需要将缓存策略设置为RecycleElement

<ListView CachingStrategy="RecycleElement" x:Name="DimensionListView" HasUnevenRows="True"  >

推荐阅读