问题描述
我有问题.我创建了一个具有自定义ViewPage的CollectionView,以放置带有行距的网格.使用ViewModel设置ItemSource.现在,当我在CollectionView中滚动时,行被随机放置在屏幕上,甚至彼此都放置在屏幕上.
I have a problem. I created a CollectionView with a custom ViewPage to place a grid with rowspan. The ItemSource is set using a ViewModel. Now when I scroll in the CollectionView the rows are randomly placed on the screen, even inside each other.
这是我的xaml代码:
Here is my xaml code:
<CollectionView ItemsSource="{Binding agentOrderList}" Margin="5,0,5,0" HeightRequest="450">
<CollectionView.ItemsLayout>
<LinearItemsLayout Orientation="Vertical" ItemSpacing="0"/>
</CollectionView.ItemsLayout>
<CollectionView.ItemTemplate>
<DataTemplate>
<local:OrderView BindingContext="{Binding }"/>
</DataTemplate>
</CollectionView.ItemTemplate>
</CollectionView>
这是ViewModel:
Here is the ViewModel:
public class VM_AgentsPage : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
protected void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
private ObservableCollection<OrderBundle> _agentOrderList;
public ObservableCollection<OrderBundle> agentOrderList
{
get
{
return _agentOrderList;
}
set
{
if (_agentOrderList != value)
{
_agentOrderList = value;
OnPropertyChanged();
}
}
}
public VM_AgentsPage()
{
agentOrderList = new ObservableCollection<OrderBundle>();
LoadAgentOrders_Handler();
}
public Task LoadAgentOrders_Handler()
{
return LoadAgentOrders();
}
private async Task LoadAgentOrders()
{
IsRefreshingAgentOrders = true;
App.AgentOrderList = await App.RestService.GetAgentOrders(App.AgentId);
ObservableCollection<OrderBundle> tempAgentOrderList = new ObservableCollection<OrderBundle>();
foreach (OrderBundle orderBundle in App.AgentOrderList)
{
tempAgentOrderList.Add(orderBundle);
}
agentOrderList = tempAgentOrderList;
IsRefreshingAgentOrders = false;
}
}
最后是OrderView.xaml.cs:
And finally the OrderView.xaml.cs:
public partial class OrderView : ContentView
{
Grid grid;
public OrderView()
{
InitializeComponent();
grid = new Grid
{
ColumnDefinitions =
{
new ColumnDefinition{ Width = new GridLength(1, GridUnitType.Star)},
new ColumnDefinition{ Width = new GridLength(1, GridUnitType.Star) },
new ColumnDefinition{ Width = new GridLength(1, GridUnitType.Star) },
new ColumnDefinition{ Width = new GridLength(1, GridUnitType.Star) },
new ColumnDefinition{ Width = new GridLength(1, GridUnitType.Star) }
}
};
grid.RowSpacing = 0;
this.Content = grid;
this.BindingContextChanged += View1_BindingContextChanged;
}
private void View1_BindingContextChanged(object sender, EventArgs e)
{
var obj = this.BindingContext as OrderBundles;
if (obj != null)
{
List<Trade> list = obj.Trades;
for(int i = 0; i < list.Count; i++)
{
grid.RowDefinitions.Add(new RowDefinition { Height = new GridLength(20) });
Trade trade = list[i];
grid.Children.Add(new Label
{
Text = trade.Date
}, 0, i);
grid.Children.Add(new Label
{
Text = trade.Action
}, 1, i);
grid.Children.Add(new Label
{
Text = trade.Coin
}, 2, i);
grid.Children.Add(new Label
{
Text = trade.Price.ToString()
}, 3, i);
}
Label label = new Label
{
Text = list[0].ProfitUSDT,
TextColor = Color.Green ,
VerticalTextAlignment = TextAlignment.Center
};
grid.Children.Add(label, 4, 0);
Grid.SetRowSpan(label, list.Count);
}
}
}
这是开头的样子:
但是当我滚动时,它开始看起来像这样:
But when I scroll it starts looking like this:
是什么原因造成的?
推荐答案
我发现BindingContextChanged
方法在滚动collectionview时触发,因此它会不断添加RowDefinition
和控件,我们应该在开始时清除列表. >
I found BindingContextChanged
method triggers when scroll the collectionview , so it keeps adding RowDefinition
and controls , we should clear the list at the beginning .
private void View1_BindingContextChanged(object sender, EventArgs e)
{
//add the following two lines
grid.RowDefinitions.Clear();
grid.Children.Clear();
这篇关于Xamarin Forms CollectionView无法正确显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!