Skip to content

Investigate ENC0020 when replacing named XAML elements during Hot Reload #38993

Closed

Description

@jonathanpeppers

Description

Replacing a XAML element with x:Name with a different named element during a running .NET MAUI Hot Reload session has been reported to produce ENC0020: Renaming field 'count' requires restarting the application. The user removed an element and added another; they did not explicitly rename a field in C#. The diagnostic appears against MainPage.xaml.cs. This needs a minimal reproduction to establish the affected MAUI/IDE/runtime versions, platform(s), and whether the failure is in XAML Hot Reload, C# Hot Reload of generated code, or their interaction.

MAUI's code-behind generator emits fields for named XAML elements. Roslyn rejects field renames and deletions, but handles auto-property renames as deleting the old property/accessors and adding new ones when the runtime supports adding instance fields. Deleting an auto-property leaves its backing field in the running type. Both CoreCLR and Mono advertise AddInstanceFieldToExistingType, but this does not establish that replacing named MAUI XAML elements works end-to-end; the runtime instance-field update test is currently skipped on Mono.

Investigate whether opt-in generated auto-properties (or another stable access mechanism) permit this edit without a restart. Account for existing field-based code-behind API and x:FieldModifier, runtime InitializeComponent, XAML source generation, and XamlC's SetFieldVisitor, which currently writes fields. Verify existing page instances and namescopes after replacement, not just absence of the diagnostic. A global field-to-property switch would be a compatibility change; do not assume it is safe. Related but distinct stale-reference behavior: #5731.

Steps to Reproduce

Reported sequence (not yet independently reproduced):

  1. Run a MAUI page containing a named element (for example <Label x:Name="count" />) with Hot Reload enabled.
  2. Remove that element and add a different element with a different x:Name while the app is running.
  3. Apply the edit and inspect Hot Reload output.

Expected: the edit is applied without a misleading field-rename diagnostic, if supported by the runtime; otherwise provide an accurate, actionable reason for requiring restart.

Actual reported: ENC0020 claims a generated field was renamed and requires restarting the app.

Link to public reproduction project repository

dotnet new maui and then rename the x:Name="count".

Version with bug

.NET 11 RC 2 (unreleased builds at the time)

Is this a regression from previous behavior?

No

Last version that worked well

None

Affected platforms

All

Affected platform versions

N/A

Did you find any workaround?

Restart the application after replacing named XAML elements. Avoid removing/adding x:Name during a Hot Reload session when a restart is unacceptable.

Relevant log output

2026-09-29 11:51:00.739 [error] MainPage.xaml.cs(16,2): error ENC0020: Renaming field 'count' requires restarting the application.

Activity

  1. added
    area-xaml
    xsg
    on Sep 29, 2026
  2. added
    s/triaged
    on Sep 29, 2026
  3. jonathanpeppers commented on Sep 29, 2026

    @jonathanpeppers
    MemberAuthor

    Another version of the "rude edit" is by removing an x:Name:

    2026-09-29 15:55:54.896 [error]  ❌ D:\src\hellowmaui\obj\Debug\net11.0-android\Microsoft.Maui.Controls.SourceGen\Microsoft.Maui.Controls.SourceGen.XamlGenerator\MainPage.xaml.sg.cs (line 16): error ENC0033: Deleting field 'TestLabel' requires restarting the application.
    
  4. StephaneDelcroix commented on Oct 5, 2026

    @StephaneDelcroix
    Contributor

    we could modify the code behind generator so it doesn't remove fields. that would keep it coherent, and keep x:Name backed up by fields, not props

  5. added a commit that references this issue on Oct 20, 2026
    e38973a
  6. github-actions commented on Oct 20, 2026

    @github-actions
    Contributor

    Closed by #39281 (merged to net11.0). GitHub only auto-closes for PRs merged to the default branch; this issue was fixed by a PR merged to a non-default branch.

  7. locked and limited conversation to collaborators on Nov 20, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

area-xamls/triagedxsg

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions