Repository navigation
Open
Labels
bugSomething isn't workingSomething isn't working
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):
- Page-relative paths (
../) fail the preview render:<ResourceDictionary Source="../Themes/ScenarioResources.xaml"/>fails atXamlReader.LoadbecauseApp.RewriteAppRelativeDictionarySourcesonly runs onApp.xaml, andNormalizeAppResourceSourcedoes not resolve../paths relative toDocumentPath. - Keys from page-merged dictionaries are stripped by
XamlCleanerRule 6: Even when the path is written asSource="/Themes/ScenarioResources.xaml"orms-appx:///Themes/ScenarioResources.xamlsoXamlReader.Loadsucceeds,XamlCleaneronly collectsdocKeysfrom inlinex:Keyattributes in the current file and checksApp.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, butpreview.logreports:
[Surface] Unresolved resource key(s) (defined in code or outside App.xaml?): MergedOnlyBrush, ScenarioThemeBrush
and bothForegroundattributes are stripped byXamlCleanerRule 6 so the text renders in default black/white.
- With
- Expected result:
../Themes/ScenarioResources.xamlresolves relative toDocumentPath(Pages/ResourcesPage.xaml->ms-appx:///Themes/ScenarioResources.xaml).- Keys defined in dictionaries merged under
<Page.Resources>(including insideThemeDictionaries) are recognized as resolvable byXamlCleanerRule 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 againstDocumentPath(resolving relative../segments and leading/toms-appx:///...).surface/Surface/XamlCleaner.cs(Clean, around line 234): When buildingdocKeys, includex:Keyandx:Nameentries from local project<ResourceDictionary Source="..."/>files merged inside the document's resources.
Version
- Branch / PR:
zateutsch-xaml-previewer(WinUI XAML Previewer for Visual Studio #323) - Commit:
00927d1c049b59160ec4dd26b38f09ab5caed9d9(WinUIXamlPreview.vsixv0.3.15, CI run41725914148)
Metadata
Metadata
Assignees
Labels
bugSomething isn't workingSomething isn't working
Activity