ProgressRing can cause Layout cycle error

ProgressRing can cause Layout cycle error

Recently, during development of our Windows 8 store application (XAML+C#), we’ve stumbled across tricky error: in some circumstances we constantly received Layout cycle error.

The common reason of this error is infinite loop during the lying out of page content by WPF engine. One way you can get into such trouble is subscribe to SizeChanged event and in the event handler of this event try to change the size of sender control.

That was the first thing we’ve checked, but with no luck.

After spending more hours and several nights the cause has been found, it was the following piece of code:

<ProgressRing 
    IsActive="{Binding IsLoading}" 
    Style="{StaticResource ProgressRingStyle}"
    VerticalAlignment="Center" />

Such progress ring was a part of bigger control, which was placed all over the page. Despite the fact that during the page load it is not active, it causes the Layout cycle error. The actual issue mechanics is not quite clear for us yet.

The fix of this issue was no less interesting that the cause itself, the following change helped:

<ProgressRing 
    IsActive="{Binding IsLoading}" 
    Style="{StaticResource ProgressRingStyle}"
    VerticalAlignment="Center" 
    Visibility="Collapsed" />

According to MSDN:

A UIElement that is not visible is not rendered and does not communicate its desired size to layout.

Even though the visibility is set to collapsed, when the IsActive property is set to true, the control is visible as usually, so we did not need to change visibility of the control along with changing of active status.

Hope this info can save some time and nerves :).

As Solution Architect, Maxim leads multiple projects through the development process at Sigma Ukraine. He is expert in solutions designing, .NET and database development with more than 14 years of experience in IT. Interested in: event sourcing, CQRS, distributed systems, no-SQL dbs. Author's page

Leave a reply

Blue Captcha Image
Refresh

*