2 回答

TA貢獻(xiàn)1848條經(jīng)驗(yàn) 獲得超2個(gè)贊
不幸的是。從Xamarin.Forms - Xaminals示例來看,也出現(xiàn)了這種現(xiàn)象。這應(yīng)該是當(dāng)前版本的 XF 中 Shell FlyoutItem 的限制。
<Shell.ItemTemplate>
? ? <DataTemplate >
? ? ? ? <Grid>
? ? ? ? ? ? <Grid.ColumnDefinitions>
? ? ? ? ? ? ? ? <ColumnDefinition Width="0.2*" />
? ? ? ? ? ? ? ? <ColumnDefinition Width="0.8*" />
? ? ? ? ? ? </Grid.ColumnDefinitions>
? ? ? ? ? ? <Image Source="{Binding FlyoutIcon}"
? ? ? ? ? ? ? ? Margin="5"
? ? ? ? ? ? ? ? HeightRequest="45" />
? ? ? ? ? ? <Label Grid.Column="1"
? ? ? ? ? ? ? ? Text="{Binding Title}"
? ? ? ? ? ? ? ? FontAttributes="Italic"
? ? ? ? ? ? ? ? VerticalTextAlignment="Center" />
? ? ? ? </Grid>
? ? </DataTemplate>
</Shell.ItemTemplate>
如果不使用Shell.ItemTemplate,則 selectitem 被標(biāo)記:
否則 selectitem 未標(biāo)記:
=====================================更新============== =================
解決方案:
如果給模板添加樣式,選擇后可以保持選中狀態(tài)。
Shell.Resources:添加FoutItemStyle。
<Style x:Key="FloutItemStyle" TargetType="Grid">
? ? <Setter Property="VisualStateManager.VisualStateGroups">
? ? ? ? <VisualStateGroupList>
? ? ? ? ? ? <VisualStateGroup x:Name="CommonStates">
? ? ? ? ? ? ? ? <VisualState x:Name="Normal" />
? ? ? ? ? ? ? ? <VisualState x:Name="Selected">
? ? ? ? ? ? ? ? ? ? <VisualState.Setters>
? ? ? ? ? ? ? ? ? ? ? ? <Setter Property="BackgroundColor" Value="Accent"/>
? ? ? ? ? ? ? ? ? ? </VisualState.Setters>
? ? ? ? ? ? ? ? </VisualState>
? ? ? ? ? ? </VisualStateGroup>
? ? ? ? </VisualStateGroupList>
? ? </Setter>
</Style>
在Shell.ItemTemplate中使用如下:
<Shell.ItemTemplate>
? ? <DataTemplate >
? ? ? ? <Grid Style="{StaticResource FloutItemStyle}">
? ? ? ? ? ? <Grid.ColumnDefinitions>
? ? ? ? ? ? ? ? <ColumnDefinition Width="0.2*" />
? ? ? ? ? ? ? ? <ColumnDefinition Width="0.8*" />
? ? ? ? ? ? </Grid.ColumnDefinitions>
? ? ? ? ? ? <Image Source="{Binding FlyoutIcon}"
? ? ? ? Margin="5"
? ? ? ? HeightRequest="45" />
? ? ? ? ? ? <Label Grid.Column="1"
? ? ? ? Text="{Binding Title}"
? ? ? ? FontAttributes="Italic"
? ? ? ? VerticalTextAlignment="Center" />
? ? ? ? </Grid>
? ? </DataTemplate>
</Shell.ItemTemplate>
最后展示一下效果:

TA貢獻(xiàn)1876條經(jīng)驗(yàn) 獲得超7個(gè)贊
您可以使用綁定屬性。創(chuàng)建自定義網(wǎng)格
public class ShellItemGrid : Grid
{
public static readonly BindableProperty SelectedColorProperty = BindableProperty.Create("SelectedColor", typeof(Color), typeof(ShellItemGrid),Color.Transparent);
public Color SelectedColor
{
get { return (Color)GetValue(SelectedColorProperty); }
set { SetValue(SelectedColorProperty, value); }
}
}
定義網(wǎng)格的樣式
<Shell.Resources>
<Style x:Key="FlyoutItemStyle" TargetType="controls:ShellItemGrid">
<Setter Property="VisualStateManager.VisualStateGroups">
<VisualStateGroupList>
<VisualStateGroup x:Name="CommonStates">
<VisualState x:Name="Selected">
<VisualState.Setters>
<Setter Property="SelectedColor" Value="Red"/>
</VisualState.Setters>
</VisualState>
<VisualState x:Name="Normal">
<VisualState.Setters>
<Setter Property="SelectedColor" Value="White"/>
</VisualState.Setters>
</VisualState>
</VisualStateGroup>
</VisualStateGroupList>
</Setter>
</Style>
定義項(xiàng)目模板并將Label的TextColor綁定到網(wǎng)格的SelectedColor
<Shell.ItemTemplate>
<DataTemplate>
<controls:ShellItemGrid x:Name="mGrid" Style="{StaticResource FlyoutItemStyle}" >
<Label HorizontalTextAlignment="Start"
VerticalTextAlignment="Center"
Margin="20,10,0,10"
Text="{Binding Title}"
TextColor="{Binding Source={x:Reference mGrid},Path=SelectedColor}"
FontSize="18" />
</controls:ShellItemGrid >
</DataTemplate>
</Shell.ItemTemplate>
- 2 回答
- 0 關(guān)注
- 147 瀏覽
添加回答
舉報(bào)