我有这个XAML,它使用自定义模板3次,然后再对ViewCell,Grid等进行硬编码:
<ContentPage.Content>
<TableView Intent="Settings" HasUnevenRows="false" RowHeight="50" x:Name="atiSection">
<TableView.Root>
<TableSection Title="Set the amount of time that the answer will remain visible on the Play screen">
<template:SwitchViewCellTemplate Text="{Binding [0].Name}"
IsVisible="{Binding [0].IsSelected}"
SelectAction="Handle_SelectAction" />
<template:SwitchViewCellTemplate Text="{Binding [1].Name}"
IsVisible="{Binding [1].IsSelected}"
SelectAction="Handle_SelectAction" />
<template:SwitchViewCellTemplate Text="{Binding [2].Name}"
IsVisible="{Binding [2].IsSelected}"
SelectAction="Handle_SelectAction" />
<ViewCell Tapped="selectValue" >
<Grid VerticalOptions="CenterAndExpand" Padding="20,0" >
<local:StyledLabel Text="{Binding [3].Name}" HorizontalOptions="StartAndExpand" />
<local:StyledLabel IsVisible="{Binding [3].IsSelected}" TextColor="Gray" HorizontalOptions="End" Text="✓" />
</Grid>
</ViewCell>
为了使内容更整洁,我创建了一个模板,该模板在此示例中使用了三次:
<?xml version="1.0" encoding="utf-8" ?>
<ViewCell xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:local="clr-namespace:Japanese;assembly=Japanese"
x:Class="Japanese.SwitchViewCellTemplate"
x:Name="this" >
<Grid VerticalOptions="CenterAndExpand" Padding="20,0" >
<local:StyledLabel Text="{Binding Text, Source={x:Reference this}}" HorizontalOptions="StartAndExpand" />
<local:StyledLabel IsVisible="{Binding IsVisible, Source={x:Reference this}}" TextColor="Gray" HorizontalOptions="End" Text="✓" />
</Grid>
</ViewCell>
这是后端CS代码初始化:
SSVViewModel[] AnswerTimeInterval = new[] {
new SSVViewModel{ Id = 0, Name = ATI.Two.Text(), IsSelected = false} ,
new SSVViewModel{ Id = 1, Name = ATI.Three.Text(), IsSelected = false} ,
new SSVViewModel{ Id = 2, Name = ATI.Five.Text(), IsSelected = false} ,
new SSVViewModel{ Id = 3, Name = ATI.Ten.Text(), IsSelected = false} ,
new SSVViewModel{ Id = 4, Name = ATI.Fifteen.Text(), IsSelected = false} ,
new SSVViewModel{ Id = 5, Name = ATI.Thirty.Text(), IsSelected = false} ,
new SSVViewModel{ Id = 6, Name = ATI.Sixty.Text(), IsSelected = false} ,
new SSVViewModel{ Id = 7, Name = ATI.ThreeHundred.Text(), IsSelected = false} ,
new SSVViewModel{ Id = 8, Name = ATI.SixHundred.Text(), IsSelected = false} ,
};
public ATIPage()
{
InitializeComponent();
atiSection.BindingContext = AnswerTimeInterval;
}
页面首次显示时如下所示:
在我单击三个单元格中的第一个后,它变为:
有人有什么想法,为什么行中的文本最初显示时几乎没有高度,然后单击视图单元格时为什么会变大?
答案 0 :(得分:2)
AndExpand LayoutOptions
are only for use by a StackLayout
,尽管我不确定这是否是问题的根本原因。将模板网格更改为以下似乎可以解决该问题:
<Grid Padding="20,0">
<Label Text="{Binding Text, Source={x:Reference this}}"
VerticalTextAlignment="Center"/>
<Label HorizontalOptions="End"
IsVisible="{Binding IsVisible, Source={x:Reference this}}"
Text="✓"
TextColor="Gray"
VerticalTextAlignment="Center" />
</Grid>
Xamarin.Forms创作者Jason Smith的Performance tips也提到以下内容:
优选
VerticalTextAlignment
和HorizontalTextAlignment
Label
和VerticalOptions
上HorizontalOptions
的属性。