Cannot find a Resource with the Name / Key - xaml

I have a UWP App. The Styles works without a problem when I execute the app. But not in the Designer. Especially User Control can't be loaded. The Error message who is displayed is:
Cannot find a Resource with the Name / Key [Name]
When I go to that User Control it mostly is displayed correctly.
I reference the style with:
Style="{StaticResource DeemphasizedBodyTextBlockStyle}"
I have a file for my Textstyles. In that file I have the style defined as followed:
<Style x:Key="DeemphasizedBodyTextBlockStyle"
TargetType="TextBlock"
BasedOn="{StaticResource BodyTextBlockStyle}">
<Setter Property="FontSize"
Value="12" />
<Setter Property="TextWrapping"
Value="NoWrap" />
<Setter Property="CharacterSpacing"
Value="75" />
<Setter Property="HorizontalAlignment"
Value="Left" />
<Setter Property="VerticalAlignment"
Value="Top" />
<Setter Property="Foreground"
Value="{StaticResource AppForegroundColorSecondary}" />
</Style>
And in the properties of the view project I have a Designer Resource file added (as page) with the following content:
<ResourceDictionary
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="ms-appx:///Style/AppStyles.xaml" />
<ResourceDictionary Source="ms-appx:///Style/ColorsDark.xaml" />
<ResourceDictionary Source="ms-appx:///Style/ColorsLight.xaml" />
<ResourceDictionary Source="ms-appx:///Style/ControlStyles.xaml" />
<ResourceDictionary Source="ms-appx:///Style/TextStyles.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
The whole repo is available here: https://github.com/MoneyFox/MoneyFox

Related

UWP Global Resources is overriden by Local Resources even if not the same property

I have an application resource defined in App.xaml. It is tested that the resource applies to the local style if no local resource is defined. So this not some issue like "resource not being initialized".
The code is as below.
App.xaml
<Application
...omitted
RequestedTheme="Light">
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="Style/DefaultTheme.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
</Application>
DefaultTheme.xaml
<ResourceDictionary
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Style TargetType="Button">
<Setter Property="FontSize" Value="50" />
<Setter Property="Background" Value="Red" />
</Style>
</ResourceDictionary>
MainPage.xaml
...omitted
<StackPanel Orientation="Horizontal">
<StackPanel.Resources>
<Style TargetType="Button">
<Setter Property="Width" Value="100" />
<Setter Property="VerticalAlignment" Value="Stretch" />
</Style>
</StackPanel.Resources>
<Button Content="Hello" />
<Button Content="Happy" />
<Button Content="World" />
</StackPanel>
You can see that from the StackPanel's local resources. It is only setting Width and VerticalAlignment while the global dictionary is assigning FontSize and Background. There is no contradiction.
I have also tried this syntax in Xamarin Forms XAML. The local resource will only override global resource when they are assigning the same property. Global resources still apply to the element if not specified.
For more information, ReSharper is having a nice icon saying this "Hides resource from DefaultTheme.xaml ". But when I do this in Xamarin Forms, it will fade out only the properties that are being overridden.
Is there a way to make local resources an add on to global resources in UWP? Assigning x:Key to the global resource then add BasedOn will not be a solution. As I don't think making every button in the app having to derive the global resource make sense in theming.

Xaml Style Child Control in Universal Windows Platform (UWP)

I would like to know how to set style targetting child controls on the UWP within a style definition.
WPF seems to have 'Style.Resources' to define sub-styles but this doesn't seem the case for UWP
example in wpf : WPF - How to create a style that applies styles to child types
If you want the styles in separate sheets ( which you should. I showed the Styles in the control itself because I misread and thought that's what you wanted ) you can create a Resources folder and add different ResourceDictionaries. Personally I usually create a separate dictionary for Brushes, Styles, Templates, and Converters. Then you declare them in the ResourceDictionary.MergedDictionaries in App.xaml
<Application
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
RequestedTheme="Light">
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="/Resources/Brushes.xaml" />
<ResourceDictionary Source="/Resources/Styles.xaml" />
<ResourceDictionary Source="/Resources/Converters.xaml" />
<ResourceDictionary Source="/Resources/Templates.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
You can define Styles in the ResourceDictionary of the parent control. The Style defined in Window.Resources applies to all Rectangles because it doesn't specify a Key, so the Rectangle in the first StackPanel is yellow and small. The second StackPanel defines it's own Resources, which its children use, and they end up different colors and a larger size. There's also Style inheritance in there using BasedOn
<Window x:Class="GBTest.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-GBTest"
mc:Ignorable="d"
Title="MainWindow"
Height="350"
Width="525">
<!--Default style defined in the Window Resources-->
<Window.Resources>
<Style TargetType="Rectangle">
<Setter Property="Width"
Value="100" />
<Setter Property="Height" Value="100" />
<Setter Property="Fill"
Value="Yellow" />
</Style>
</Window.Resources>
<StackPanel>
<Rectangle />
<StackPanel>
<!--The styles defined in the resources of this StckPanel will apply to its children, overriding the default style defined in the Window Resources-->
<StackPanel.Resources>
<Style TargetType="Rectangle"
x:Key="BigStyle">
<Setter Property="Height"
Value="200" />
<Setter Property="Width"
Value="200" />
</Style>
<Style TargetType="Rectangle"
x:Key="RedStyle"
BasedOn="{StaticResource BigStyle}">
<Setter Property="Fill"
Value="Red" />
</Style>
<Style TargetType="Rectangle"
BasedOn="{StaticResource BigStyle}"
x:Key="BlueStyle">
<Setter Property="Fill"
Value="Blue" />
</Style>
</StackPanel.Resources>
<Rectangle Style="{StaticResource RedStyle}" />
<Rectangle Style="{StaticResource BlueStyle}" />
</StackPanel>
</StackPanel>

Windows Store app failed to create style BasedOn shared style

I have a windows 8.1 app.
In Project.Shared there is ResourceDictionary SharedResources.xaml with base style,
<Style x:Key="CommonLayerListItemStyle" TargetType="TextBlock">
<Setter Property="FontFamily" Value="Segoe WP" />
<Setter Property="Foreground" Value="{StaticResource UnselectBrush}" />
<Setter Property="VerticalAlignment" Value="Stretch" />
</Style>
in Windows Phone app is StyleResources.xaml wiht style based on this
<Style x:Key="LayerListItemStyle"
BasedOn="{StaticResource CommonLayerListItemStyle}"
TargetType="TextBlock">
<Setter Property="FontSize" Value="20" />
</Style>
same in Windows app StyleResources.xaml:
<Style x:Key="LayerListItemStyle"
BasedOn="{StaticResource CommonLayerListItemStyle}"
TargetType="TextBlock">
<Setter Property="FontSize" Value="28" />
<Setter Property="Margin" Value="42,0,0,0" />
</Style>
All styles are used in UserControl created in Shared project.
I do this to override FontSize on diffrent platforms.
I merged all dictionaries in App.xaml
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="Styles/SharedResources.xaml" />
<ResourceDictionary Source="Styles/StyleResources.xaml" />
</ResourceDictionary.MergedDictionaries>
But my app does not start with Unhandled exception
Cannot find a Resource with the Name/Key CommonLayerListItemStyle [Line: 10 Position: 37]
Why does this happens?
I had the same issue. You need to merge the SharedResources.xaml directly into StyleResources.xaml, and only reference StyleResources.xaml from App.xaml.
Here is a complete example:
http://blog.craftingbytes.com/2015/05/resource-sharing-in-windows-universal.html

MergedDictionaries can not be referenced from App.xaml

My styles are separated in separate files for better organization and readability. All files are referenced in my App.xaml. Some styles are BasedOn other styles, so I have placed them in the proper order so that the inherited styles are loaded before the dependent ones. However, I am still getting the following error on my XAML views that reference a font style, for example...Cannot find a Resource with the Name/Key SourceSansPro-Light.
Can someone enlighten me on why this Name/Key cannot be found? Here's the relevant code.
App.xaml
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="Common/Styles/StyleResources.xaml" />
<ResourceDictionary Source="Common/Styles/InputStyles.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
StyleResources.xaml
<ResourceDictionary.ThemeDictionaries>
<ResourceDictionary x:Key="Light">
<SolidColorBrush x:Key="TextBoxBackgroundBrush" Color="White"/>
</ResourceDictionary>
</ResourceDictionary.ThemeDictionaries>
<FontFamily x:Key="SourceSansPro-Light">../../Assets/Fonts/SourceSansPro-Light.otf#Source Sans Pro</FontFamily>
<Style x:Key="TextBlockStyle" TargetType="TextBlock" BasedOn="{StaticResource BaseTextBlockStyle}">
<Setter Property="FontSize" Value="18"/>
<Setter Property="FontFamily" Value="{StaticResource SourceSansPro-Semibold}" />
</Style>
InputStyles.xaml
<Style x:Key="SecondaryTextStyle" TargetType="TextBlock" BasedOn="{StaticResource TextBlockStyle}">
<Setter Property="FontFamily" Value="{StaticResource SourceSansPro-Light}"/>
</Style>
HomePage.xaml (where style, SourceSansPro-Light, is being called in this view)
<Page.Resources>
<Style x:Name="InfoBodyTextStyle" TargetType="TextBlock">
<Setter Property="FontFamily" Value="{StaticResource SourceSansPro-Light}"/>
</Style>
Please try this code.
<Style TargetType="TextBlock">
<Setter Property="Foreground" Value="#FF64CCEF"/>
<Setter Property="SnapsToDevicePixels" Value="True"/>
<Setter Property="FontFamily" Value="{DynamicResource FontFamilyResource}"/>
</Style>
<FontFamily x:Key="FontFamilyResource">/ApplicationName(SolutionName);component/Resources/Fonts/#Vladimir Script</FontFamily>

Use separate .xaml file to use resources in a WP8 app

I want use a external file to customize styles on my app, but it does not working. I am following this step-by-step but when I execute the project exception falls into:
A first chance exception of type 'System.Windows.Markup.XamlParseException' occurred in System.Windows.ni.dll
My XAML code:
app.xaml:
<Application.Resources>
<local:LocalizedStrings xmlns:local="clr-namespace:App1" x:Key="LocalizedStrings"/>
<ResourceDictionary x:Key="myDict">
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="Resources.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
Resources.xaml:
<ResourceDictionary
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Style TargetType="TextBox" x:Key="MyTextBox">
<Setter Property="Background" Value="Transparent"/>
<Setter Property="BorderThickness" Value="0.5"/>
<Setter Property="BorderBrush" Value="Gray"/>
<Setter Property="Opacity" Value="0.5"/>
<Setter Property="Foreground" Value="Red"/>
</Style>
</ResourceDictionary>
Try moving your local resource declarations inside the ResourceDictionary that you're creating and assigning to the Application.Resources property:
<Application.Resources>
<ResourceDictionary x:Key="myDict">
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="Resources.xaml"/>
</ResourceDictionary.MergedDictionaries>
<local:LocalizedStrings xmlns:local="clr-namespace:App1" x:Key="LocalizedStrings"/>
<!-- other resources in here -->
</ResourceDictionary>
</Application.Resources>