引用 WPF 和 WinForms 的应用程序必须消除 MenuItem 和 ContextMenu 类型的歧义

引用 Windows Presentation Foundation(WPF)和 Windows 窗体(WinForms)的应用程序现在必须消除某些类型(例如 MenuItemContextMenu)的歧义,以避免编译时错误。

已引入的版本

.NET 10

以前的行为

以前,类型ContextMenuDataGridDataGridCellMenuMenuItemToolBarStatusBar将解析为System.Windows.Controls命名空间,因为它们在 .NET Core 3.1 到 .NET 9.0 的System.Windows.Forms命名空间中不存在。

<ImplicitUsings>enable</ImplicitUsings>
<UseWindowsForms>true</UseWindowsForms>
<UseWPF>true</UseWPF>

新行为

System.Windows.Forms命名空间中,如果System.Windows.ControlsSystem.Windows.Forms之间存在不明确的引用,受影响的类型会导致编译时错误。

CS0104 'ContextMenu' is an ambiguous reference between 'System.Windows.Controls.ContextMenu' and 'System.Windows.Forms.ContextMenu'

破坏性变更的类型

这是 源不兼容 的更改。

更改原因

当无法更新第三方库时,此更改有助于从 .NET Framework 迁移。 .NET 10 应用程序可以继续引用 .NET Framework 依赖项并在运行时处理错误。

使用别名来解决命名空间冲突。 例如:

using ContextMenu = System.Windows.Controls.ContextMenu;

请参阅别名冲突文档以获取更多详细信息。

受影响的 API