MenuItem Create(IMenuItem item, IMenuItemMetadata metadata, MenuItemContext ctx, IInputElement commandTarget, MenuItem menuItem, bool isCtxMenu) { if (menuItem == null) { menuItem = new MenuItem(); } menuItem.CommandTarget = commandTarget; string header = ResourceHelper.GetString(item, metadata.Header); string inputGestureText = ResourceHelper.GetString(item, metadata.InputGestureText); ImageReference?iconImgRef = ImageReferenceHelper.GetImageReference(item, metadata.Icon); header = item.GetHeader(ctx) ?? header; inputGestureText = item.GetInputGestureText(ctx) ?? inputGestureText; iconImgRef = item.GetIcon(ctx) ?? iconImgRef; menuItem.IsChecked = item.IsChecked(ctx); menuItem.Header = header; menuItem.InputGestureText = inputGestureText; var cmdHolder = item as ICommandHolder; bool lastIsEnabledCallValue = false; if (iconImgRef != null) { if (cmdHolder == null) { lastIsEnabledCallValue = item.IsEnabled(ctx); } else { var routedCommand = cmdHolder.Command as RoutedCommand; lastIsEnabledCallValue = commandTarget == null || routedCommand == null || routedCommand.CanExecute(ctx, commandTarget); } Add16x16Image(menuItem, iconImgRef.Value, lastIsEnabledCallValue); } if (metadata.Guid != null) { var itemGuid = Guid.Parse(metadata.Guid); if (guidToGroups.ContainsKey(itemGuid)) { menuItem.Items.Add(new MenuItem()); menuItem.SubmenuOpened += (s, e) => { if (e.Source == menuItem) { InitializeSubMenu(menuItem, ctx, itemGuid, commandTarget, isCtxMenu); } }; menuItem.SubmenuClosed += (s, e) => { if (e.Source == menuItem) { menuItem.Items.Clear(); menuItem.Items.Add(new MenuItem()); } }; } } ctx.OnDisposed += (_, __) => { // Buggy automation peers could hold a reference to us, so clear the captured variables (we can't clear the captured 'this') menuItem = null; ctx = null; commandTarget = null; item = null; iconImgRef = null; }; menuItem.Command = cmdHolder != null ? cmdHolder.Command : new RelayCommand(a => { Debug.Assert(!ctx.IsDisposed); if (ctx?.IsDisposed == false) { item.Execute(ctx); ctx?.Dispose(); } }, a => { if (ctx?.IsDisposed != false) { return(false); } bool b = item.IsEnabled(ctx); if (lastIsEnabledCallValue != b && iconImgRef != null) { Add16x16Image(menuItem, iconImgRef.Value, lastIsEnabledCallValue = b); } return(b); }); return(menuItem); }
MenuItem Create(IMenuItem item, IMenuItemMetadata metadata, MenuItemContext ctx, IInputElement commandTarget, MenuItem menuItem, bool isCtxMenu) { if (menuItem == null) menuItem = new MenuItem(); menuItem.CommandTarget = commandTarget; string header = ResourceHelper.GetString(item, metadata.Header); string inputGestureText = ResourceHelper.GetString(item, metadata.InputGestureText); ImageReference? iconImgRef = ImageReferenceHelper.GetImageReference(item, metadata.Icon); header = item.GetHeader(ctx) ?? header; inputGestureText = item.GetInputGestureText(ctx) ?? inputGestureText; iconImgRef = item.GetIcon(ctx) ?? iconImgRef; menuItem.IsChecked = item.IsChecked(ctx); menuItem.Header = header; menuItem.InputGestureText = inputGestureText; var cmdHolder = item as ICommandHolder; bool lastIsEnabledCallValue = false; if (iconImgRef != null) { if (cmdHolder == null) lastIsEnabledCallValue = item.IsEnabled(ctx); else { var routedCommand = cmdHolder.Command as RoutedCommand; lastIsEnabledCallValue = commandTarget == null || routedCommand == null || routedCommand.CanExecute(ctx, commandTarget); } Add16x16Image(menuItem, iconImgRef.Value, lastIsEnabledCallValue); } if (metadata.Guid != null) { var itemGuid = Guid.Parse(metadata.Guid); List<MenuItemGroupMD> list; if (guidToGroups.TryGetValue(itemGuid, out list)) { menuItem.Items.Add(new MenuItem()); menuItem.SubmenuOpened += (s, e) => { if (e.Source == menuItem) InitializeSubMenu(menuItem, ctx, itemGuid, commandTarget, isCtxMenu); }; menuItem.SubmenuClosed += (s, e) => { if (e.Source == menuItem) { menuItem.Items.Clear(); menuItem.Items.Add(new MenuItem()); } }; } } menuItem.Command = cmdHolder != null ? cmdHolder.Command : new RelayCommand(a => { Debug.Assert(!ctx.IsDisposed); if (!ctx.IsDisposed) { item.Execute(ctx); ctx.Dispose(); } }, a => { if (ctx.IsDisposed) return false; bool b = item.IsEnabled(ctx); if (lastIsEnabledCallValue != b && iconImgRef != null) Add16x16Image(menuItem, iconImgRef.Value, lastIsEnabledCallValue = b); return b; }); return menuItem; }