我已经ListView
在我的xaml页面中创建了,并且正在从其cs页面上添加网格和按钮。当我在手机上运行此listview
代码时,当我将其滚动到底部时,后面的代码将再次自动运行,并再次添加相同的代码listview
。我希望此DataTemplate
代码仅运行一个,当我滚动到底部时,listview
它不应再次执行。不知道为什么它再次执行。
背后的代码
public List<AmanoraSlotsClass> AmanoraSlotsClassesList { get; set; }
public List<HadapsarSlots> HadapsarSlotClassesList { get; set; }
string SelectedLocation = null;
string SelectedDate = null;
Dictionary<string, string> keyValuePairs = new Dictionary<string, string>();
int OuterI = 0;
int innerJ = 0;
public ShowSlotsPage(string _Location, string _Date)
{
this.SelectedLocation = _Location;
this.SelectedDate = _Date;
SelectedDate = SelectedDate.Replace('/', '-');
if (SelectedLocation == "Amanora Town Park")
{
AmanoraSlotsClassesList = new List<AmanoraSlotsClass>();
}
else if (SelectedLocation == "Hadapsar Malwadi")
{
HadapsarSlotClassesList = new List<HadapsarSlots>();
}
else
{
return;
}
FetchData(SelectedLocation, SelectedDate);
InitializeComponent();
BindingContext = this;
//When i reach bottom of my listview this the control jumps to this and again adds same elements.
DataTemplate _dataTemplate = new DataTemplate(() =>
{
Grid _grid = new Grid();
_grid.RowSpacing = 50;
_grid.RowDefinitions.Add(new RowDefinition());
_grid.ColumnDefinitions.Add(new ColumnDefinition());
_grid.ColumnSpacing = 25;
if (SelectedLocation == "Amanora Town Park")
{
OuterI = AmanoraSlotsClassesList[0].GetType().GetProperties().Count();
}
for (int i = 0; i < OuterI - 1; i++)
{
if (SelectedLocation == "Amanora Town Park")
{
innerJ = AmanoraSlotsClassesList.Count();
}
for (int j = 0; j < innerJ; j++)
{
Button _but = new Button();
_but.FontSize = 10;
_but.Scale = 1;
_but.HeightRequest = 70;
if (SelectedLocation == "Amanora Town Park")
{
_but.Text = AmanoraSlotsClassesList[j].S1;
}
keyValuePairs.Add(_but.Id.ToString(), $"S{i + 1}");
// ColorDtermination(_but); //Changes color of button
Grid.SetRow(_but, j);
Grid.SetColumn(_but, i);
_grid.Children.Add(_but);
}
}
var viewCell_ = new ViewCell
{
View = _grid
};
return viewCell_;
}); ///*Till here code runs again when i scroll bottom of listview.*
_listView.ItemTemplate = _dataTemplate;
if(SelectedLocation == "Amanora Town Park")
{
_listView.ItemsSource = AmanoraSlotsClassesList;
}
}
XAML声明列表视图-列表视图的XAML declarion
Xamarin.Forms ListView呈现AmanoraSlotsClassesList
集合中每个项目的项目模板。如果该应用呈现了其他项,则意味着您的收藏集中有那么多项。如果您希望列表仅渲染模板一次,请确保列表具有单个元素。
一些建议:
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句