【问题标题】:Xamarin Forms Hide a Button on iOS and Show it on AndroidXamarin Forms 在 iOS 上隐藏按钮并在 Android 上显示
【发布时间】:2017-07-03 21:58:15
【问题描述】:

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

谢谢

【问题讨论】:

  • 但应用程序崩溃:在 xmlns 中找不到类型 IsVisible 这就是为什么我需要知道我有什么把 x:TypeArguments
  • 嗨@Mireille,看看我的答案,它应该对你有帮助:) 如果有,请将其标记为答案,以便对其他人有所帮助! :)
  • 只是为了澄清:“IsVisible”是属性名称。您应该分配给 x:TypeArguments 的是属性的实际类型,它是一个 bool(标准内容页面中的 x:Boolean)

标签: xamarin xamarin.forms visibility


【解决方案1】:

如果你想在 XAML 上做,为了在特定平台上隐藏视图,你可以使用这个:

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

希望对你有帮助!

【讨论】:

  • 感谢您的回复,对我帮助很大
  • 很高兴我能帮上忙!但是,看起来您正在通过代码隐藏寻找答案。
【解决方案2】:
// IOS, Android, WP
SomeButton.IsVisible = Device.OnPlatform<bool>(false, true, true);

或者

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

【讨论】:

    【解决方案3】:

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

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

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

    【讨论】:

      【解决方案4】:

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

      【讨论】:

        【解决方案5】:

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

        奈杰尔

        【讨论】:

          【解决方案6】:

          对于偶然发现此问题并寻求代码隐藏解决方案的任何人:

                      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.

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2020-12-21
            • 1970-01-01
            • 2012-07-26
            • 1970-01-01
            • 2018-03-16
            相关资源
            最近更新 更多