}
public int Id => id;
+
+ public string Text => $"Item {Id}";
}
[Preserve(AllMembers = true)]
public ItemView()
{
var idLabel = new Label() { StyleId = "id", TextColor = Color.White };
- idLabel.SetBinding(Label.TextProperty, nameof(Item.Id));
+ idLabel.SetBinding(Label.TextProperty, nameof(Item.Text));
var stackLayout = new StackLayout
{
Children = {
- //new Label { Text = "Target" },
- //new Label { Text = "Stack" }
+ new Label { Text = "Target" },
+ new Label { Text = "Stack" }
},
BackgroundColor = Color.Red
};
{
Orientation = StackOrientation.Horizontal,
HorizontalOptions = LayoutOptions.FillAndExpand,
- Children = {
- CreateButton ("<<", () => carouselView.Position = 0),
- CreateButton ("<", () => { try { carouselView.Position--; } catch { } }),
- CreateButton (">", () => { try { carouselView.Position++; } catch { } }),
- CreateButton (">>", () => carouselView.Position = Items.Count - 1)
- }
+ Children =
+ {
+ CreateButton("+", () => Items.Add(new Item())),
+ CreateButton("<<", () => carouselView.Position = 0),
+ CreateButton("<", () =>
+ {
+ try
+ {
+ carouselView.Position--;
+ }
+ catch
+ {
+ }
+ }),
+ CreateButton(">", () =>
+ {
+ try
+ {
+ carouselView.Position++;
+ }
+ catch
+ {
+ }
+ }),
+ CreateButton(">>", () => carouselView.Position = Items.Count - 1)
+ }
};
Content = new StackLayout
{
- Children = {
+ Children =
+ {
carouselView,
moveBar
}
#if UITEST
//[Test]
- public void CarouselViewTest ()
+ public void CarouselViewTest()
{
var app = RunningApp;
- app.Screenshot ("I am at Issue 1");
- app.WaitForElement (q => q.Marked ("Remove"));
+ app.WaitForElement(q => q.Marked("Item 0"));
+ app.SwipeRightToLeft();
+ app.WaitForElement(q => q.Marked("Item 1"));
+ app.Tap(c => c.Marked("<"));
+ app.WaitForElement(q => q.Marked("Item 0"));
+ }
- app.Screenshot ("I see the Label");
+ [Test]
+ public void CarouselViewTestAddItem()
+ {
+ var app = RunningApp;
+ app.WaitForElement(q => q.Marked("Hide Target Stack"));
+ app.Tap(c => c.Marked("+"));
+ app.SwipeRightToLeft();
+ app.SwipeRightToLeft();
+ app.WaitForElement(q => q.Marked("Item 2"));
+ app.Screenshot("I see the Item 2");
}
#endif
}
var dynamicItemSource = _itemSource as INotifyCollectionChanged;
if (dynamicItemSource != null)
{
- new WeakNotifyCollectionChanged(dynamicItemSource, OnCollectionChange);
+ new WeakNotifyCollectionChanged(this, dynamicItemSource);
}
}
internal event NotifyCollectionChangedEventHandler CollectionChanged;
- void OnCollectionChange(object sender, NotifyCollectionChangedEventArgs e)
+ internal void OnCollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
{
CollectionChanged?.Invoke(sender, e);
}
sealed class WeakNotifyCollectionChanged
{
- readonly INotifyCollectionChanged _source;
- // prevent the itemSource from keeping the itemsView alive
- readonly WeakReference<NotifyCollectionChangedEventHandler> _weakTarget;
+ readonly WeakReference<INotifyCollectionChanged> _weakCollection;
+ readonly WeakReference<ItemsView> _weakSource;
- public WeakNotifyCollectionChanged(INotifyCollectionChanged source, NotifyCollectionChangedEventHandler target)
+ public WeakNotifyCollectionChanged(ItemsView source, INotifyCollectionChanged incc)
{
- _weakTarget = new WeakReference<NotifyCollectionChangedEventHandler>(target);
- _source = source;
- _source.CollectionChanged += OnCollectionChanged;
+ incc.CollectionChanged += OnCollectionChanged;
+
+ _weakSource = new WeakReference<ItemsView>(source);
+ _weakCollection = new WeakReference<INotifyCollectionChanged>(incc);
}
- public void OnCollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
+ void OnCollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
{
- NotifyCollectionChangedEventHandler weakTarget;
- if (!_weakTarget.TryGetTarget(out weakTarget))
+ ItemsView source;
+ if (!_weakSource.TryGetTarget(out source))
{
- _source.CollectionChanged -= OnCollectionChanged;
+ INotifyCollectionChanged collection;
+ if (_weakCollection.TryGetTarget(out collection))
+ collection.CollectionChanged -= OnCollectionChanged;
+
return;
}
- weakTarget(sender, e);
+ source.OnCollectionChanged(sender, e);
}
}