I have downloaded a font from fonts.google.com and would like to set the font family for all labels in my Xamarin Forms app.
Adding the following code to all labels seems like an excessive amount of work
<Label.FontFamily>
<OnPlatform x:TypeArguments="x:String">
<OnPlatform.Android>Mogra-Regular.ttf#Mogra-Regular</OnPlatform.Android>
</OnPlatform>
</Label.FontFamily>
Ideally I would set the font family in the Resource Dictionary where I've already succesfully set text colour and font attributes, however, I've been unsuccesful in doing so.
I have managed to change the font family using a custom renderer but the moment I alter the label text colour or font attributes using dynamic resources the font family is reset to default.
Is there any way that will allow me to universally use my custom font and still be allowed to dynamically change other aspects of any given label?
Create an implicit Style (a Style without an x:Key) in your App.xaml and it'll be applied to all Labels
<Style TargetType="Label">
<Setter Property="FontFamily">
<Setter.Value>
<OnPlatform x:TypeArguments="x:String">
<OnPlatform.Android>Mogra-Regular.ttf#Mogra-Regular</OnPlatform.Android>
</OnPlatform>
</Setter.Value>
</Setter>
</Style>
Related
I Have a label set to relative positioning and auto size as shown below
<Style TargetType="Label">
<Setter Property="BackgroundColor" Value="Transparent" />
//Something like below?
<Setter Property="AbsoluteLayout.LayoutFlags" Value="PositionProportional | SizeProportional" />
</Style>
....
<Label Text="0" AbsoluteLayout.LayoutBounds="0.5, 0.00499999999999998, AutoSize, AutoSize"/>
Is there a way to specify multiple LayoutFlags in XAML?
As I run the code on devices with higher resolution the relative position of the label is correct but the size (font) of the label does not increase, although it is set to AutoSize. I figured I also needed to specify a LayoutFlag for the Label that is SizeProportional as well as PositionProportional. But how to do it in XAML? Currently the fonts don't resize when the device is rotated to landscape orientation.
As you can read here :
AbsoluteLayout on Xamarin Documentation
Flags are also specified in the declaration of views in the layout
using the AbsoluteLayout.LayoutFlags property. Note that flags can be
combined in XAML using a comma-separated list.
So in your case, there is no point since you want to use the All value (using both Positionning and Sizing proportionnaly).
But I had to use something like this and it works just fine :
<StackLayout AbsoluteLayout.LayoutBounds="0,1,1,100"
AbsoluteLayout.LayoutFlags="XProportional,YProportional,WidthProportional">
</StackLayout>
Hope it will help people like who googled it and found this post :)
I want to set the background image for all the pages in my windows phone 8.1 app (RT not Silverlight) by declaring a style that targets the page.
Like this:
<Style TargetType="Page">
<Setter Property="Background" Value="Red" />
</Style>
It seems to work in the designer, as I see the red background on all my pages. However when I actually run the app the background is missing (black, blank) not red.
Some of the pages in my app derive from a custom type (which derives from Page) and I know that TargetType doesn't inherit. So I added additional styles for these:
<Style TargetType="local:ViewBase">
<Setter Property="Background" Value="Red" />
</Style>
Again, in the designer I see the Red (tho strangely enough I also saw the red when I was only targeting Page). Upon launch again however, the background is not Red, but blank (black).
I could easily give it a key, or add the Background property to every page and bind it to a resource, but I thought the whole point of Implicit styles was to allow me to override every instance of the control...
Can I not target a Page for a default (implicit) style?
aha: https://social.msdn.microsoft.com/Forums/windowsapps/en-US/bb927601-2e3e-45ac-afe8-621df57b9738/why-does-style-targettypepage-not-work-in-an-application?forum=winappswithcsharp
looks like this is a known issue, boo I say! I'll set the background to a resource then.
Why don't you override brush in application resource
<SolidColorBrush x:Name="ApplicationPageBackgroundThemeBrush" Color="RED"/>
In a windows UWP app project, I am trying to define a Thickness by assigning to its Left, Top, Right and Bottom properties:
<Setter Property="Margin">
<Setter.Value>
<Thickness Left="{StaticResource SomeDouble}"
Top="0"
Right="0"
Bottom="0" />
</Setter.Value>
</Setter>
This answer seems to suggest that this is possible in WPF, however, in my UWP project (as well as in a WinRT app), I get the following error:
XAML Thickness type cannot be constructed.
In order to be constructed in XAML, a type cannot be abstract, interface, nested, generic
or a struct, and must have a public default constructor.
Is there a way to define the Thickness using resources?
You still can. Only to change System:Double to x:Double. e.g.
<x:Double x:Key="SomeDouble">12</x:Double>
Update
Interesting, with the above xaml the designer shows up fine but it doesn't compile... Yes it gives the exact same error like you showed.
So I guess you have to define the whole Thickness in xaml.
<Thickness x:Key="Thickness1">12,0,0,0</Thickness>
<Setter Property="Margin" Value="{StaticResource Thickness1}" />
I ran the project this time and yeah it worked. :)
I am using the excellent Modern UI Charts for creating a Doughnut chart in my Windows 8 application. But I haven't been able to customize it much which has led me to look for other ways to create a doughnut chart. I would be very happy if anyone could answer my below questions on how to customize the Modern UI chart or if not then please do suggest on how i can implement my own Doughnut chart or any other FREE library which i can use to implement it.
Customizability questions on Modern UI charts:
How to increase the size of the Doughnut chart ? (Major Issue)
How to show percentages instead of numbers on the chart ? I know I can calculate the percentages and pass them to the chart but I will still not see the "%" symbol on the UI which will confuse the user as to whether the numbers are actual statistic number of percentages (Minor Issue)
Is there any way I can show the legend items vertically rather than horizantally ? (Minor Issue)
I would be happy if atleast I can increase the size of the chart otherwise I will have to look at a totally new way to create doughnut charts. Any lead or help on creating doughnut charts will be deeply appreciated. Thank you
1) You should be able to set the size of the chart. Try changing the chart style. If it's a larger PlottedArea that you want, the styles for those are in there as well.
2) You should be able to change the style for the PiePieceLabel. You can either set one for a given control or all over. Below is an example for setting it globally.
<Style TargetType="charts:PiePieceLabel">
<Setter Property="Background" Value="White" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="charts:PiePieceLabel">
<Border>
<!-- Changed it to a StackPanel -->
<StackPanel Orientation="Horizontal">
<TextBlock Text="{TemplateBinding Value}" />
<!-- May need a margin -->
<TextBlock Text="%" />
</StackPanel>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
3) Yes. Same deal. Find the Legend style. Change it to display vertically.
When I create a new page on a windows 8 store application it has default color which I want to change. If I remove all elements on the page and change the background color it has no effect. I've set the back ground to pink in my example below. How can I make this color take effect? (I've also removed everything out of App.xaml)
<common:LayoutAwarePage
x:Name="pageRoot"
x:Class="DemoWindows8StoreApp.BasicPage3"
DataContext="{Binding DefaultViewModel, RelativeSource={RelativeSource Self}}"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:DemoWindows8StoreApp"
xmlns:common="using:DemoWindows8StoreApp.Common"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
Background="Pink"
mc:Ignorable="d">
It's best to follow the default templates in that rather than setting the Background of the Page, but of the root element (usually a Grid), I'm not 100% sure why a Background on that page doesn't work (I suspect the control template).
UPDATE 1
According to Control.Background
Each control might apply this property differently based on its visual
template. This property only affects a control whose template uses the
Background property as a parameter. On other controls, this property
has no effect. For more info about visual templates, see the Template
property.
So there might be possible that Page's template doesn't use Background property as a parameter.
Remove nothing from the project to change the color. Go to Common\StandardStyles.xaml. Search for "LayoutRootStyle". You will find style for Panel. Change Background there. Please note this will be affected to all the pages in the project. If you want different color for different page then you can create separate style for each page.
<Style x:Key="LayoutRootStyle" TargetType="Panel">
<Setter Property="Background" Value="Pink"/>
<Setter Property="ChildrenTransitions">
<Setter.Value>
<TransitionCollection>
<EntranceThemeTransition/>
</TransitionCollection>
</Setter.Value>
</Setter>
</Style>