|
| 1 | +--- |
| 2 | +title: "Breaking change - Applications referencing both WPF and WinForms must disambiguate MenuItem and ContextMenu types" |
| 3 | +description: "Learn about the breaking change in .NET 10 Preview 1 where applications referencing both WPF and WinForms must disambiguate MenuItem and ContextMenu types." |
| 4 | +ms.date: 3/11/2025 |
| 5 | +ai-usage: ai-assisted |
| 6 | +ms.custom: https://github.com/dotnet/docs/issues/44738 |
| 7 | +--- |
| 8 | + |
| 9 | +# Applications referencing both WPF and WinForms must disambiguate MenuItem and ContextMenu types |
| 10 | + |
| 11 | +This document describes a breaking change introduced in .NET 10 Preview 1. Applications that reference both Windows Presentation Foundation (WPF) and Windows Forms (WinForms) must disambiguate certain types, such as `MenuItem` and `ContextMenu`, to avoid compile-time errors. |
| 12 | + |
| 13 | +## Version introduced |
| 14 | + |
| 15 | +.NET 10 Preview 1 |
| 16 | + |
| 17 | +## Previous behavior |
| 18 | + |
| 19 | +Previously, the types `ContextMenu`, `DataGrid`, `DataGridCell`, `Menu`, `MenuItem`, `ToolBar`, and `StatusBar` would resolve to the <xref:System.Windows.Controls> namespace because they did not exist in the <xref:System.Windows.Forms> namespace in .NET Core 3.1 through .NET 9.0. |
| 20 | + |
| 21 | +```xml |
| 22 | +<ImplicitUsings>enable</ImplicitUsings> |
| 23 | +<UseWindowsForms>true</UseWindowsForms> |
| 24 | +<UseWPF>true</UseWPF> |
| 25 | +``` |
| 26 | + |
| 27 | +## New behavior |
| 28 | + |
| 29 | +The affected types in the <xref:System.Windows.Forms> namespace cause a compile-time error when there is an ambiguous reference between <xref:System.Windows.Controls> and <xref:System.Windows.Forms>. |
| 30 | + |
| 31 | +```output |
| 32 | +CS0104 'ContextMenu' is an ambiguous reference between 'System.Windows.Controls.ContextMenu' and 'System.Windows.Forms.ContextMenu' |
| 33 | +``` |
| 34 | + |
| 35 | +## Type of breaking change |
| 36 | + |
| 37 | +This is a [source incompatible](../../categories.md#source-compatibility) change. |
| 38 | + |
| 39 | +## Reason for change |
| 40 | + |
| 41 | +The change facilitates migration from .NET Framework when third-party libraries cannot be updated. A .NET 10 application can continue to reference .NET Framework dependencies and handle errors at runtime. |
| 42 | + |
| 43 | +## Recommended action |
| 44 | + |
| 45 | +Use aliases to resolve conflicting namespaces. For example: |
| 46 | + |
| 47 | +```csharp |
| 48 | +using ContextMenu = System.Windows.Controls.ContextMenu; |
| 49 | +``` |
| 50 | + |
| 51 | +Refer to the [alias name conflicts documentation](/dotnet/csharp/language-reference/compiler-messages/using-directive-errors#alias-name-conflicts) for more details. |
| 52 | + |
| 53 | +## Affected APIs |
| 54 | + |
| 55 | +- <xref:System.Windows.Forms.ContextMenu?displayProperty=fullName> |
| 56 | +- <xref:System.Windows.Forms.DataGrid?displayProperty=fullName> |
| 57 | +- <xref:System.Windows.Forms.DataGridCell?displayProperty=fullName> |
| 58 | +- <xref:System.Windows.Forms.Menu?displayProperty=fullName> |
| 59 | +- <xref:System.Windows.Forms.MenuItem?displayProperty=fullName> |
| 60 | +- <xref:System.Windows.Forms.ToolBar?displayProperty=fullName> |
| 61 | +- <xref:System.Windows.Forms.StatusBar?displayProperty=fullName> |
| 62 | +- <xref:System.Windows.Controls.ContextMenu?displayProperty=fullName> |
| 63 | +- <xref:System.Windows.Controls.DataGrid?displayProperty=fullName> |
| 64 | +- <xref:System.Windows.Controls.DataGridCell?displayProperty=fullName> |
| 65 | +- <xref:System.Windows.Controls.Menu?displayProperty=fullName> |
| 66 | +- <xref:System.Windows.Controls.MenuItem?displayProperty=fullName> |
| 67 | +- <xref:System.Windows.Controls.ToolBar?displayProperty=fullName> |
| 68 | +- `System.Windows.Controls.StatusBar` |
0 commit comments