Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/xamarin/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Xamarin表单在iOS上隐藏一个按钮,并在Android上显示_Xamarin_Xamarin.forms_Visibility - Fatal编程技术网

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

Xamarin表单在iOS上隐藏一个按钮,并在Android上显示,xamarin,xamarin.forms,visibility,Xamarin,Xamarin.forms,Visibility,如何在iOS上隐藏按钮、标签或网格单元格并在android上显示,我有一个xamarin.forms应用程序(便携式),我知道我必须在平台上使用,但如何访问控件的可见性 谢谢如果您想在XAML上执行此操作,为了隐藏特定平台上的视图,您可以使用以下方法: <Button> <Button.IsVisible> <OnPlatform x:TypeArguments="x:Boolean" iO

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


谢谢

如果您想在XAML上执行此操作,为了隐藏特定平台上的视图,您可以使用以下方法:

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


希望有帮助

如mindOfAi所述,您可以在XAML中这样做:

// IOS, Android, WP
SomeButton.IsVisible = Device.OnPlatform<bool>(false, true, true);
<Button>
    <Button.IsVisible>
        <OnPlatform x:TypeArguments="x:Boolean"
                      iOS="false"
                      Android="true"/>
    </Button.IsVisible>
</Button>

所有这些答案似乎都涉及到创建控件,不管您是否真正需要它,然后在您不想要的平台上将IsVisible设置为false。更好的解决方案是,如果您确实需要控件,那么首先只创建它。第一步是将其包装到内容视图中:

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


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

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

真的
假的

Nigel

对于那些偶然发现这个问题并寻求代码隐藏解决方案的人:

            switch (Device.RuntimePlatform)
            {
                case Device.iOS:
                    //iOS specific code here
                    break;
                case Device.Android:
                     //Android specific code here
                    break;
            }
设备类具有以下设备常量:


.

但是应用程序崩溃了:在xmlns中找不到类型IsVisible这就是为什么我需要知道我必须把x:TypeArgumentsHi@Mireille放在哪里,看看我的答案,它应该会帮助你:)如果是的话,把它标记为答案,这样它会帮助其他人!:)只是为了澄清一下:“IsVisible”是属性名。您应该将x:TypeArguments作为属性的实际类型,这是一个bool(标准内容页中的x:Boolean)。谢谢您的回复,它帮助了我很多,很高兴我帮助了您!但是,看起来您是通过代码隐藏来寻找答案的。
<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>
            switch (Device.RuntimePlatform)
            {
                case Device.iOS:
                    //iOS specific code here
                    break;
                case Device.Android:
                     //Android specific code here
                    break;
            }