Xamarin表单在iOS上隐藏按钮并在Android上显示

ufj5ltwl  于 2022-12-07  发布在  iOS
关注(0)|答案(7)|浏览(145)

如何隐藏一个按钮,一个标签或一个网格单元在iOS上,并在Android上显示它,我有一个xamarin.窗体应用程序(便携式),我知道我必须在平台上使用,但如何访问控件的可见性.
谢谢

6fe3ivhb

6fe3ivhb1#

如果您想在XAML上执行此操作,以便在特定平台上隐藏视图,可以使用以下命令:

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

希望能有所帮助!

ni65a41a

ni65a41a2#

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

或者

if (Device.OS == TargetPlatform.Android)
{
    SomeButton.IsVisible = true;
}
else
...
3b6akqbq

3b6akqbq3#

所有这些答案似乎都涉及到创建控件,无论您是否真的需要它,然后在您不希望它出现的平台上将IsVisible设置为false。IMO的一个更好的解决方案是仅在您确实需要它时才创建控件。第一步是将它 Package 在内容视图中:

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

这样做更好,但仍然会创建一个多余的ContentView。更进一步,使用OnPlatform声明一个ControlTemplate,您将在所有平台上获得最佳实现。

kyvafyod

kyvafyod4#

就像mindOfAi提到的那样,您可以在XAML中这样做:

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

在程式码中,您可以使用Device.OnPlatform或检查Device.OS属性。
看起来像这样:

// ... Other code here
Device.OnPlatform(iOS: () => { myButton.IsVisible = false; });

// Or do this:
if (Device.OS == TargetPlatform.iOS)
    myButton.IsVisible = false;

// ... Other code here
s5a0g9ez

s5a0g9ez5#

从Xamarin.Forms版本2.5.x开始,这是按照下面的代码完成的。使用一个基本按钮作为示例。

<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>

奈杰尔

7vux5j2d

7vux5j2d6#

对于任何无意中发现这个问题并寻求代码隐藏解决方案的人:

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

Device类别具有下列Device常数:
Constants as shown from VS 2019 Intellisense

ix0qys7i

ix0qys7i7#

扩展解决方案,您还可以执行xaml内联:

IsVisible="{OnPlatform iOS=true, Android=false}"

相关问题