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

前端 未结 6 1421
天命终不由人
天命终不由人 2020-12-31 14:23

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 th

相关标签:
6条回答
  • 2020-12-31 14:49

    From Xamarin.Forms version 2.5.x this is done as per code below. Using a basic button as an example.

    <Button Text="NFC Pairing" Command="{Binding YourVmCommand}">
        <Button.IsVisible>
            <OnPlatform x:TypeArguments="x:Boolean">
                <On Platform="iOS">true</On>
                <On Platform="Android">false</On>
            </OnPlatform>
        </Button.IsVisible>
    </Button>
    

    Nigel

    0 讨论(0)
  • 2020-12-31 14:51

    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!

    0 讨论(0)
  • 2020-12-31 14:55

    Like mindOfAi mentioned you can do this in XAML like this:

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

    In code you can use the Device.OnPlatform or check the Device.OS property.

    That would look like:

    // ... Other code here
    Device.OnPlatform(iOS: () => { myButton.IsVisible = false; });
    
    // Or do this:
    if (Device.OS == TargetPlatform.iOS)
        myButton.IsVisible = false;
    
    // ... Other code here
    
    0 讨论(0)
  • 2020-12-31 14:57

    For anyone that stumbles upon this question seeking for the codebehind solution:

                switch (Device.RuntimePlatform)
                {
                    case Device.iOS:
                        //iOS specific code here
                        break;
                    case Device.Android:
                         //Android specific code here
                        break;
                }
    

    The Device class has the following Device constants:

    Constants as shown from VS 2019 Intellisense.

    0 讨论(0)
  • 2020-12-31 15:04

    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.

    0 讨论(0)
  • 2020-12-31 15:08
    // IOS, Android, WP
    SomeButton.IsVisible = Device.OnPlatform<bool>(false, true, true);
    

    Or

    if (Device.OS == TargetPlatform.Android)
    {
        SomeButton.IsVisible = true;
    }
    else
    ...
    
    0 讨论(0)
提交回复
热议问题