【问题标题】:How to only set OnPlatform for a specific platform?如何仅为特定平台设置 OnPlatform?
【发布时间】:2020-02-05 09:59:22
【问题描述】:

我正在使用 Xamarin.Forms 进行编程。我想知道如何在 XAML 中使用OnPlatform 仅设置单个平台的高度请求?如果不引用平台,我想保留默认值-1。

我目前有以下代码:

<ListView.HeightRequest>
    <OnPlatform x:TypeArguments="x:Double">
        <On Platform="WPF" Value="153" />
        <On Platform="Android" Value="-1" />
        <On Platform="iOS" Value="-1" />
        <On Platform="UWP" Value="-1" />
    </OnPlatform>
</ListView.HeightRequest>

我希望我只需要写这样的东西:

<ListView.HeightRequest>
    <OnPlatform x:TypeArguments="x:Double">
        <On Platform="WPF" Value="153" />
    </OnPlatform>
</ListView.HeightRequest>

这不行,有可能吗?

【问题讨论】:

    标签: xaml xamarin xamarin.forms


    【解决方案1】:

    您可以在一行中编写具有相同值的平台:

    <ListView.HeightRequest>
        <OnPlatform x:TypeArguments="x:Double">
            <On Platform="WPF" Value="153" />
            <On Platform="Android,iOS,UWP" Value="-1" />
        </OnPlatform>
    </ListView.HeightRequest>
    

    参考:Providing platform-specific values

    【讨论】:

      【解决方案2】:

      这在 xaml.cs 中会更容易实现

      Xaml

      <ListView x:Name="myListView".....
      

      Xaml.cs

      if(Device.RuntimePlatform ==Device.WPF)
      myListView.HeightRequest=153;
      

      【讨论】:

        【解决方案3】:

        有一个 Default 平台值,它应该完全符合这个目的(虽然没有尝试过)

        <ListView.HeightRequest>
            <OnPlatform x:TypeArguments="x:Double">
                <On Platform="WPF" Value="153" />
                <On Platform="Default" Value="-1" />
            </OnPlatform>
        </ListView.HeightRequest>
        

        【讨论】:

        • 你测试了吗?它不适用于最新版本的 XF。
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2012-03-25
        • 1970-01-01
        • 2012-02-18
        • 1970-01-01
        • 1970-01-01
        • 2011-01-21
        • 1970-01-01
        相关资源
        最近更新 更多