当前位置: 首页>>代码示例>>C#>>正文


C# IMenuItem.Execute方法代码示例

本文整理汇总了C#中IMenuItem.Execute方法的典型用法代码示例。如果您正苦于以下问题:C# IMenuItem.Execute方法的具体用法?C# IMenuItem.Execute怎么用?C# IMenuItem.Execute使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在IMenuItem的用法示例。


在下文中一共展示了IMenuItem.Execute方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: Create

        MenuItem Create(IMenuItem item, IMenuItemMetadata metadata, IMenuItemContext 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);
            string iconName = metadata.Icon;

            var mi2 = item as IMenuItem2;
            if (mi2 != null) {
                header = mi2.GetHeader(ctx) ?? header;
                inputGestureText = mi2.GetInputGestureText(ctx) ?? inputGestureText;
                iconName = mi2.GetIcon(ctx) ?? iconName;
                menuItem.IsChecked = mi2.IsChecked(ctx);
            }

            menuItem.Header = header;
            menuItem.InputGestureText = inputGestureText;

            var cmdHolder = item as ICommandHolder;
            bool lastIsEnabledCallValue = false;
            if (!string.IsNullOrEmpty(iconName)) {
                if (cmdHolder == null)
                    lastIsEnabledCallValue = item.IsEnabled(ctx);
                else {
                    var routedCommand = cmdHolder.Command as RoutedCommand;
                    lastIsEnabledCallValue = commandTarget == null || routedCommand == null || routedCommand.CanExecute(ctx, commandTarget);
                }
                imageManager.Add16x16Image(menuItem, item.GetType().Assembly, iconName, isCtxMenu, 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 => item.Execute(ctx), a => {
                bool b = item.IsEnabled(ctx);
                if (lastIsEnabledCallValue != b && !string.IsNullOrEmpty(iconName))
                    imageManager.Add16x16Image(menuItem, item.GetType().Assembly, iconName, isCtxMenu, lastIsEnabledCallValue = b);
                return b;
            });

            return menuItem;
        }
开发者ID:lovebanyi,项目名称:dnSpy,代码行数:60,代码来源:MenuManager.cs

示例2: Create

		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;
		}
开发者ID:manojdjoshi,项目名称:dnSpy,代码行数:64,代码来源:MenuService.cs


注:本文中的IMenuItem.Execute方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。