Layout cycle detected. Layout could not complete. Layout cycle detected. Layout could not complete

久未见 提交于 2020-07-15 09:39:14

问题


I know there are some questions with same head line, but I couldn't find any answer that worked for me.

Got in Xml file triple ListBox , which was built from 3 inner Observable collection. (List of Registers which contains list of Fields which contains list of int).

The 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>

but when my data is in more than ~60 registers I get this message:

Layout cycle detected. Layout could not complete. Layout cycle detected. Layout could not complete.

Don't know what is it about. The debugger has no information either.

I'm almost sure it has to do with the ScrollViewer because when it is removed, there is no exception. but I need this ScrollViewer, so any ideas to do something with ScrollViewer are welcome too. Thanks.

Edit:

The classes are:

    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;
        }

    }

Filling Registers list is too complicated to add here but for simplification:

    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;
        }
    }

回答1:


I used the ListBox built-in ScrollViewer instead of ScrollViewer layout and it seems to fix the bug.

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

Performance still not good for more than 50 objects, but this is better than exception. When using ListView instead of ListBox performance is better but the built-in ScrollViewer is not shown. So I don't mark this answer as acceptable.

The ListView version:

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



回答2:


I had a few StackLayouts populated via foreach loops. They were each contained within a ScrollViewer of fixed height.

When the content was too great, it started to crash with the obscure 'layout cycle detected' error..

I changed the StackLayouts to Grids, and finally this extremely annoying problem was solved.

Update: 1 year later my application suffered from the problem again. I fixed it by adding a VerticalAlignment property to the offending Grid view...



来源:https://stackoverflow.com/questions/56242888/layout-cycle-detected-layout-could-not-complete-layout-cycle-detected-layout

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!