【问题标题】:if it's an android place a button or if it's iOS place another button如果是 android 放置一个按钮,或者如果它是 iOS 放置另一个按钮
【发布时间】:2017-10-31 14:53:14
【问题描述】:

有人知道如何在 XAML 代码中使用 xamarin 表单,¿如果它是 android 放置一个按钮,或者如果它是 iOS 放置另一个按钮?

这是我的代码

<ContentPage.Content>


    <ListView x:Name="ListElement" SeparatorColor="Red"
              Header=""
              HasUnevenRows="True">


        <ListView.HeaderTemplate>
            <DataTemplate>
              **<!-- if is android** 
                <Grid Padding="20" 
                        BackgroundColor="#DE6B51">
                    <Label Text="Bandeja de Entrada" TextColor="White"
                            HorizontalOptions="Center" FontSize="Large" />
                </Grid>
              **-->**
            </DataTemplate>
        </ListView.HeaderTemplate>


    </ListView>
</ContentPage.Content>

【问题讨论】:

    标签: android ios xamarin cross-platform


    【解决方案1】:

    可能不够干净,在 XAML 中我会创建 2 个按钮并为它们添加一个属性,第一个:

    IsVisible="{Binding IsAndroid}" 
    

    第二个:

    IsVisible="{Binding IsAndroid, Converter={StaticResource not}}"
    

    IsVisible="{Binding IsIOS}"
    

    如果您不想使用转换器,只需在模型中创建两个道具

        private bool _IsAndroid;
        public bool IsAndroid
        {
            get { return Device.RuntimePlatform == Device.Android; }
        }
    
        private bool _IsIOS;
        public bool IsIOS
        {
            get { return Device.RuntimePlatform == Device.iOS; }
        }
    

    【讨论】:

      【解决方案2】:

      如果是 android 放置一个按钮,或者如果是 iOS 放置另一个按钮?

      是的,这是可能的。在 XAML 中,您可以像这样编写代码:

      <OnPlatform x:TypeArguments="View">
          <OnPlatform.Android>
              <!--view for Android-->
          </OnPlatform.Android>
          <OnPlatform.iOS>
              <!--view for iOS-->
          </OnPlatform.iOS>
      </OnPlatform>
      

      或者我建议你阅读这个文档:Mobile - Embedding Native Views in Your Xamarin.Forms Apps,它讲述了为不同平台嵌入不同视图的几种方法。

      【讨论】:

      • 我尝试了那个解决方案,但是如果你注意到我使用了“”,那么如果你添加这些行它就不起作用了。
      • @Ceregol,您想添加不同的视图作为ContentPage 的内容吗?它应该可以工作,你能告诉我你的代码吗?
      猜你喜欢
      • 2018-05-05
      • 1970-01-01
      • 1970-01-01
      • 2016-09-30
      • 2013-05-09
      • 2018-12-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多