同時參考 Windows Presentation Foundation(WPF)和 Windows Forms(WinForms)的應用程式現在必須釐清特定類型,例如MenuItem和ContextMenu,以避免編譯階段錯誤。
引進的版本
.NET 10
先前的行為
先前,ContextMenu、DataGrid、DataGridCell、Menu、MenuItem、ToolBar和 StatusBar 類型會解析為 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