object Create(ToolBarButtonMD md, IToolBarItemContext ctx, IInputElement commandTarget) { var item = (IToolBarButton)md.ToolBarItem; var md2 = (IToolBarButtonMetadata)md.Metadata; var cmdHolder = item as ICommandHolder; var cmd = cmdHolder != null ? cmdHolder.Command : new RelayCommand(a => item.Execute(ctx), a => item.IsEnabled(ctx)); string header = ResourceHelper.GetString(item, md2.Header); string icon = md2.Icon; string toolTip = ResourceHelper.GetString(item, md2.ToolTip); header = item.GetHeader(ctx) ?? header; toolTip = item.GetToolTip(ctx) ?? toolTip; var imgRef = item.GetIcon(ctx) ?? ImageReferenceHelper.GetImageReference(item, icon) ?? default(ImageReference); var toggleButtonCmd = item as IToolBarToggleButton; Debug.Assert(md2.IsToggleButton == (toggleButtonCmd != null), "Implement IToolBarToggleButton if IsToggleButton is true"); if (toggleButtonCmd != null) { return(CreateToggleButton(toggleButtonCmd.GetBinding(ctx), cmd, commandTarget, header, toolTip, imgRef)); } return(new ToolBarButtonVM(cmd, commandTarget, header, toolTip, imgRef)); }
static AppSettingsPageVM TryCreate(object obj, IAppSettingsPageContainerMetadata md, PageContext context) { Guid?guid = md.Guid == null ? null : TryParseGuid(md.Guid); Debug.Assert(guid != null, "Invalid GUID"); if (guid == null) { return(null); } Guid?parentGuid = md.ParentGuid == null ? rootGuid : TryParseGuid(md.ParentGuid); Debug.Assert(parentGuid != null, "Invalid Parent GUID"); if (parentGuid == null) { return(null); } if (string.IsNullOrEmpty(md.Title)) { return(null); } var title = ResourceHelper.GetString(obj, md.Title); var icon = ImageReferenceHelper.GetImageReference(obj, md.Icon) ?? ImageReference.None; return(new AppSettingsPageVM(new AppSettingsPageContainer(title, md.Order, guid.Value, parentGuid.Value, icon), context)); }
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); }