[SharpDevelop]菜单状态更新
方式一
在Idle方法中更新

1 void OnApplicationIdle(object sender, EventArgs e)
2 {
3 // Use the Idle event to update the status of menu and toolbar.
4 // Depending on your application and the number of menu items with complex conditions,
5 // you might want to update the status less frequently.
6 UpdateMenuItemStatus();
7 }
8
9 /// <summary>Update Enabled/Visible state of items in the main menu based on conditions</summary>
10 void UpdateMenuItemStatus()
11 {
12 foreach (ToolStripItem item in menu.Items) {
13 if (item is IStatusUpdate)
14 (item as IStatusUpdate).UpdateStatus();
15 }
16 }
方式二
sealed class DefaultWorkbench : Form, IWorkbench
通过一个Timer来更新
1 toolbarUpdateTimer = new System.Windows.Forms.Timer();
2 toolbarUpdateTimer.Tick += new EventHandler(UpdateMenu);

1 void UpdateMenu(object sender, EventArgs e)
2 {
3 UpdateMenus();
4 UpdateToolbars();
5 }
6
7 void UpdateMenus()
8 {
9 // update menu
10 foreach (object o in TopMenu.Items) {
11 if (o is IStatusUpdate) {
12 ((IStatusUpdate)o).UpdateStatus();
13 }
14 }
15 }
16
17 void UpdateToolbars()
18 {
19 if (ToolBars != null) {
20 foreach (ToolStrip toolStrip in ToolBars) {
21 ToolbarService.UpdateToolbar(toolStrip);
22 }
23 }
24 }
方式三
sealed partial class WpfWorkbench : FullScreenEnabledWindow, IWorkbench, System.Windows.Forms.IWin32Window
1 requerySuggestedEventHandler = new EventHandler(CommandManager_RequerySuggested);
2 CommandManager.RequerySuggested += requerySuggestedEventHandler;

1 EventHandler requerySuggestedEventHandler;
2
3 void CommandManager_RequerySuggested(object sender, EventArgs e)
4 {
5 UpdateMenu();
6 }
7 void UpdateMenu()
8 {
9 MenuService.UpdateStatus(mainMenu.ItemsSource);
10 foreach (ToolBar tb in toolBars)
11 {
12 ToolBarService.UpdateStatus(tb.ItemsSource);
13 }
14 }
作者:太一吾鱼水
文章未经说明均属原创,学习笔记可能有大段的引用,一般会注明参考文献。
欢迎大家留言交流,转载请注明出处。
分类:
插件式体系架构
, 编程开发集合 / C#开发
标签:
SharpDevelop
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 单元测试从入门到精通
· 上周热点回顾(3.3-3.9)
· Vue3状态管理终极指南:Pinia保姆级教程