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>
Related
Can merged resource dictionaries access resources from App.xaml? The goal is to split the style to have it more readable.
This is what I'm looking for, but doesn't work in that way:
App.xaml in UWP project
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="Styles\DefaultButtonStyle.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
<!-- other custom styles, definitions, ThemeDictionaries, ... -->
<Color x:Key="Primary">#dfdfdf</Color>
</Application.Resources>
DefaultButtonStyle.xaml
<ResourceDictionary
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:AppName.UWP.Styles">
<!-- some definitions -->
<Style TargetType="Button" x:Key="DefaultButtonStyle">
<!-- my styles -->
<Setter Property="Background" Value="{StaticResource Primary}" />
</Style>
</ResourceDictionary>
The app crashes with
Cannot find a Resource with the Name/Key Primary
I could put everything in one big style.xaml, or copy the needed values in each xaml file, but aren't there other options? Could a merged dictionary include another merged dictionary? Or something like that?
I have used separated dictionaries and have tried to keep them in order of usage. In my application I have:
ColorsAndBrushes.xaml
SizesAndLayout.xaml
DefaultStyles.xaml
NamedStyles.xaml
Where ColorsAndBrushes looks something like:
<ResourceDictionary
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:MyApp.App.Styles">
<!-- Colors -->
<Color x:Key="Color_Banner">#FF333232</Color>
<!--overridden from themeresource-->
<Color x:Key="SystemChromeDisabledLowColor">#FFA8A49F</Color>
<Color x:Key="SystemAccentColor">#FF2877CF</Color>
<!-- /Colors -->
<!-- Brushes -->
<SolidColorBrush x:Key="Brush_Banner" Color="{StaticResource Color_Banner}" />
<!-- /Brushes -->
</ResourceDictionary>
SizesAndLayout:
<ResourceDictionary
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:MyApp.App.Styles">
<!-- Padding -->
<Thickness x:Key="Padding_Button">24,4</Thickness>
<Thickness x:Key="Padding_Dialog">10</Thickness>
<Thickness x:Key="Padding_Content">20</Thickness>
<!-- /Padding -->
<!-- Fonts -->
<FontFamily x:Key="Font_DefaultFamily">Segoe UI</FontFamily>
<FontWeight x:Key="Font_DefaultWeight">SemiLight</FontWeight>
<FontWeight x:Key="Font_NormalWeight">Normal</FontWeight>
<FontWeight x:Key="Font_BoldWeight">Semibold</FontWeight>
<x:Double x:Key="ContentControlFontSizeSmall">11</x:Double>
<x:Double x:Key="Font_NormalSize">20</x:Double>
<x:Double x:Key="Font_H1Size">36</x:Double>
<x:Double x:Key="Font_H2Size">28</x:Double>
<!-- /Fonts -->
</ResourceDictionary>
DefaultStyles (apply to all of type - these use resources from other 2):
<ResourceDictionary
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:MyApp.App.Styles">
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="ColorsAndBrushes.xaml" />
<ResourceDictionary Source="SizesAndLayout.xaml" />
</ResourceDictionary.MergedDictionaries>
<Style TargetType="TextBlock">
<Setter Property="FontFamily" Value="{StaticResource Font_DefaultFamily}" />
<Setter Property="FontWeight" Value="{StaticResource Font_DefaultWeight}" />
<Setter Property="FontSize" Value="{StaticResource Font_NormalSize}" />
<Setter Property="TextWrapping" Value="WrapWholeWords" />
</Style>
</ResourceDictionary>
and NamedStyles are overrides of the default:
<ResourceDictionary
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:MyApp.App.Styles">
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="ColorsAndBrushes.xaml" />
<ResourceDictionary Source="SizesAndLayout.xaml" />
<ResourceDictionary Source="DefaultStyles.xaml" />
</ResourceDictionary.MergedDictionaries>
<Style x:Key="FontStyle_H1" TargetType="TextBlock" BasedOn="{StaticResource FontStyle_Default}">
<Setter Property="FontSize" Value="{StaticResource Font_H1Size}" />
<Setter Property="Foreground" Value="{StaticResource Brush_DarkBlue}" />
<Setter Property="Margin" Value="{StaticResource Margin_TitleFont}" />
</Style>
</ResourceDictionary>
And finally, in the App.xaml:
<Application
x:Class="MyApp.App.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:MyApp.App"
RequestedTheme="Light">
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="Styles/ColorsAndBrushes.xaml" />
<ResourceDictionary Source="Styles/SizesAndLayout.xaml" />
<ResourceDictionary Source="Styles/DefaultStyles.xaml" />
<ResourceDictionary Source="Styles/NamedStyles.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
</Application>
It works for me and keeps the XAML files smaller by using the smaller scoped files. However, I will say there are times that Visual Studio will give me a bunch of squigglies complaining that it can't figure out the namespace... but only when the file is open.
I have also experienced that, while at runtime, the order of the declaration of static resources does not matter, at times, the designer within Visual Studio will not render the styles if they aren't in a top-down format.
Good luck!
Try this:
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="Styles\DefaultButtonStyle.xaml"/>
</ResourceDictionary.MergedDictionaries>
<!-- other custom styles, definitions, ThemeDictionaries, ... -->
<Color x:Key="Primary">#dfdfdf</Color>
</ResourceDictionary>
</Application.Resources>
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
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>
I am following the tutorial located here:
http://msdn.microsoft.com/en-us/library/windows/apps/hh465045.aspx
In the section about styling the application they want you to insert some Xaml in the ResourceDictionary
<SolidColorBrush x:Key="WindowsBlogBackgroundBrush" Color="#FF0A2562"/>
<Style x:Key = "WindowsBlogLayoutRootStyle" TargetType = "Panel" BasedOn = "{StaticResource LayoutRootStyle}">
<Setter Property="Background" Value="{StaticResource WindowsBlogBackgroundBrush}"/>
</Style>
However, the compiler gives me these errors:
error WMC0035: Duplication assignment to the '_Items' property of the 'ResourceDictionary' object
Property elements cannot be in the middle of an element's content. They must be before or after the content.
Anyone have any ideas as to whats going on here?
Thanks
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary>
<SolidColorBrush x:Key="WindowsBlogBackgroundBrush" Color="#FF0A2562" />
<Style x:Key="WindowsBlogLayoutRootStyle" TargetType="Panel" >
<Setter Property="Background" Value="{StaticResource WindowsBlogBackgroundBrush}"/>
</Style>
</ResourceDictionary>
<!--
Styles that define common aspects of the platform look and feel
Required by Visual Studio project and item templates
-->
<ResourceDictionary Source="Common/StandardStyles.xaml">
</ResourceDictionary>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
this is just working fine i have tested it a layout grid.
I need to separate application styles to several xaml files.
But I also need to define some shared values like
<x:Double x:Key="SharedValue">100</x:Double>
in single file for use this value in styles defined in other files.
For instance:
<Style x:Name="SomeStyle" TargetType="TextBox">
<Setter Property="Width" Value="{StaticResource SharedValue}"/>
</Style>
and in another resource dictionary file:
<Style x:Name="AnotherStyle" TargetType="Button">
<Setter Property="Height" Value="{StaticResource SharedValue}"/>
</Style>
But when I try to define merged resource dictionary in App.xaml file
<Application.Resources>
<ResourceDictionary >
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="DefinedValues.xaml"/>
<ResourceDictionary Source="Styles1.xaml"/>
<ResourceDictionary Source="Styles2.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
I get this runtime exception:"Message = "Cannot find a Resource with the Name/Key SharedValue"
Can you tell me is it posible to do this and what I'm doing wrong?
Thanks.
Using merged dictionaries can get a bit tricky if you have dependencies between other merged dictionaries.
When you have multiple application-scope resources the order of the declare is important. They are resolved in the inverse order of the declare, so in your case you should have the order.
<Application.Resources>
<ResourceDictionary >
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="Styles1.xaml"/>
<ResourceDictionary Source="Styles2.xaml"/>
<ResourceDictionary Source="DefinedValues.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
Also, you may need to reference the other ResourceDictionary in Styles1.xaml. This worked for me in Styles1.xaml.
<ResourceDictionary "...">
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="SharedValues.xaml" />
</ResourceDictionary.MergedDictionaries>
<Style x:Name="AnotherStyle"
TargetType="Button">
<Setter Property="Height"
Value="{StaticResource SharedValue}" />
</Style>
</ResourceDictionary>