Skip to content

Named resource entries of top-level XAML element aren't generated #4032

Open

Description

@davidjohnoliver

How to reproduce it (as minimally and precisely as possible)

	<Page.Resources>
		<ResourceDictionary>
			<SolidColorBrush x:Name="MyBrush" Color="Blue"/>
		</ResourceDictionary>
	</Page.Resources>
		public MainPage()
		{
			this.InitializeComponent();

			MyBrush.ToString();
		}

Current behavior

1> C:\src\djo\tests\UnoTestbed15\UnoTestbed15\UnoTestbed15.Shared\MainPage.xaml.cs(33,4,33,11): error CS0103: The name 'MyBrush' does not exist in the current context
1> C:\src\djo\tests\UnoTestbed15\UnoTestbed15\UnoTestbed15.Droid\obj\Debug\90\g\XamlCodeGenerator\MainPage_3d81ab9d64dc9fbde82f418d1c389ae6.g.cs(313,5,313,12): error CS0103: The name 'MyBrush' does not exist in the current context

Expected behavior

Compiles as per UWP, resource entry can be referred to by name in code-behind

Workaround

Put the resource in a non-top-level element, eg:

<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
		<Grid.Resources>
			<ResourceDictionary>
				<SolidColorBrush x:Name="MyBrush"
								 Color="Blue" />
			</ResourceDictionary>
		</Grid.Resources>
		<TextBlock Text="Hello, world!" Margin="20" FontSize="30" />
    </Grid>

Environment

Nuget Package:

  • Uno.UI / Uno.UI.WebAssembly / Uno.UI.Skia
  • Uno.WinUI / Uno.WinUI.WebAssembly / Uno.WinUI.Skia
  • Uno.SourceGenerationTasks
  • Uno.UI.RemoteControl / Uno.WinUI.RemoteControl
  • Other:

Nuget Package Version(s): 3.1.0-dev.288

Affected platform(s):

  • iOS
  • Android
  • WebAssembly
  • WebAssembly renderers for Xamarin.Forms
  • macOS
  • Skia
    • WPF
    • GTK (Linux)
    • Tizen
  • Windows
  • Build tasks
  • Solution Templates

IDE:

  • Visual Studio 2017 (version: )
  • Visual Studio 2019 (version: )
  • Visual Studio for Mac (version: )
  • Rider Windows (version: )
  • Rider macOS (version: )
  • Visual Studio Code (version: )

Relevant plugins:

  • Resharper (version: )

Anything else we need to know?

Activity

  1. added
    kind/bug
    triage/untriaged
    on Sep 11, 2020
  2. removed
    triage/untriaged
    on Sep 11, 2020
  3. added
    project/core-tools 🛠️
    on Sep 21, 2020
  4. added
    difficulty/tbd
    on Feb 15, 2021
  5. added
    difficulty/medium 🤔
    and removed
    difficulty/tbd
    on Jun 1, 2021
  6. MartinZikmund commented on Jun 1, 2021

    @MartinZikmund
    Member

    This is quite useful for TeachingTip support, as x:Name approach is used often with it

  7. Youssef1313 commented on Sep 16, 2021

    @Youssef1313
    Member

    Registering a name for the current scope is done under a condition:

    							var isMemberInsideResourceDictionary = IsMemberInsideResourceDictionary(objectDefinition);
    
    							if (
    								member.Member.Name == "Name"
    								&& !isMemberInsideResourceDictionary.isInside
    							)
    							{
    								writer.AppendLineInvariant($@"nameScope.RegisterName(""{member.Value}"", {closureName});");
    							}

    Then, RegisterBackingField call (which I assume is responsible for generating the field) is gated under the same conditions.

    I don't have the complete background to know why the isMemberInsideResourceDictionary.isInside check is there in the first place. It doesn't look like a simple removal of the condition, I think there must be a reason it originally existed.

    Any ideas?

  8. jeromelaban commented on Sep 16, 2021

    @jeromelaban
    Member

    @Youssef1313 I don't recall the details for this, but looking at the associated commits will give you some context.

  9. Youssef1313 commented on Sep 16, 2021

    @Youssef1313
    Member

    @jeromelaban It exists from the very beginning:

    image

    There are compilation errors when I removed the condition in #7108, I'll look into that and adjust, and see if anything else breaks.

  10. MartinZikmund commented on Oct 8, 2021

    @MartinZikmund
    Member

    I presume this scenario is the same?

    <Page.Resources>
        <DataTemplate x:Name="NormalTemplate">
            <TextBlock Text="{Binding}" Foreground="Green"></TextBlock>
        </DataTemplate>
    </Page.Resources>
    
    <Grid>
        <ListView ItemTemplate="{StaticResource NormalTemplate}" />
    </Grid>
  11. Youssef1313 commented on Oct 8, 2021

    @Youssef1313
    Member

    @MartinZikmund Yeah looks like it's the same. Is this a scenario blocking any control you're working on? Might worth re-testing with #7108 or after it's merged.

  12. changed the title [-]Named Resource entries of top-level Xaml element aren't generated[/-] [+]Named resource entries of top-level XAML element aren't generated[/+] on Jun 14, 2023
  13. lindexi commented on Sep 21, 2023

    @lindexi
    Contributor

    Or

            <ControlTemplate x:Key="Template.Loading" TargetType="ContentControl">
                <Grid x:Name="RootGrid" />
            </ControlTemplate>
    
  14. github-actions commented on Jul 26, 2025

    @github-actions
    Contributor

    Hey there — just a heads-up that this issue has been quiet for over a year. If there’s no update or comment in the next 10 days, it’ll be closed automatically. Feel free to remove the stale label or drop a note to keep it open.

  15. added
    stale
    on Jul 26, 2025
  16. MartinZikmund commented on Jul 27, 2025

    @MartinZikmund
    Member

    Still relevant

  17. removed
    stale
    on Jul 27, 2025
  18. MartinZikmund commented on Apr 7, 2026

    @MartinZikmund
    Member

    Repro generator

    Runtime test(s) added in branch dev/mazi/repro/4032:

    • src/Uno.UI.RuntimeTests/Tests/BindingTests/PageResourceNameTest.xaml + PageResourceNameTest.xaml.cs
    • src/Uno.UI.RuntimeTests/Tests/BindingTests/Given_PageResourceName_Issue4032.cs → When_Named_Resource_In_Page_Resources_ResourceDictionary_Is_Accessible

    ⚠️ The branch FAILS TO BUILD — this IS the reproduction of the bug:

    The AT command has been deprecated. Please use schtasks.exe instead.

    Invalid command.

    The AT command schedules commands and programs to run on a computer at
    a specified time and date. The Schedule service must be running to use
    the AT command.

    AT [\computername] [ [id] [/DELETE] | /DELETE [/YES]]
    AT [\computername] time [/INTERACTIVE]
    [ /EVERY:date[,...] | /NEXT:date[,...]] "command"

    \computername Specifies a remote computer. Commands are scheduled on the
    local computer if this parameter is omitted.
    id Is an identification number assigned to a scheduled
    command.
    /delete Cancels a scheduled command. If id is omitted, all the
    scheduled commands on the computer are canceled.
    /yes Used with cancel all jobs command when no further
    confirmation is desired.
    time Specifies the time when command is to run.
    /interactive Allows the job to interact with the desktop of the user
    who is logged on at the time the job runs.
    /every:date[,...] Runs the command on each specified day(s) of the week or
    month. If date is omitted, the current day of the month
    is assumed.
    /next:date[,...] Runs the specified command on the next occurrence of the
    day (for example, next Thursday). If date is omitted, the
    current day of the month is assumed.
    "command" Is the Windows NT command, or batch program to be run.

    Root cause: The XAML source generator generates an assignment () in the generated InitializeComponent() code but never generates the field declaration (). The error is in the generator's own output, not in user code. The bug is in XamlFileGenerator.cs — named resources in Page.Resources > ResourceDictionary (top-level element) don't get field declarations generated.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    difficulty/medium 🤔has-repro-branchkind/bugproject/core-tools 🛠️

    Type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions