Browse Source
Merge pull request #1655 from AvaloniaUI/fix/crash-carousel-null-items
fix exception when carousel items set to null.
pull/1669/head
Jumar Macato
8 years ago
committed by
GitHub
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with
50 additions and
1 deletions
-
src/Avalonia.Controls/Presenters/CarouselPresenter.cs
-
tests/Avalonia.Controls.UnitTests/CarouselTests.cs
|
|
|
@ -126,7 +126,21 @@ namespace Avalonia.Controls.Presenters |
|
|
|
generator.Clear(); |
|
|
|
Panel.Children.RemoveAll(containers.Select(x => x.ContainerControl)); |
|
|
|
|
|
|
|
MoveToPage(-1, SelectedIndex >= 0 ? SelectedIndex : 0); |
|
|
|
var newIndex = SelectedIndex; |
|
|
|
|
|
|
|
if(SelectedIndex < 0) |
|
|
|
{ |
|
|
|
if(Items != null && Items.Count() > 0) |
|
|
|
{ |
|
|
|
newIndex = 0; |
|
|
|
} |
|
|
|
else |
|
|
|
{ |
|
|
|
newIndex = -1; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
MoveToPage(-1, newIndex); |
|
|
|
} |
|
|
|
break; |
|
|
|
} |
|
|
|
|
|
|
|
@ -170,6 +170,41 @@ namespace Avalonia.Controls.UnitTests |
|
|
|
Assert.Equal("Bar", ((TextBlock)child).Text); |
|
|
|
} |
|
|
|
|
|
|
|
[Fact] |
|
|
|
public void Selected_Index_Changes_To_When_Items_Assigned_Null() |
|
|
|
{ |
|
|
|
var items = new ObservableCollection<string> |
|
|
|
{ |
|
|
|
"Foo", |
|
|
|
"Bar", |
|
|
|
"FooBar" |
|
|
|
}; |
|
|
|
|
|
|
|
var target = new Carousel |
|
|
|
{ |
|
|
|
Template = new FuncControlTemplate<Carousel>(CreateTemplate), |
|
|
|
Items = items, |
|
|
|
IsVirtualized = false |
|
|
|
}; |
|
|
|
|
|
|
|
target.ApplyTemplate(); |
|
|
|
target.Presenter.ApplyTemplate(); |
|
|
|
|
|
|
|
Assert.Single(target.GetLogicalChildren()); |
|
|
|
|
|
|
|
var child = target.GetLogicalChildren().Single(); |
|
|
|
|
|
|
|
Assert.IsType<TextBlock>(child); |
|
|
|
Assert.Equal("Foo", ((TextBlock)child).Text); |
|
|
|
|
|
|
|
target.Items = null; |
|
|
|
|
|
|
|
var numChildren = target.GetLogicalChildren().Count(); |
|
|
|
|
|
|
|
Assert.Equal(0, numChildren); |
|
|
|
Assert.Equal(-1, target.SelectedIndex); |
|
|
|
} |
|
|
|
|
|
|
|
[Fact] |
|
|
|
public void Selected_Index_Is_Maintained_Carousel_Created_With_Non_Zero_SelectedIndex() |
|
|
|
{ |
|
|
|
|