Is there a way to specify multiple LayoutFlags in XAML Xamarin? - xaml

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 :)

Related

Custom font for all labels Xamarin Forms

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>

Define a Thickness using resources

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. :)

ContentControl (CheckBox) does not inherit Foreground

It looks like the CheckBox control does not inherit its Foreground property:
<UserControl Foreground="Orange">
<StackPanel>
<!-- this shows as black - no inheritance -->
<CheckBox Content="foo" />
<!-- this shows as turquoise -->
<CheckBox Foreground="Turquoise" Content="bar" />
<!-- but this shows as orange - inheritance works here -->
<TextBlock Text="baz" />
</StackPanel>
</UserControl>
What's the reason for this, and what is the best workaround?
Edit #helb suggested the reason is the default style, which sets the Foreground to black. I don't think this explains it, though: if you override that style and remove the line - <Setter Property="Foreground" Value="#FF000000"/> - the behavior stays the same.
The best I could find is this, which may be an explanation:
Property value inheritance behavior is not globally enabled for all dependency properties, because the calculation time for inheritance does have some performance impact. Property value inheritance is typically only enabled for properties where a particular scenario suggests that property value inheritance is appropriate.
So maybe they decided not to apply Foreground inheritance to ContentControls, for performance reasons. Would be nice to be able to confirm this, though. (This answer claims to have used reflector to uncover an opaque hex value, in the internal DP registration, that may have something to do with it.)
This is one possible workaround (seems adequate, I guess):
<Style TargetType="CheckBox">
<Setter Property="Foreground" Value="{Binding RelativeSource={RelativeSource AncestorType=UserControl},Path=Foreground}" />
</Style>
Edit
More evidence for the "ContentControl doesn't inherit Foreground" theory: Button does not inherit; ComboBox does inherit.

How to create a Doughnut chart in windows 8 app?

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.

Change the default page color on a window 8 store application

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>