【问题标题】:Xamarin forms View difference in android & iOSXamarin 表单在 android 和 iOS 中查看差异
【发布时间】:2021-11-03 19:15:51
【问题描述】:

android 和 iOS 中的视图存在差异。请帮助我了解这是否是默认平台行为?

代码:

<Grid>
<Grid.RowDefinitions>
<RowDefinition
  Height="Auto" />
<RowDefinition
  Height="Auto" />
<RowDefinition
  Height="Auto" />  
</Grid.RowDefinitions>  
<Label grid.row="0" Text={Binding FirstName}/>
<Label grid.row="1" Text={Binding LastName}/>
<Label grid.row="2" Text={Binding Email}/>                                                      
</Grid>

如果LastName在android中为空,它会在下面的2号网格中留下空白

而在 iOS 中,如果 LastName 为空,则 UI 调整如下

感谢任何帮助!

【问题讨论】:

  • 平台是这样设计的。在Android上,如果你的文本为空,它似乎仍然保持位置,而在ios上,如果文本为空,其他视图从零开始计算位置。您可以在每个平台上进行测试。

标签: xaml xamarin.forms xamarin.android xamarin.ios


【解决方案1】:

在这种情况下尝试使用 StackLayout。根据我的经验,如果标签为空,它会自动调整大小。

【讨论】:

    【解决方案2】:

    无论变量中的内容如何,​​它都会留下空间。尝试使用“非空转换器”来解决您的问题。

    public class IsNotNullConverter : IValueConverter
    {
            public object Convert(object? value, Type? targetType, object? parameter, CultureInfo? culture) => !ConvertInternal(value);
    
            internal static bool ConvertInternal(object? value) =>
            value == null || (value is string str && string.IsNullOrWhiteSpace(str));
    
            public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
            {
                throw new NotSupportedException();
            }
    }
    

    如何在您的代码中使用它: &lt;Label grid.row="1" Text={Binding LastName} IsVisibile="{Binding LastName, Converter="{x:StaticResource NotNullConverter}"}"/&gt;

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-12-11
      • 1970-01-01
      相关资源
      最近更新 更多