Home > Software design >  How to add a Behaviour to a Control in my Xaml but only for a specific platform?
How to add a Behaviour to a Control in my Xaml but only for a specific platform?

Time:10-29

I have created a behaviour for my Xamarin application but I only want this behaviour to be used when running on UWP. How can I do this in XAML?

I know about using OnPlatform, But I am not sure how to use something like that for Behaviours and Effects. (Or generally anything that is a list in XAML).

Any help on this would be much appreciated.

CodePudding user response:

You just could check in your OnAttachedOn method of your behaviour which device it is like so:

if(Device.RuntimePlatform == Device.UWP)
{
    //then do something
}

This is a good example how behaviours work: https://github.com/codemillmatt/xamarin-show-demos/blob/master/Behaviors/Behaviors/Behaviors/EntryPressCommandBehavior.cs

CodePudding user response:

I think this is what you can use.Made 3 different Entry's A,B and C . You can use them platform-specific In App.xaml

  <Style x:Key="AEntry" TargetType="Entry">
        <Setter Property="TextColor" Value="#A7ADB1" />
        <Setter Property="BackgroundColor" Value="#25333E" />
        <Setter Property="WidthRequest" Value="80" />
        <Setter Property="HeightRequest" Value="10" />
        <Setter Property="IsReadOnly" Value="True" />
        <Setter Property="Opacity" Value="0.8" />
        <Setter Property="HorizontalTextAlignment" Value="Center" />
    </Style>
    <Style x:Key="CEntry" TargetType="Entry">
        <Setter Property="TextColor" Value="#A7ADB1" />
        <Setter Property="BackgroundColor" Value="#25333E" />
        <Setter Property="WidthRequest" Value="80" />
        <Setter Property="HeightRequest" Value="10" />
        <Setter Property="IsReadOnly" Value="False" />
        <Setter Property="Opacity" Value="0.8" />
        <Setter Property="Keyboard" Value="Numeric" />
        <Setter Property="HorizontalTextAlignment" Value="Center" />
    </Style>
    <Style x:Key="BEntry" TargetType="Entry">
        <Setter Property="TextColor" Value="#A7ADB1" />
        <Setter Property="BackgroundColor" Value="#25333E" />
        <Setter Property="FontSize" Value="Medium" />
        <Setter Property="IsReadOnly" Value="True" />
        <Setter Property="HorizontalTextAlignment" Value="Center" />
    </Style>

Use in MainPage.xaml

  <Entry
                        x:Name="Totaaltanks"
                        Grid.Row="12"
                        Grid.Column="4"
                        Grid.ColumnSpan="2"
                        Style="{x:OnPlatform Android={StaticResource BEntry},
                                             UWP={StaticResource CEntry},
                                             iOS={StaticResource AEntry}}" />

Alternatively, you can set platform-specific style attributes in the same style.

<Style x:Key="AEntry" TargetType="Entry">
<Setter Property="FontSize" Value="{x:OnPlatform UWP='', iOS='22', Android='20'}" />
...
  • Related