Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Carousel control crashes on addition without initial items #905

Closed
SamirKharchi opened this issue Jul 13, 2021 · 6 comments
Closed

Carousel control crashes on addition without initial items #905

SamirKharchi opened this issue Jul 13, 2021 · 6 comments
Labels
🤔 pending discussion It may take time to discuss.

Comments

@SamirKharchi
Copy link

SamirKharchi commented Jul 13, 2021

Describe the bug
The carousel control crashes when it has no items initially and one adds items afterwards.

To Reproduce

  1. Add a carousel without any initial items
  2. Add an item (e.g. via code) once the view has been initialised.
  3. Crashes with an Out-of-bounds exception

Expected behavior
No crash.

Additional context
Debugging shows that Carousel.UpdateItemsPosition() is crashing once it tries to use the PageIndex, which is -1 at that point and therefore of course out-of-bounds.

Possible fix is to update the PageIndex to an in-bounds range value when an item is added.

private void UpdateItemsPosition()
{
    if (!CheckNull() || !_appliedTemplate || Items.Count == 0)
        return;

    if (PageIndex == -1) // Fixes the crash
    {
         PageIndex = 0;
    }
@NaBian
Copy link
Member

NaBian commented Jul 16, 2021

can you provide a demo, i can not reproduce this issue

@SamirKharchi
Copy link
Author

Hi,

I am on holidays. I will provide a demo once I am back on my machine.

@NaBian
Copy link
Member

NaBian commented Aug 27, 2021

Hi,

I am on holidays. I will provide a demo once I am back on my machine.

hello?

@ghost1372 ghost1372 added the 🤔 pending discussion It may take time to discuss. label Oct 19, 2021
@ghost1372
Copy link
Member

@SamirKharchi Are you back?😅

@SamirKharchi
Copy link
Author

Hi guys!

sorry for the delay (had tons of work crashing on me after holidays, then it slipped my mind).
I had the time yesterday to strip down our crashing code base and once I have stripped it down, the crash is not reproducable anymore I'm afraid.
However, in our full blown code it still does crash. It might be a threading issue on our side or something to do with Prism (which we are using) I guess, because I cannot reproduce it on a clean project or the demo project of HandyControl either.

I keep it in our version of HandyControl but it probably is not a necessary fix. So rry for keeping this pending for so long.

@810906591
Copy link

ItemsSource 发生变化后
private void UpdateItemsPosition()
{
if (!CheckNull() || !_appliedTemplate || Items.Count == 0)
return;
if (PageIndex == -1) // Fixes the crash
{
PageIndex = 0;
}
if (!IsCenter)
{
ItemsHost.BeginAnimation(MarginProperty,
AnimationHelper.CreateAnimation(new Thickness(-_widthList[PageIndex], 0, 0, 0)));
}
else
{
var ctl = (FrameworkElement) ItemsHost.Children[PageIndex];
var ctlWidth = ctl.DesiredSize.Width;
ItemsHost.BeginAnimation(MarginProperty,
AnimationHelper.CreateAnimation(
new Thickness(-_widthList[PageIndex] + (ActualWidth - ctlWidth) / 2, 0, 0, 0)));
}
}
这时, var ctl = (FrameworkElement) ItemsHost.Children[PageIndex];会报越界。
原先ItemsSource 集合有多条数据,PageIndex=1(非0);这时ItemsSource 集合只剩下1条数据,这个时候就报索引越界了

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
🤔 pending discussion It may take time to discuss.
Projects
None yet
Development

No branches or pull requests

4 participants