new ContextPopupDirectionPriorities(ContextPopupDirection.Up, ContextPopupDirection.Left, ContextPopupDirection.Right, ContextPopupDirection.Down);
ContextPopupItem _selectedItem = null;
+
bool _isDisposed;
public ContextPopupImplementation()
public event PropertyChangedEventHandler PropertyChanged;
- public event EventHandler SelectedIndexChanged;
+ public event EventHandler ItemSelected;
public event EventHandler Dismissed;
{
foreach (var item in items)
{
- var nativeItem = _items[item];
- nativeItem.Delete();
- _items.Remove(item);
+ if (_items.ContainsKey(item))
+ {
+ var nativeItem = _items[item];
+ nativeItem.Delete();
+ _items.Remove(item);
+ }
}
}
_popup.Clear();
}
- public void Show(XForms.View anchor)
+ public void Show(XForms.View anchor, int xAnchorOffset, int yAnchorOffset)
{
var geometry = XFPlatformTizen.Platform.GetRenderer(anchor).NativeView.Geometry;
- _popup.Move(geometry.X, geometry.Y);
+ _popup.Move(geometry.X + xAnchorOffset, geometry.Y + yAnchorOffset);
_popup.Show();
KeyGrab();
}
nativeItem.Selected += (s, e) =>
{
- if (SelectedItem != item)
- {
- //When SelectedItem is changed, SelectedIndexChanged is invoked.
- SelectedItem = item;
- }
- else
- {
- //If not a newly selected item, invoke SelectedIndexChanged.
- SelectedIndexChanged?.Invoke(this, EventArgs.Empty);
- };
+ SelectedItem = item; // This will invoke SelectedIndexChanged if the index has changed
+ ItemSelected?.Invoke(this, EventArgs.Empty);
};
}
public class ContextPopup : BindableObject
{
IContextPopup _contextPopup;
+
ObservableCollection<ContextPopupItem> _items;
static ContextPopupDirectionPriorities _priorities =
public static readonly BindableProperty ItemsSourceProperty = BindableProperty.Create(nameof(ItemsSource), typeof(IList), typeof(ContextPopup), default(IList),
propertyChanged: OnItemsSourceChanged);
+ /// <summary>
+ /// The constructor which creates a new ContextPopup instance.
+ /// </summary>
public ContextPopup()
{
_contextPopup = DependencyService.Get<IContextPopup>(DependencyFetchTarget.NewInstance);
+
_contextPopup.Dismissed += (s, e) => Dismissed?.Invoke(this, EventArgs.Empty);
- _contextPopup.SelectedIndexChanged += (s, e) => SelectedIndexChanged?.Invoke(this, EventArgs.Empty);
+ _contextPopup.ItemSelected += (s, e) => ItemSelected?.Invoke(this, EventArgs.Empty);
_items = new ObservableCollection<ContextPopupItem>();
_items.CollectionChanged += ItemsCollectionChanged;
public event EventHandler Dismissed;
/// <summary>
- /// Occurs when an item is selected.
+ /// Occurs when the index of the selected ContextPopupItem changes.
/// </summary>
public event EventHandler SelectedIndexChanged;
/// <summary>
+ /// Occurs when a ContextPopupItem is selected.
+ /// </summary>
+ public event EventHandler ItemSelected;
+
+ /// <summary>
/// Gets or sets the orientation of the ContextPopup.
/// The default value is ContextPopupOrientation.Vertical.
/// </summary>
}
/// <summary>
- /// Shows the ContextPopup.
+ /// Shows the ContextPopup. The ContextPopup is positioned at the horizontal and vertical position of a specific anchor.
/// </summary>
/// <param name="anchor">The View to which the popup should be anchored.</param>
public void Show(View anchor)
{
- _contextPopup.Show(anchor);
+ Show(anchor, 0, 0);
+ }
+
+ /// <summary>
+ /// Shows the ContextPopup. The ContextPopup is positioned at the horizontal and vertical position of a specific anchor with offsets.
+ /// </summary>
+ /// <param name="anchor">The View to which the popup should be anchored.</param>
+ /// <param name="xOffset">The horizontal offset from the anchor.</param>
+ /// <param name="yOffset">The vertical offset from the anchor.</param>
+ public void Show(View anchor, int xOffset, int yOffset)
+ {
+ _contextPopup.Show(anchor, xOffset, yOffset);
+ }
+
+ /// <summary>
+ /// Shows the ContextPopup. The ContextPopup is positioned at the horizontal and vertical position of a specific anchor with offsets.
+ /// </summary>
+ /// <param name="anchor">The View to which the popup should be anchored.</param>
+ /// <param name="xOffset">The horizontal offset from the anchor.</param>
+ /// <param name="yOffset">The vertical offset from the anchor.</param>
+ public void Show(View anchor, double xOffset, double yOffset)
+ {
+ Show(anchor, (int)xOffset, (int)yOffset);
}
/// <summary>
var item = ConvertObjectToContextPopupItem(oldItem);
item.PropertyChanged -= ContextPopupItemPropertyChanged;
items.Add(item);
+ if (_items.Contains(item))
+ {
+ _items.Remove(item);
+ }
}
_contextPopup.RemoveItems(items);
var item = ConvertObjectToContextPopupItem(newItem);
item.PropertyChanged += ContextPopupItemPropertyChanged;
items.Add(item);
+ if (!_items.Contains(item))
+ {
+ _items.Add(item);
+ }
}
_contextPopup.AddItems(items);