What are the OnPlatform Strings in Xaml(.Net Maui) - xaml

I have a Maui app and want to style my app according to the platform I am on.
In a resource dictionary, I can use the following
<ResourceDictionary
xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml">
<OnPlatform x:Key="ButtonSize" x:TypeArguments="x:Double">
<On Platform="iOS" Value="14" />
<On Platform="Android" Value="16" />
</OnPlatform>
</ResourceDictionary>
What are the "Platform Strings" for Windows and Mac?
<On Platform="???" Value="16" />

according to the docs
Android
iOS
MacCatalyst
Tizen
WinUI

Related

Can't get FontSize for labels to work in ResourceDictionary when using OnPlatform

When using <Style TargetType="Label"> I cant get FontSize to work with NamedSize.
Using Xamarin.Forms version : 3.4.0.1008975
Working :
<Style TargetType="Label">
<Setter Property="FontSize" Value="Small" />
</Style>
NOT working :
<Style TargetType="Label">
<Setter Property="FontSize">
<Setter.Value>
<OnPlatform x:TypeArguments="x:String"
Android="Small"
iOS="Medium"/>
</Setter.Value>
</Setter>
</Style>
I have tried to change the TypeArguments to <OnPlatform x:TypeArguments="NamedSize" .../> with no success.
I am using other propertysetters with <OnPlatform .../> and they work as they should. Its just FontSize.
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:local="clr-namespace:App67"
x:Class="App67.MainPage">
<ContentPage.Resources>
<OnPlatform x:Key="textColor"
x:TypeArguments="Color">
<On Platform="WinPhone" Value="Red" />
<On Platform="Android" Value="Aqua" />
<On Platform="iOS" Value="#80FF80" />
</OnPlatform>
<OnPlatform x:Key="fontSize"
x:TypeArguments="Font">
<On Platform="WinPhone" Value="Bold,Large" />
<On Platform="Android" Value="Bold,Large" />
<On Platform="iOS" Value="Bold,Large" />
</OnPlatform>
</ContentPage.Resources>
<ContentPage.Content>
<StackLayout>
<Label Text="Check out my style." TextColor="{StaticResource textColor}" Font="{StaticResource fontSize}" />
</StackLayout>
</ContentPage.Content>
</ContentPage>

Platform specific CornerRadius on <Frame> Xamarin.Forms

I'm trying to assign different CornerRadius on iOS and Android like following:
<Frame
HasShadow="false"
Padding="10"
BackgroundColor="Red">
<Frame.CornerRadius>
<OnPlatform x:TypeArguments="x:Double">
<On
Platform="iOS">20</On>
<On
Platform="Android">30</On>
</OnPlatform>
</Frame.CornerRadius>
<Label
Text="Hello World" />
</Frame>
But getting a
Cannot assign property "CornerRadius": Property does not exists, or is not assignable, or mismatching type between value and property
I've tried x:TypeArguments="Thickness" and x:TypeArguments="x:Int32". Decompiling the Assembly it appears CornerRadius is of type float. However, there is no Float property in x namespace, I mean x:TypeArguments="x:Float" doesn't exists.
Any ideas what I'm doing wrong or this is a bug?
The CornerRadius type is a Single:
<Frame HasShadow="true" OutlineColor="Red">
<Frame.CornerRadius>
<OnPlatform x:TypeArguments="x:Single">
<On Platform="iOS" Value="20"/>
<On Platform="Android" Value="30"/>
</OnPlatform>
</Frame.CornerRadius>
<Frame.Content>
<Label Text="Welcome to Xamarin Forms!" VerticalOptions="Center" HorizontalOptions="Center" />
</Frame.Content>
</Frame>
Well I fell there is a syntax error here do something like this :
<Frame.CornerRadius>
<OnPlatform x:TypeArguments="x:Single">
<OnPlatform.Platforms>
<On Platform="iOS" Value="20" />
<On Platform="Android" Value="30" />
<On Platform="UWP" Value="30" />
</OnPlatform.Platforms>
</OnPlatform>
</Frame.CornerRadius>
Try this and in case it doesn't work kindly revert.

Xamarin's new way of performing OnPlatform StaticResource binding

Previously I was able to do something like this
<OnPlatform x:TypeArguments="Color" Android="{StaticResource Primary}"/>
Now that, that syntax is deprecated, I'm trying to do this:
<OnPlatform x:TypeArguments="Color">
<On Platform="Android">{StaticResource Primary}</On>
</OnPlatform>
but I'm getting the following error:
Cannot convert "{StaticResource Primary}" into Xamarin.Forms.Color
How should my syntax be?
Because StaticResource is a markup extension, you can use it either through attribute usage, or element usage
For example, try this:
<OnPlatform x:TypeArguments="Color">
<On Platform="Android" Value="{StaticResource Primary}" />
</OnPlatform>
or,
<OnPlatform x:TypeArguments="Color">
<On Platform="Android">
<StaticResource Key="Primary" />
</On>
</OnPlatform>

FontFamily defined in XAML per platform

In my Xamarin Forms project I would like to define the Form family onect per platform and the use that in the application.
So far I've hardcoded the FontFamily per controltype
<Style x:Key="TahomaBase_Label" TargetType="Label" BaseResourceKey="SubtitleStyle">
<Setter Property="FontFamily" Value="Tahoma" />
...
</Style>
Is it posible to set Fotnfamily globally in my XAML code preferable itn a OnPlatform tag?
Define a style in the App.xaml and then reference that style throughout the app. That way you can set the font once in the App.xaml using the OnPlatform tag and never have to worry about OnPlatform in all your other XAML files.
<Application xmlns="http://xamarin.com/schemas/2014/forms" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" x:Class="PlatformFontSample.App">
<Application.Resources>
<ResourceDictionary>
<OnPlatform x:Key="FontFamilyName" x:TypeArguments="x:String" iOS="MarkerFelt-Thin" Android="OpenSans" WinPhone="Segoe UI" />
<Style x:Key="FontLabel" TargetType="Label">
<Setter Property="FontFamily" Value="{DynamicResource FontFamilyName}" />
</Style>
</ResourceDictionary>
</Application.Resources>
</Application>
And then:
<Label Text="{Binding Name}" Style="{DynamicResource FontLabel}" FontSize="Medium" FontAttributes="Bold" LineBreakMode="NoWrap"/>

How to change windows universal app default accent color?

i want to change the accent color in my app (windows universal app) from the default one to a custom color that is defined in my app.
Any idea how can I do this?
You cannot really change the base Accent color in your application because that's defined by the system theme color (Taskbar, Startmenu, etc...). However, you can MODIFY the Accent color by using styles. Here's an example:
<ResourceDictionary
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:App1">
<!-- Basic Colors -->
<SolidColorBrush x:Key="SystemAccentTransparentHighBrush" Color="{ThemeResource SystemAccentColor}" Opacity="0.9"/>
<SolidColorBrush x:Key="SystemAccentTransparentMediumHighBrush" Color="{ThemeResource SystemAccentColor}" Opacity="0.7" />
<SolidColorBrush x:Key="SystemAccentTransparentMediumBrush" Color="{ThemeResource SystemAccentColor}" Opacity="0.6" />
<SolidColorBrush x:Key="SystemAccentTransparentMediumLowBrush" Color="{ThemeResource SystemAccentColor}" Opacity="0.5" />
<SolidColorBrush x:Key="SystemAccentTransparentLowBrush" Color="{ThemeResource SystemAccentColor}" Opacity="0.3" />
<SolidColorBrush x:Key="SystemAccentHighlightBrush" Color="{ThemeResource SystemAccentColor}" />
<SolidColorBrush x:Key="SystemBlackBrush" Color="{ThemeResource SystemChromeBlackHighColor}" />
<SolidColorBrush x:Key="SystemWhiteBrush" Color="{ThemeResource SystemChromeWhiteColor}" />
<SolidColorBrush x:Key="SystemGreyHighBrush" Color="#FFE0E0E0" />
<SolidColorBrush x:Key="SystemGreyMediumHighBrush" Color="#FFA0A0A0" />
<SolidColorBrush x:Key="SystemGreyMediumBrush" Color="#FF808080" />
<SolidColorBrush x:Key="SystemGreyMediumLowBrush" Color="#FF606060" />
<SolidColorBrush x:Key="SystemGreyLowBrush" Color="#FF404040" />
<SolidColorBrush x:Key="SystemAppBackgroundBrush" Color="{ThemeResource ApplicationPageBackgroundThemeBrush}" />
<Style x:Key="GridStyle_Page" TargetType="Grid">
<Setter Property="HorizontalAlignment" Value="Left" />
<Setter Property="VerticalAlignment" Value="Top" />
<Setter Property="ScrollViewer.VerticalScrollBarVisibility" Value="Auto" />
<Setter Property="ScrollViewer.HorizontalScrollBarVisibility" Value="Disabled" />
<Setter Property="Background" Value="{StaticResource SystemBlackBrush}" />
</Style>
</ResourceDictionary>
You would put that in a styles.xaml file. Then you have to tell the system about it as follows in the App.xaml file:
<Application
x:Class="App1.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:App1"
RequestedTheme="Light">
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="Styles.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
</Application>
Finally, you use the styles/colors in your application by doing one of the following:
Add a Style="{StaticResource }" to the element that you
want to style. You would replace with a style that is
defined in the Styles.xaml file above.
Add a color attribute such as Forground, Background, or Border to
the element that you want to color. To do this, add something like
Background="{StaticResource }" where the is
replaced by one of the color styles above.
To modify the accent color, you need to overlay it over a background color. Notice in the SolidColorBrush definitions above. Some of them have an Opacity setting. The range for this setting is between 1.0 and 0.0 where 1.0 is completely opaque while 0.0 is completely transparent. So the lower the Opacity setting, the more of the underlying color shows through. If the underlying color is black, then a lower Opacity setting will give you a darker Accent color. If the underlying color is white, then you will get a lighter Accent color as the Opacity value decreases.
A note about styles: Much of how xaml works is through styles. There are a couple of files on the computer that defines what all the styles are in the system. Those files are generic.xaml and themeresources.xaml. On my system (Windows 10 Pro RTM with VS2015), these files are found in the following location:
C:\Program Files (x86)\Windows Kits\10\DesignTime\CommonConfiguration\Neutral\UAP\10.0.10240.0\Generic
I hope this helps and sorry for the late answer. I just came across your question while I was googling for something else.
Have you tried using:
(App.Current.Resources["PhoneAccentBrush"] as SolidColorBrush).Color = Colors.Blue;
in you App.xaml.cs?