Я новичок в Xamarin и хотел попробовать изменить внешний вид переключателя. Но с моими текущими кодами я получаю сообщение об ошибке Cannot resolve property type "TextColor" on type "Grid".
Вот мои коды:
<ContentPage.Resources>
<ControlTemplate x:Key = "RadioButtonTemplate">
<Grid RowSpacing = "0">
<VisualStateManager.VisualStateGroups>
<VisualStateGroupList>
<VisualStateGroup x:Name = "CheckedStates">
<VisualState x:Name = "Checked">
<VisualState.Setters>
<Setter TargetName = "checkBorderTop"
Property = "BackgroundColor"
Value = "{x:StaticResource PrimaryColor}"/>
<Setter TargetName = "checkBorderBot"
Property = "BackgroundColor"
Value = "{x:StaticResource PrimaryColor}"/>
<Setter TargetName = "check"
Property = "BackgroundColor"
Value = "{x:StaticResource PrimaryShade}"/>
<Setter TargetName = "checkIcon"
Property = "TextColor"
Value = "{x:StaticResource PrimaryColor}"/>
</VisualState.Setters>
</VisualState>
<VisualState x:Name = "Unchecked">
<VisualState.Setters>
<Setter TargetName = "checkBorderTop"
Property = "BackgroundColor"
Value = "Transparent"/>
<Setter TargetName = "checkBorderBot"
Property = "BackgroundColor"
Value = "Transparent"/>
<Setter TargetName = "check"
Property = "BackgroundColor"
Value = "Transparent"/>
<Setter TargetName = "checkIcon"
Property = "TextColor"
Value = "Black"/>
</VisualState.Setters>
</VisualState>
</VisualStateGroup>
</VisualStateGroupList>
</VisualStateManager.VisualStateGroups>
<Grid.RowDefinitions>
<RowDefinition Height = "2"/>
<RowDefinition Height = "auto"/>
<RowDefinition Height = "2"/>
</Grid.RowDefinitions>
<BoxView x:Name = "checkBorderTop"
Grid.Row = "0"/>
<Frame x:Name = "check"
BorderColor = "Transparent"
Margin = "0"
Padding = "20, 10"
Grid.Row = "1">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width = "20"/>
<ColumnDefinition Width = "auto"/>
</Grid.ColumnDefinitions>
<Label x:Name = "checkIcon"
Text = "{x:Static icon:FontAwesomeIcons.Paw}"
FontFamily = "IconSolid"
HorizontalOptions = "Start"
FontSize = "20"
VerticalTextAlignment = "Center"/>
<ContentPresenter Grid.Column = "1"/>
</Grid>
</Frame>
<BoxView x:Name = "checkBorderBot"
Grid.Row = "2"
Margin = "0"/>
</Grid>
</ControlTemplate>
<Style TargetType = "RadioButton">
<Setter Property = "ControlTemplate"
Value = "{StaticResource RadioButtonTemplate}" />
</Style>
</ContentPage.Resources>
Вот мой ожидаемый результат:
Код хорошо работает при изменении цвета фона и границы. Но когда я добавляю
<Setter TargetName = "checkIcon"
Property = "TextColor"
Value = "{x:StaticResource PrimaryColor}"/>
ошибка показывает. Мне не удается изменить цвет значка лапы, когда он отмечен и не отмечен. Я думаю, что это иерархия между родительскими и дочерними элементами, поскольку метка находится на самом внутреннем дочернем элементе, idk.
Нужна помощь знающих ответ :(
заранее спасибо





Вы можете проверить Диспетчер визуальных состояний.
Часто необходимо использовать одну и ту же разметку Visual State Manager для двух или более представлений.
То есть вы можете использовать Style для элементов Grid на странице просмотра:
<Style TargetType = "Grid">
<Setter Property = "RowSpacing" Value = "0" />
<Setter Property = "VisualStateManager.VisualStateGroups">
<VisualStateGroupList>
<VisualStateGroup x:Name = "CheckedStates">
<VisualState x:Name = "Checked">
<VisualState.Setters>
<Setter TargetName = "checkBorderTop"
Property = "BackgroundColor"
Value = "{x:StaticResource PrimaryColor}"/>
<Setter TargetName = "checkBorderBot"
Property = "BackgroundColor"
Value = "{x:StaticResource PrimaryColor}"/>
<Setter TargetName = "check"
Property = "BackgroundColor"
Value = "{x:StaticResource PrimaryShade}"/>
<Setter TargetName = "checkIcon"
Property = "TextColor"
Value = "{x:StaticResource PrimaryColor}"/>
</VisualState.Setters>
</VisualState>
<VisualState x:Name = "Unchecked">
<VisualState.Setters>
<Setter TargetName = "checkBorderTop"
Property = "BackgroundColor"
Value = "Transparent"/>
<Setter TargetName = "checkBorderBot"
Property = "BackgroundColor"
Value = "Transparent"/>
<Setter TargetName = "check"
Property = "BackgroundColor"
Value = "Transparent"/>
<Setter TargetName = "checkIcon"
Property = "TextColor"
Value = "Black"/>
</VisualState.Setters>
</VisualState>
</VisualStateGroup>
</VisualStateGroupList>
</Setter>
</Style>
Хотелось бы, чтобы это было полезно для вас.
Вы решили проблему?