Set View property for one platform only in XAML

LeoJHarrisLeoJHarris NZMember ✭✭✭
edited September 30 in Xamarin.iOS

Hi all,

Im trying to set some entry HeightRequest value for iOS and leave android, in code I I know I can simply check if iOS and then apply some value on the property through x:Name="EntryFoo" like the below:

if (Device.RuntimePlatform == Device.iOS)
            {
                if (Device.Idiom == TargetIdiom.Tablet)
                {
                    EntryFoo.HeightRequest = 70;
                }
                else
                {
                    EntryFoo.HeightRequest = 50;
                }
            }

However in XAML how might I do this?

I have tried the following which did not work as it is expecting a value for android (I dont want a default value either like: `Default="40"' ):

<OnPlatform x:Key="EntryHeightRequest" x:TypeArguments="x:Double">
        <On Platform="iOS">
            <OnIdiom
                x:TypeArguments="x:Double"
                Phone="50"
                Tablet="70" />
        </On>
    </OnPlatform>

<Entry  
           x:Name="FooEntry" 
            HeightRequest="{StaticResource EntryHeightRequest}">

I only want the height request set on iOS devices for my entries and on android it can be left to the platform to set the height request. I want to keep this XAML as well if possible?

Answers

  • ColeXColeX Member, Xamarin Team Xamurai

    The code you do in xaml is the SAME as

           if (Device.RuntimePlatform == Device.iOS)
            {
                if (Device.Idiom == TargetIdiom.Tablet)
                {
                    EntryFoo.HeightRequest = 70;
                }
                else
                {
                    EntryFoo.HeightRequest = 50;
                }
            }
            else
            {
                EntryFoo.HeightRequest = 0;
            }
    

    I want to keep this XAML as well if possible?

    So it is impossible .

  • LeoJHarrisLeoJHarris NZMember ✭✭✭
    edited September 30
    Hi @ColeX sorry what I was meaning was I want to find a way to implement this in XAML and not in code, for a simplified case the following wont display the entry on android device but correctly sets the heightRequest on iOS:

    <OnPlatform
    x:Key="EntryHeightRequest"
    x:TypeArguments="x:Double"
    iOS="50" />

    <Entry
    ...
    HeightRequest="{StaticResource EntryHeightRequest}"
    />

    I bassically want to hardcode a HeightRequest value for iOS and let Android automatically calculate its own height if possible using XAML.
  • ColeXColeX Member, Xamarin Team Xamurai
    edited October 3

    As i said above , if you use the code you provided , the HeightRequest on android would be set to 0 .

    So i don't think it can be achieved by xaml.

  • LeoJHarrisLeoJHarris NZMember ✭✭✭
    @ColeX thanks for the response, i thought thta maybe the case regarding xaml. Appreciate the info
Sign In or Register to comment.