Skip to content

[Bug]: XAML Previewer fails on page-relative ResourceDictionary Source ("../") and strips keys from page-merged dictionaries #343

Open

Description

Problem

When a Page or UserControl merges a <ResourceDictionary Source="..."/> inside <Page.Resources>, two problems prevent the dictionary and its resources from rendering in the Visual Studio XAML Previewer (zateutsch-xaml-previewer, v0.3.15):

  1. Page-relative paths (../) fail the preview render: <ResourceDictionary Source="../Themes/ScenarioResources.xaml"/> fails at XamlReader.Load because App.RewriteAppRelativeDictionarySources only runs on App.xaml, and NormalizeAppResourceSource does not resolve ../ paths relative to DocumentPath.
  2. Keys from page-merged dictionaries are stripped by XamlCleaner Rule 6: Even when the path is written as Source="/Themes/ScenarioResources.xaml" or ms-appx:///Themes/ScenarioResources.xaml so XamlReader.Load succeeds, XamlCleaner only collects docKeys from inline x:Key attributes in the current file and checks App.ResourceKeyResolver (Application.Current.Resources). Keys defined in a dictionary merged under <Page.Resources> are treated as unresolved and stripped before rendering.

Smallest repro

In a page under Pages\ResourcesPage.xaml with a dictionary at Themes\ScenarioResources.xaml:

<!-- Themes/ScenarioResources.xaml -->
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <ResourceDictionary.ThemeDictionaries>
        <ResourceDictionary x:Key="Light"><SolidColorBrush x:Key="ScenarioThemeBrush" Color="DarkSlateBlue"/></ResourceDictionary>
        <ResourceDictionary x:Key="Dark"><SolidColorBrush x:Key="ScenarioThemeBrush" Color="LightSkyBlue"/></ResourceDictionary>
    </ResourceDictionary.ThemeDictionaries>
    <SolidColorBrush x:Key="MergedOnlyBrush" Color="SteelBlue"/>
</ResourceDictionary>
<!-- Pages/ResourcesPage.xaml -->
<Page x:Class="BugBash.App.Pages.ResourcesPage"
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <Page.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="../Themes/ScenarioResources.xaml"/>
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
    </Page.Resources>
    <StackPanel>
        <TextBlock Text="Merged-only steel blue" Foreground="{StaticResource MergedOnlyBrush}"/>
        <TextBlock Text="Themed text" Foreground="{ThemeResource ScenarioThemeBrush}"/>
    </StackPanel>
</Page>
  • Actual result:
    • With Source="../Themes/ScenarioResources.xaml", the preview fails to render with:
      XAML error: Line 4, col 72 — Cannot locate resource from 'ms-resource:///Themes/ScenarioResources.xaml'.
    • With Source="/Themes/ScenarioResources.xaml", the page renders, but preview.log reports:
      [Surface] Unresolved resource key(s) (defined in code or outside App.xaml?): MergedOnlyBrush, ScenarioThemeBrush
      and both Foreground attributes are stripped by XamlCleaner Rule 6 so the text renders in default black/white.
  • Expected result:
    • ../Themes/ScenarioResources.xaml resolves relative to DocumentPath (Pages/ResourcesPage.xaml -> ms-appx:///Themes/ScenarioResources.xaml).
    • Keys defined in dictionaries merged under <Page.Resources> (including inside ThemeDictionaries) are recognized as resolvable by XamlCleaner Rule 6 and render with their expected colors.

Root cause & smallest fix

  • surface/Surface/App.cs (NormalizeAppResourceSource / RewriteAppRelativeDictionarySources) & surface/Surface/XamlCleaner.cs: Normalize <ResourceDictionary Source="..."/> URIs in page/control XAML against DocumentPath (resolving relative ../ segments and leading / to ms-appx:///...).
  • surface/Surface/XamlCleaner.cs (Clean, around line 234): When building docKeys, include x:Key and x:Name entries from local project <ResourceDictionary Source="..."/> files merged inside the document's resources.

Version

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

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions