Issue I can't solve in the SimpleBlogReader Windows 8 tutorial - windows-8

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.

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.

Can merged resource dictionaries access resources from App.xaml?

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>

Create a theme in Windows 8.1

In Windows 8, you were able to create your own themes for your application (here's a tutorial).
In Windows 8.1 Applications, themes are handled differently: you can change them at run-time and set a theme for a specific control in your XAML (if you don't want to apply the theme to the whole Application).
For instance:
<Grid x:Name="MainGrid" RequestedTheme="Dark">
However, I could not find a way to create my own themes. The property RequestedTheme takes an enumeration (its type is FrameworkElement.RequestedTheme) and an enumeration by definition cannot be extended (in C#). Also, if I want to define a new Theme Dictionary I would have written:
<ResourceDictionary.ThemeDictionaries>
But it is not available in Windows 8.1.
How can one create a theme in Windows 8.1? Am I limited to the existing ones (light & dark)?
Yes you're restricted to 3 themes I believe
Default (light)
Dark
High Contrast
You can create new styles or override existing ones for the 3 themes like this in 8.1
<ResourceDictionary.ThemeDictionaries>
<ResourceDictionary x:Key="Default">
<Style TargetType="TextBlock">
<Setter Property="FontSize" Value="24" />
<Setter Property="Foreground" Value="Green"/>
</Style>
</ResourceDictionary>
<ResourceDictionary x:Key="Dark">
<Style TargetType="TextBlock">
<Setter Property="FontSize" Value="30" />
<Setter Property="Foreground" Value="Orange"/>
</Style>
</ResourceDictionary>
<ResourceDictionary x:Key="HighContrast">
<Style TargetType="TextBlock">
<Setter Property="FontSize" Value="24" />
<Setter Property="Foreground" Value="Blue"/>
</Style>
</ResourceDictionary>
</ResourceDictionary.ThemeDictionaries>

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>

winrt xaml merged resources

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>