检测到布局周期。布局无法完成​​。检测到布局周期。布局无法完成

时间:2019-05-21 16:42:44

标签: c# xaml uwp

我知道有些标题相同的问题,但是找不到适合我的答案。

获得Xml文件Triple ListBox,它是由3个内部Observable集合构建的。 (包含有包含int列表的字段列表的寄存器列表)。

Xaml:

<ScrollViewer HorizontalScrollBarVisibility="Auto">  
        <Grid HorizontalAlignment="Stretch" VerticalAlignment="Stretch">
            <Grid.RowDefinitions>
                <RowDefinition Height="60"/>
                <RowDefinition Height="*"/>
                <RowDefinition MaxHeight="50"/>
            </Grid.RowDefinitions>

            <ListBox x:Name="RegistersListView" ItemsSource="{x:Bind registersList}" Grid.Row="1">
                <ListBox.ItemTemplate>
                    <DataTemplate x:DataType="structures:Register">
                        <Grid>
                            <Grid.RowDefinitions>
                                <RowDefinition/>
                                <RowDefinition/>
                            </Grid.RowDefinitions>
                            <TextBlock Text="{x:Bind name}" Grid.Row="0" />

                            <ListBox x:Name="FieldsListView" ItemsSource="{x:Bind reg_fields}" Grid.Row="1">
                                <ListBox.ItemTemplate>
                                    <DataTemplate x:DataType="structures:Field">
                                        <StackPanel>
                                            <TextBlock Text="{x:Bind name}"/>

                                            <ListBox x:Name="BitsListView" ItemsSource="{x:Bind bitsList}">
                                                <ListBox.ItemsPanel>
                                                    <ItemsPanelTemplate>
                                                        <StackPanel Orientation="Horizontal"/>
                                                    </ItemsPanelTemplate>
                                                </ListBox.ItemsPanel>
                                            </ListBox>

                                        </StackPanel>
                                    </DataTemplate>
                                </ListBox.ItemTemplate>
                                <ListBox.ItemsPanel>
                                    <ItemsPanelTemplate>
                                        <StackPanel Orientation="Horizontal" />
                                    </ItemsPanelTemplate>
                                </ListBox.ItemsPanel>
                            </ListBox>

                        </Grid>
                    </DataTemplate>
                </ListBox.ItemTemplate>
            </ListBox>

        </Grid>
    </ScrollViewer>

但是当我的数据存储在约60个以上的寄存器中时,会显示以下消息:

  

检测到布局周期。布局无法完成​​。布局周期   检测到。布局无法完成​​。

不知道是关于什么的。调试器也没有信息。

我几乎可以确定它与ScrollViewer有关,因为将其删除时也不例外。但是我需要这个ScrollViewer,所以也欢迎任何使用ScrollViewer进行操作的想法。 谢谢。

编辑:

这些类是:

    public class Field : INotifyPropertyChanged
    {
        public string name;
        public int offset;
        public int length;
        public string description;
        private UInt64 _value;
        private ObservableCollection<int> bitsList = new ObservableCollection<int>();

        public ObservableCollection<int> BitsList
        {
            get
            {
                return new ObservableCollection<int>(bitsList);
            }
            set
            {
               //todo
            }
        }


        public event PropertyChangedEventHandler PropertyChanged;
        public void OnPropertyChanged(string propertyName)
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
        }

        public Field(string _name)
        {
            name = _name;
        }

        override public string ToString()
        {
            return name;
        }

        public void Value(UInt64 value)
        {
            _value = value;
#pragma warning disable CS4014
            Windows.ApplicationModel.Core.CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
            {
                for (int i = 0; i < length; i++)
                {
                    bitsList.Add(Convert.ToInt32(value & 1));
                    value = value >> 1;
                }
                OnPropertyChanged("BitsList");
            });
#pragma warning restore CS4014
        }
    }

    public class Register
    {
        public string name;
        public UInt64 _deafult_value;
        public UInt64 value;
        public int offset;
        public int index;
        public string description;
        public int register_size;
        public ObservableCollection<Field> reg_fields = new ObservableCollection<Field>();

        public Register(string _name)
        {
            name = _name;
        }

    }

填充寄存器列表太复杂了,无法在此处添加,只是为了简化:

    public ObservableCollection<Register> registersList = new ObservableCollection<Register>();
    private void InnerDataCreator()
        {
            Instances instances = new Instances();
            registersList = instances.PopulateRegistersData();
        }

public ObservableCollection<Register> PopulateRegistersData()
        {
            const int REG_AMOUNT = 100;
            const int REG_SIZE = 32;
            ObservableCollection<Register> registers = new ObservableCollection<Register>();


            for (int regIndex = 0; regIndex < REG_AMOUNT; regIndex++)
            {
                Register register = new Register("reg_" + regIndex.ToString());

                register.description = "register description _***_ " + regIndex.ToString();

                register.register_size = REG_SIZE;

                ObservableCollection<Field> fields = new ObservableCollection<Field>();

                int offset = 0;
                /* 4 fields in each register */
                for (int fieldNum = 0; fieldNum < 4; fieldNum++)
                {
                    string fieldName;
                    if(regIndex < REG_AMOUNT / 2)
                    {
                        fieldName = "reg_" + regIndex.ToString() + " Field_" + fieldNum.ToString();
                    }
                    else
                    {
                        fieldName = "################ reg_" + regIndex.ToString() + " Field_" + fieldNum.ToString() + "###################";
                    }

                    Field field = new Field(fieldName);

                    field.description = "field description. reg: " + regIndex.ToString() + ". field: " + fieldNum.ToString();
                    field.length = 8;
                    field.offset = offset;
                    field.Value(BitConverter.GetBytes(170)[0]); /* 10101010 */

                    register.reg_fields.Add(field);

                    offset += field.length;
                }


                registers.Add(register);
            }

            return registers;
        }
    }

2 个答案:

答案 0 :(得分:0)

我使用的是ListBox内置的ScrollViewer而不是ScrollViewer布局,它似乎可以解决该错误。

<ListBox x:Name="RegistersListView" ItemsSource="{x:Bind registersList}" Grid.Row="1" 
             ScrollViewer.HorizontalScrollBarVisibility="Visible" ScrollViewer.HorizontalScrollMode="Enabled">

对于50个以上的对象,性能仍然不好,但这比异常要好。当使用ListView而不是ListBox时,性能会更好,但不会显示内置ScrollViewer。因此,我不会将此答案标记为可接受。

ListView版本:

<ListView x:Name="RegistersListView" ItemsSource="{x:Bind registersList}" Grid.Row="1"
             ScrollViewer.HorizontalScrollBarVisibility="Visible" ScrollViewer.HorizontalScrollMode="Enabled"

答案 1 :(得分:0)

我通过StackLayout循环填充了几个foreach。它们每个都包含在固定高度的ScrollViewer中。

当内容太大时,它开始崩溃,并出现模糊的“检测到布局周期”错误。

我将StackLayout更改为Grid,最后解决了这个非常烦人的问题。