Style was not found XAML - c#

So I have my App.xaml :
<Application xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="Fitness_App.App">
<Application.Resources>
<ResourceDictionary>
<Style x:Key="WorkoutStyle" TargetType="Label">
<Setter Property="FontSize" Value="28"/>
<Setter Property="FontFamily" Value="BebasNeue"/>
<Setter Property="TextColor" Value="Wheat"/>
</Style>
</ResourceDictionary>
</Application.Resources>
</Application>
Here I declared a style GLOBALLY as far as I've read the microsoft documentation.However , when in another xaml file I do this:
<Label Style= "{StaticResource WorkoutStyle}"
It says that WorkoutStyle could not be found.Any idea why?

Declaration is correct. Just Remove obj and bin of targeted project and do a rebuild.
This will solve the issue.

This is working
<Application.Resources>
<ResourceDictionary>
<Style x:Key="WorkoutStyle" TargetType="Label">
<Setter Property="FontSize" Value="28"/>
<Setter Property="TextColor" Value="Wheat"/>
</Style>
</ResourceDictionary>
</Application.Resources>
This line is not good
<Setter Property="FontFamily" Value="BebasNeue"/>
Your FontFamily is wrong look here for an example , change it with your font
<Style x:Key="NormalFont" TargetType="Label">
<Setter Property="FontFamily" Value="NunitoSans-Regular.ttf#NunitoSans-Regular.ttf"/>
</Style>
Explaned here
https://www.serkanseker.com/xamarin-forms-custom-font-family/

Related

Change Font Size of all UI Elements in using Static Resource from App.xaml in WPF

I'm need to change the font size of all text across the application.
I have tried doing as follows, but that doesn't work:-
<Style x:Key="fontsize" TargetType="{x:Type FrameworkElement}">
<Setter Property="Control.FontSize" Value="20"/>
</Style>
<Style TargetType="{x:Type FrameworkElement}" BasedOn="{StaticResource fontsize}"/>
When I try setting as follows then that works fine but doesn't get applied to all elements & needs to apply that for all different types of elements aperately.
<Style TargetType="TextBlock" BasedOn="{StaticResource fontsize}"/>
<Style TargetType="TextBox" BasedOn="{StaticResource fontsize}"/>
<Style TargetType="DataGridCell" BasedOn="{StaticResource fontsize}"/>
<Style TargetType="MenuItem" BasedOn="{StaticResource fontsize}"/>
<Style TargetType="DatePicker" BasedOn="{StaticResource fontsize}"/>
Also I would like to ask that, is there a way that I can override the Global Style for a particular element, like Heading text should be of different size on a user control?
in App.xaml
<Style TargetType="TextBlock">
<Setter Property="FontSize" Value="20"/>
<Setter Property="FontWeight" Value="Bold"/>
</Style>
Create a global style for the window in App.xaml.
<Application.Resources>
<Style x:Key="WindowStyle" TargetType="{x:Type Window}">
<Setter Property="FontStyle" Value="Italic" />
<Setter Property="FontSize" Value="24" />
<Setter Property="Foreground" Value="Green"/>
</Style>
</Application.Resources>
and set that style for the required windows.
<Window x:Class="YourNamespace.MainWindow" Style="{StaticResource WindowStyle}".....>
for overriding the style for a usercontrol
<local:UserControl1>
<local:UserControl1.Style>
<Style TargetType="UserControl">
<Setter Property="FontSize" Value="10"/>
</Style>
</local:UserControl1.Style>
</local:UserControl1>
There are two controls this involves.
You're maybe thinking "hey what about this cell or that calendar".
Their templates show text in a textblock.
When you set Header on a menuitem or content on a label, you get a textblock generated.
You therefore "only" need to set style on both textblock and textbox:
<Application.Resources>
<ResourceDictionary>
<Style TargetType="TextBlock">
<Setter Property="FontSize" Value="20"/>
</Style>
<Style TargetType="TextBox">
<Setter Property="FontSize" Value="20"/>
</Style>
</ResourceDictionary>
</Application.Resources>
</Application>
Having said that.
As Clemens pointed out.
The Font size and styling dependency properties are marked as inherits, so if you just have mainwindow then you could just set on that.
It's not just "obvious" that a label ends up with a textblock in it when you set content though. Similarly a menuitem and header. Hence I thought it worth posting this answer.

How to change all element's font family wpf-xaml

I use MetroDark theme.
I use this code in xaml :
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="Theme/MetroDark/MetroDark.MSControls.Core.Implicit.xaml" />
<ResourceDictionary Source="Theme/MetroDark/MetroDark.MSControls.Toolkit.Implicit.xaml" />
</ResourceDictionary.MergedDictionaries>
<Style TargetType="{x:Type Label}" >
<Setter Property="FontFamily" Value="B titr" />
<Setter Property="FontSize" Value="13" />
<Setter Property="FontWeight" Value="Bold" />
</Style>
<Style TargetType="{x:Type TextBox}" >
<Setter Property="FontFamily" Value="B Nazanin" />
<Setter Property="FontSize" Value="16" />
<Setter Property="FontWeight" Value="Bold" />
</Style>
</ResourceDictionary>
</Application.Resources>
font family was changed correctly but the background color was changed too.
I just want to change the font family and font size, and another property (such as: background, border etc) and get effect from default theme (MetroDark).
How can I do that?
set base style in BasedOn attribute like this:
<Style TargetType="{x:Type Label}" BasedOn="{StaticResource {x:Type Label}}">
<Style TargetType="{x:Type TextBox}" BasedOn="{StaticResource {x:Type TextBox}}">
this way custom style inherits all settings from base style and can override some of them

WPF dynamic resource not found in MergedDictionary but outside it is fine

I have a main Class (with an MainView.xaml) that dynamically loads various other Pages long after the MainView has been shown.
So MainView.xaml has following style defined:
<Window.Resources>
<Style x:Key="ErrorTemplate" TargetType="TextBox">
<Setter Property="FontSize" Value="30"/>
</Style>
</Window.Resources>
In one of the loaded pages I had following declaration:
<Page.Resources>
<Style x:Key="ErrorStyle" TargetType="TextBox">
<Setter Property="Validation.ErrorTemplate" Value="{DynamicResource ErrorTemplate"/>
</Style>
</Page.Resources>
This worked fine.
Now I needed to load other resources from an external XAML file so I used MergedDictionaries.
<Page.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<!-- Guaranteed to not have an ErrorTemplate key -->
<ResourceDictionary Source="MoreStyles.xaml"/>
<ResourceDictionary>
<Style x:Key="ErrorStyle" TargetType="TextBox">
<Setter Property="Validation.ErrorTemplate" Value="{DynamicResource ErrorTemplate"/>
</Style>
</ResourceDictionary>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Page.Resources>
And now I started to receive the message that ErrorTemplate could not be resolved.
Moving it outside of the MergedDictionary fixed the problem again.
<Page.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="MoreStyles.xaml"/>
</ResourceDictionary.MergedDictionaries>
<Style x:Key="ErrorStyle" TargetType="TextBox">
<Setter Property="Validation.ErrorTemplate" Value="{DynamicResource ErrorTemplate"/>
</Style>
</ResourceDictionary>
</Page.Resources>
What is happening here?

how to load custom staticresource

I have to 2 files:
ButtonStyle.xaml and
ConstantsStyle.xaml
in App.xaml, i will init
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="/Zalo;component/ResourceDictionary/480x800/ConstantsStyle.xaml"/>
<ResourceDictionary Source="/Zalo;component/ResourceDictionary/480x800/ButtonStyle.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
......
</Application.Resources>
file ConstantsStyle.xaml
<ResourceDictionary
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:vsm="clr-namespace:System.Windows;assembly=System.Windows">
<Thickness x:Key="GenericButtonStylePadding">0,7</Thickness>
</ResourceDictionary>
file ButtonStyle.xaml
<ResourceDictionary
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:vsm="clr-namespace:System.Windows;assembly=System.Windows"
xmlns:toolkit="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone.Controls.Toolkit"
xmlns:usercontrols="clr-namespace:Zalo.UserControls">
<Style x:Key="GenericButtonStyle" TargetType="Button">
<Setter Property="VerticalContentAlignment" Value="Center"/>
<Setter Property="HorizontalContentAlignment" Value="Center"/>
<Setter Property="Padding" Value="{StaticResource GenericButtonStylePadding}"/>
</Style>
</ResourceDictionary>
App crash runtime. Because Cannot find a Resource with the Name/Key GenericButtonStylePadding???
How can i run app correctly??? Please help me
Try using DyanamicResource instead of StaticResource
<Style x:Key="GenericButtonStyle" TargetType="Button">
<Setter Property="VerticalContentAlignment" Value="Center"/>
<Setter Property="HorizontalContentAlignment" Value="Center"/>
<Setter Property="Padding" Value="{DynamicResource GenericButtonStylePadding}"/>
</Style>
Edit:
You can test one thing. Add another constant to Constant.XAML
<Brush x:Key="BGBrush">Black</Brush>
Try to use it in your ButtonStyle.XAML
<Style x:Key="GenericButtonStyle" TargetType="Button">
<Setter Property="VerticalContentAlignment" Value="Center"/>
<Setter Property="HorizontalContentAlignment" Value="Center"/>
<Setter Property="Width" Value="200"></Setter>
<Setter Property="Background" Value="{StaticResource BGBrush}"></Setter>
</Style>
See if button background changes to Black.
If color is changing then try something else than Thickness.

How to set attributes without overriding the Mahapps theme?

I'm using Mahapps for a GUI, however I want to set some attributes different than visual ones such as margins and verticalAlignment, so I added this to the UserControl.resources section
<Style x:Key="{x:Type TextBox}" TargetType="TextBox" BasedOn="{StaticResource ResourceKey={x:Type TextBox}}">
<Setter Property="Margin" Value="2"/>
<Setter Property="VerticalAlignment" Value="Center"/>
</Style>
However it overrides all the visual styles attributes of the TextBoxes, how can I just add those attributes without overriding all the visual styles settings?
give the style a key
<Style x:Key="myCustomTextBoxStyle"
TargetType="TextBox"
BasedOn="{StaticResource ResourceKey={x:Type TextBox}}">
<Setter Property="Margin" Value="2"/>
<Setter Property="VerticalAlignment" Value="Center"/>
</Style>
and use it where you need it
<TextBox Style={StaticResource myCustomTextBoxStyle} />
EDIT
or put it to the main resource dictionary of user control or window resource without a key
<Window.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
</ResourceDictionary.MergedDictionaries>
<Style TargetType="TextBox"
BasedOn="{StaticResource ResourceKey={x:Type TextBox}}">
<Setter Property="Margin" Value="2"/>
<Setter Property="VerticalAlignment" Value="Center"/>
</Style>
</ResourceDictionary>
</Window.Resources>
hope that helps

Categories

Resources