怎么做淘宝企业网站,园林景观 网站建设,专业团队为您服务,教务管理系统入口点击上方蓝字关注“汪宇杰博客”导语我作为社区里的“拖控件之王”#xff0c;拖控件贼心不死#xff0c;有时候会维护一些老项目#xff0c;其中包括一个2004年的WinForms 软件。9月份的时候我曾经将它迁移到了 .NET Core 3.0#xff0c;因为代码实现完全没动#xff0c;… 点击上方蓝字关注“汪宇杰博客”导语我作为社区里的“拖控件之王”拖控件贼心不死有时候会维护一些老项目其中包括一个2004年的WinForms 软件。9月份的时候我曾经将它迁移到了 .NET Core 3.0因为代码实现完全没动所以基本没领福报。然而 .NET Core 3.1 有一些升级上的变化需要注意。背景这个 WinForms 工程是一个开源的屏幕尺工具用于测量屏幕上元素的尺寸由 Jeff Key 在 15 年前开发至今好用。原作者不维护后我将源代码接盘维护继续开源在 GitHub上。https://github.com/EdiWang/Ruler虽然我自己也用UWP实现过类似的屏幕尺公众号里也发文章介绍过《》可惜后来UWP这个技术……嗯不提了话说这把屏幕尺支持横竖两种排版、固顶显示、透明度调整等常用功能大家有需要的话欢迎下载使用。升级到.NET Core 3.1这个工程最早是 .NET Framework 2.0 的升级到 .NET Core 3.0 的步骤我不多介绍了官网或社区都有详细的教程由于功能简单并不需要代码实现和逻辑上的修改。但是 .NET Core 3.1 移除了一些老控件因此这次升级需要修改代码。先来看看官方的说明Windows Forms Controls RemovalThe following Windows Forms controls have been removed from .NET Core 3.1:DataGridToolBarContextMenuMenuMainMenuMenuItemThese controls were replaced with more powerful controls in .NET Framework 2.0, back in 2005. They have not been available by default in the Visual Studio Designer Toolbox for many years. As a result, we decided to remove these controls and focus only on the new ones.官方也给出了这些被删库的老控件的替换方案参见 https://devblogs.microsoft.com/dotnet/announcing-net-core-3-1/在这个Ruler工程中我需要迁移的主要就是菜单控件。类型替换将 ContextMenu 替换为 ContextMenuStrip将 MenuItem 替换为 ToolStripMenuItem如private readonly ContextMenu _menu new ContextMenu();private MenuItem _verticalMenuItem;private MenuItem _toolTipMenuItem;替换为private readonly ContextMenuStrip _menu new ContextMenuStrip();private ToolStripMenuItem _verticalMenuItem;private ToolStripMenuItem _toolTipMenuItem;API 差异Shortcut 枚举类型被删库老代码private MenuItem AddMenuItem(string text, Shortcut shortcut Shortcut.None){ MenuItem mi new MenuItem(text); mi.Click MenuHandler; mi.Shortcut shortcut; _menu.MenuItems.Add(mi); return mi;}改为Keys shortcut Keys.None...mi.ShortcutKeys shortcut;类似的MenuItems属性被删库老代码_menu.MenuItems.Add(mi);改为_menu.Items.Add(mi);这段函数升级后的完整代码private ToolStripMenuItem AddMenuItem(string text, Keys shortcut Keys.None){ ToolStripMenuItem mi new ToolStripMenuItem(text); mi.Click MenuHandler; mi.ShortcutKeys shortcut; _menu.Items.Add(mi); return mi;}在旧版Menu控件里“-” 字符串代表分隔符这个现在也有专门的类型表示所以老代码AddMenuItem(-);修改为_menu.Items.Add(new ToolStripSeparator());现在就能成功编译运行了新版菜单控件效果如下至此升级.NET Core 3.1的工作全部完成