Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/xamarin/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# Xamarin Forms CollectionView不';不能正常显示_C#_Xamarin_Xamarin.forms_Xamarin.android_Xamarin.ios - Fatal编程技术网

C# Xamarin Forms CollectionView不';不能正常显示

C# Xamarin Forms CollectionView不';不能正常显示,c#,xamarin,xamarin.forms,xamarin.android,xamarin.ios,C#,Xamarin,Xamarin.forms,Xamarin.android,Xamarin.ios,我有个问题。我创建了一个带有自定义ViewPage的CollectionView,以放置带有rowspan的网格。ItemSource是使用ViewModel设置的。现在,当我在CollectionView中滚动时,这些行被随机放置在屏幕上,甚至在彼此内部 以下是我的xaml代码: <CollectionView ItemsSource="{Binding agentOrderList}" Margin="5,0,5,0" HeightRequest="450"> <C

我有个问题。我创建了一个带有自定义ViewPage的CollectionView,以放置带有rowspan的网格。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>

以下是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;
    }
}
公共类VM\u代理页面:INotifyPropertyChanged
{
公共事件属性更改事件处理程序属性更改;
受保护的OnPropertyChanged无效([CallerMemberName]字符串propertyName=null)
{
PropertyChanged?.Invoke(这是新的PropertyChangedEventArgs(propertyName));
}
私人可观察收集(代理目录);;
公共可观察收集代理DerList
{
得到
{
返回代理列表;
}
设置
{
if(_agentOrderList!=值)
{
_agentOrderList=值;
OnPropertyChanged();
}
}
}
公共VM_代理页面()
{
agentOrderList=新的ObservableCollection();
LoadAgentOrders_Handler();
}
公共任务LoadAgentOrders_处理程序()
{
返回LoadAgentOrders();
}
专用异步任务LoadAgentOrders()
{
IsRefreshingAgentOrders=true;
App.AgentOrderList=等待App.RestService.GetAgentOrders(App.AgentId);
ObservableCollection tempAgentOrderList=新的ObservableCollection();
foreach(应用程序AgentOrderList中的OrderBundle OrderBundle)
{
添加(orderBundle);
}
agentOrderList=临时agentOrderList;
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);
        }
    }
}
public分部类OrderView:ContentView
{
网格;
公共秩序视图()
{
初始化组件();
网格=新网格
{
列定义=
{
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;
这个.Content=grid;
this.BindingContextChanged+=View1\u BindingContextChanged;
}
私有void View1\u BindingContextChanged(对象发送方,事件参数e)
{
var obj=this.BindingContext作为OrderBundles;
如果(obj!=null)
{
列表=对象交易;
for(int i=0;i
这是它一开始的样子:

但当我滚动时,它开始如下所示:


这是什么原因?

我发现滚动collectionview时会触发
BindingContextChanged
方法,因此它会不断添加
RowDefinition
和控件,我们应该在开始时清除列表

    private void View1_BindingContextChanged(object sender, EventArgs e)
    {
        //add the following two lines
        grid.RowDefinitions.Clear();
        grid.Children.Clear();

我发现滚动collectionview时会触发
BindingContextChanged
方法,因此它会不断添加
RowDefinition
和控件,我们应该在开始时清除列表

    private void View1_BindingContextChanged(object sender, EventArgs e)
    {
        //add the following two lines
        grid.RowDefinitions.Clear();
        grid.Children.Clear();

您好,您的意思是滚动时会出现这种情况吗?当滚动某些内容时,可能会导致布局更改。由于没有测试数据,如果你可以在这里共享一个示例项目链接,我将在本地站点进行测试,以检查这一点。在这个应用程序中,你可以看到一个随机间隔发生在我的应用程序中:代码数据与我知道的你共享的图片不一样,但这是同一个问题。图片中的数据是来自网络调用的真实数据,需要解析,等等。这是很多代码,所以我只分享了必要的代码,所以你不会对其他东西感到困惑。如果你知道如何修复我共享的应用程序中的问题,我可以在原始代码中修复它!嗨,你的意思是滚动时会发生吗?当滚动某些内容时,可能会导致布局更改。由于没有测试数据,如果你可以在这里共享一个示例项目链接,我将在本地站点进行测试,以检查这一点。在这个应用程序中,你可以看到一个随机间隔发生在我的应用程序中:代码数据与我知道的你共享的图片不一样,但这是同一个问题。图片中的数据是来自网络调用的真实数据,需要解析,等等。这是很多代码,所以我只分享了必要的代码,所以你不会对其他东西感到困惑。如果你知道如何修复我共享的应用程序中的问题,我可以在原始代码中修复它!