Xamarin Forms 在 iOS 上隐藏一个按钮并在 Android 上显示它
Xamarin Forms Hide a Button on iOS and Show it on Android
如何在 iOS 上隐藏按钮、标签或网格单元格并在 android 上显示它,我有一个 xamarin.forms 应用程序(便携式),我知道我必须在平台上使用,但如何访问控件的可见性。
谢谢
如果您想在 XAML 上执行此操作,以便在特定平台上隐藏视图,您可以使用:
<Button>
<Button.IsVisible>
<OnPlatform x:TypeArguments="x:Boolean"
iOS="false"
Android="true"/>
</Button.IsVisible>
</Button>
希望对您有所帮助!
// IOS, Android, WP
SomeButton.IsVisible = Device.OnPlatform<bool>(false, true, true);
或者
if (Device.OS == TargetPlatform.Android)
{
SomeButton.IsVisible = true;
}
else
...
就像 mindOfAi 提到的那样,您可以在 XAML 中这样做:
<Button>
<Button.IsVisible>
<OnPlatform x:TypeArguments="x:Boolean"
iOS="false"
Android="true"/>
</Button.IsVisible>
</Button>
在代码中你可以使用 Device.OnPlatform or check the 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
所有这些答案似乎都涉及创建控件,无论您是否真的需要它,然后在您不需要它的平台上将 IsVisible 设置为 false。 IMO 更好的解决方案是仅在您确实需要时才首先创建控件。第一步是将其包装在内容视图中:
<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>
奈杰尔
对于偶然发现此问题并寻求代码隐藏解决方案的任何人:
switch (Device.RuntimePlatform)
{
case Device.iOS:
//iOS specific code here
break;
case Device.Android:
//Android specific code here
break;
}
设备 class 具有以下设备常量:
Constants as shown from VS 2019 Intellisense.
如何在 iOS 上隐藏按钮、标签或网格单元格并在 android 上显示它,我有一个 xamarin.forms 应用程序(便携式),我知道我必须在平台上使用,但如何访问控件的可见性。
谢谢
如果您想在 XAML 上执行此操作,以便在特定平台上隐藏视图,您可以使用:
<Button>
<Button.IsVisible>
<OnPlatform x:TypeArguments="x:Boolean"
iOS="false"
Android="true"/>
</Button.IsVisible>
</Button>
希望对您有所帮助!
// IOS, Android, WP
SomeButton.IsVisible = Device.OnPlatform<bool>(false, true, true);
或者
if (Device.OS == TargetPlatform.Android)
{
SomeButton.IsVisible = true;
}
else
...
就像 mindOfAi 提到的那样,您可以在 XAML 中这样做:
<Button>
<Button.IsVisible>
<OnPlatform x:TypeArguments="x:Boolean"
iOS="false"
Android="true"/>
</Button.IsVisible>
</Button>
在代码中你可以使用 Device.OnPlatform or check the 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
所有这些答案似乎都涉及创建控件,无论您是否真的需要它,然后在您不需要它的平台上将 IsVisible 设置为 false。 IMO 更好的解决方案是仅在您确实需要时才首先创建控件。第一步是将其包装在内容视图中:
<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>
奈杰尔
对于偶然发现此问题并寻求代码隐藏解决方案的任何人:
switch (Device.RuntimePlatform)
{
case Device.iOS:
//iOS specific code here
break;
case Device.Android:
//Android specific code here
break;
}
设备 class 具有以下设备常量:
Constants as shown from VS 2019 Intellisense.