![](/img/trans.png)
[英]Xamarin Forms CollectionView doesn't have KeyboardDismissMode property
[英]Xamarin Forms CollectionView doesn't show properly
我有個問題。 我創建了一個帶有自定義 ViewPage 的 CollectionView 來放置具有行跨度的網格。 ItemSource 是使用 ViewModel 設置的。 現在,當我在 CollectionView 中滾動時,行會隨機放置在屏幕上,甚至在彼此之間。
這是我的 xaml 代碼:
<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>
這是視圖模型:
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:
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);
}
}
}
這是什么原因造成的?
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.