引用 Windows Presentation Foundation(WPF)和 Windows 窗体(WinForms)的应用程序现在必须消除某些类型(例如 MenuItem 和 ContextMenu)的歧义,以避免编译时错误。
已引入的版本
.NET 10
以前的行为
以前,类型ContextMenu、DataGrid、DataGridCell、Menu、MenuItem和ToolBarStatusBar将解析为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.Controls和System.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
- System.Windows.Forms.ContextMenu
- System.Windows.Forms.DataGrid
- System.Windows.Forms.DataGridCell
- System.Windows.Forms.Menu
- System.Windows.Forms.MenuItem
- System.Windows.Forms.ToolBar
- System.Windows.Forms.StatusBar
- System.Windows.Controls.ContextMenu
- System.Windows.Controls.DataGrid
- System.Windows.Controls.DataGridCell
- System.Windows.Controls.Menu
- System.Windows.Controls.MenuItem
- System.Windows.Controls.ToolBar
System.Windows.Controls.StatusBar