Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Xamarin Forms Hide a Button on iOS and Show it on Android

How to hide a button, a label or a grid cell on iOS and show it on android, I have a xamarin.forms app (portable), I know that I have to use on platform but how to access the visibility of the controls.

Thanks

like image 996
Mireille Avatar asked Feb 15 '17 09:02

Mireille


3 Answers

If you want to do it on XAML, in order to hide a view on a specific platform, you can use this:

  <Button>
      <Button.IsVisible>
        <OnPlatform x:TypeArguments="x:Boolean"
                      iOS="false"
                      Android="true"/>
      </Button.IsVisible>
    </Button>

Hope it helps!

like image 104
mindOfAi Avatar answered Sep 18 '22 17:09

mindOfAi


All of these answers seem to involve creating the control whether or not you actually need it and then setting IsVisible to false on the platforms you don't want it on. A better solution IMO is to only create the control in the first place if you do in fact need it. A first step would be to wrap it in a content view:

<ContentView>
    <OnPlatform x:TypeArguments="View">
        <OnPlatform.Android>
            <Button Text="Something" ...etc... />
        </OnPlatform.Android>
    </OnPlatform>
</ContentView>

That's better, but it still creates a superfluous ContentView. Take it one step further and use OnPlatform to declare a ControlTemplate and you'll achieve the most optimal implementation across all platforms.

like image 21
Mark Feldman Avatar answered Sep 18 '22 17:09

Mark Feldman


// IOS, Android, WP
SomeButton.IsVisible = Device.OnPlatform<bool>(false, true, true);

Or

if (Device.OS == TargetPlatform.Android)
{
    SomeButton.IsVisible = true;
}
else
...
like image 26
Zroq Avatar answered Sep 21 '22 17:09

Zroq