public static async Task SetCommandBarFlyoutWithExtraContextMenuItems(this ListViewBase ListControl, CommandBarFlyout Flyout, Windows.Foundation.Point?ShowAt = null) { if (Flyout == null) { throw new ArgumentNullException(nameof(Flyout), "Argument could not be null"); } if (FullTrustProcessController.Current.IsNowHasAnyActionExcuting) { return; } try { ListControl.ContextFlyout = null; string SelectedPath; if (ListControl.SelectedItems.Count <= 1) { if (ListControl.SelectedItem is FileSystemStorageItemBase Selected) { SelectedPath = Selected.Path; } else if (ListControl.FindParentOfType <FileControl>() is FileControl Control) { if (!string.IsNullOrEmpty(Control.CurrentFolder?.Path)) { SelectedPath = Control.CurrentFolder.Path; } else { return; } } else { return; } List <ContextMenuItem> ExtraMenuItems = await FullTrustProcessController.Current.GetContextMenuItemsAsync(SelectedPath, Window.Current.CoreWindow.GetKeyState(VirtualKey.Shift).HasFlag(CoreVirtualKeyStates.Down)).ConfigureAwait(true); if (ExtraMenuItems.Count > 0) { if (Flyout.SecondaryCommands.OfType <AppBarElementContainer>().FirstOrDefault() is AppBarElementContainer ExistsContainer) { StackPanel InnerPanel = ExistsContainer.Content as StackPanel; List <ContextMenuItem> MenuExistItems = InnerPanel.Children.Select((Btn) => (Btn as Button).Tag as ContextMenuItem).ToList(); foreach (ContextMenuItem AddItem in ExtraMenuItems.Except(MenuExistItems)) { Button Btn = await AddItem.GenerateUIButton().ConfigureAwait(true); Btn.Click += async(s, e) => { Flyout?.Hide(); if (((Button)s)?.Tag is ContextMenuItem MenuItem) { await MenuItem.Invoke().ConfigureAwait(true); } }; InnerPanel.Children.Add(Btn); } foreach (ContextMenuItem RemoveItem in MenuExistItems.Except(ExtraMenuItems)) { if (InnerPanel.Children.OfType <Button>().FirstOrDefault((Item) => (Item.Tag as ContextMenuItem) == RemoveItem) is Button Btn) { InnerPanel.Children.Remove(Btn); } } foreach (ContextMenuItem UpdateItem in MenuExistItems.Where((Item) => ExtraMenuItems.Any((Extra) => Extra.Equals(Item)))) { UpdateItem.UpdateBelonging(SelectedPath); } } else { StackPanel Panel = new StackPanel { HorizontalAlignment = HorizontalAlignment.Stretch }; foreach (ContextMenuItem Item in ExtraMenuItems) { Button Btn = await Item.GenerateUIButton().ConfigureAwait(true); Btn.Click += async(s, e) => { Flyout?.Hide(); if (((Button)s)?.Tag is ContextMenuItem MenuItem) { await MenuItem.Invoke().ConfigureAwait(true); } }; Panel.Children.Add(Btn); } AppBarElementContainer Container = new AppBarElementContainer { HorizontalContentAlignment = HorizontalAlignment.Stretch, Content = Panel }; List <int> SeparatorGroup = Flyout.SecondaryCommands.Select((Item, Index) => (Index, Item)).Where((Group) => Group.Item is AppBarSeparator).Select((Group) => Group.Index).ToList(); if (SeparatorGroup.Count > 0) { Flyout.SecondaryCommands.Insert(SeparatorGroup[0] + 1, new AppBarSeparator()); Flyout.SecondaryCommands.Insert(SeparatorGroup[0] + 1, Container); } else { Flyout.SecondaryCommands.Insert(0, new AppBarSeparator()); Flyout.SecondaryCommands.Insert(0, Container); } } } else { foreach (AppBarElementContainer ExistContainer in Flyout.SecondaryCommands.OfType <AppBarElementContainer>()) { Flyout.SecondaryCommands.Remove(ExistContainer); } List <int> SeparatorGroup = Flyout.SecondaryCommands.Select((Item, Index) => (Index, Item)).Where((Group) => Group.Item is AppBarSeparator).Select((Group) => Group.Index).ToList(); if (SeparatorGroup.Count == 1) { if (SeparatorGroup[0] == 0) { Flyout.SecondaryCommands.RemoveAt(0); } } else { for (int i = 0; i < SeparatorGroup.Count - 1; i++) { if (Math.Abs(SeparatorGroup[i] - SeparatorGroup[i + 1]) == 1) { Flyout.SecondaryCommands.RemoveAt(SeparatorGroup[i]); } } } } } } catch (Exception ex) { LogTracer.Log(ex); } finally { if (ShowAt != null) { try { FlyoutShowOptions Option = new FlyoutShowOptions { Position = ShowAt, Placement = FlyoutPlacementMode.RightEdgeAlignedTop }; Flyout?.ShowAt(ListControl, Option); } catch (Exception ex) { LogTracer.Log(ex, "An exception was threw when trying show flyout"); } } else { ListControl.ContextFlyout = Flyout; } } }