Xamarin 切换控件的主题
Xamarin switching control's theme
我想做的只是在单击按钮时使用 c# 更改按钮的主题。
我在xaml中有什么:
BackgroundColor="{x:AppThemeBinding Light={StaticResource Body_Default_Background}, Dark={StaticResource Body_Default_Background_Dark}}"
我想用c#设置App主题绑定
我试过了:
button.BackgroundColor = "{x:AppThemeBinding Light={StaticResource Body_Default_Background}, Dark={StaticResource Body_Default_Background_Dark}}"
change the button's theme when the button is clicked
您可以使用 VisualStateManager
或 EventTrigger
简单地更改按钮的 BackgroundColor
。
您可以参考以下代码:
<Button Text="Click Me!">
<VisualStateManager.VisualStateGroups>
<VisualStateGroup x:Name="CommonStates">
<VisualState x:Name="Normal">
<VisualState.Setters>
<Setter Property="BackgroundColor" Value="Green" />
</VisualState.Setters>
</VisualState>
<VisualState x:Name="Pressed">
<VisualState.Setters>
<Setter Property="BackgroundColor" Value="Red" />
</VisualState.Setters>
</VisualState>
</VisualStateGroup>
</VisualStateManager.VisualStateGroups>
</Button>
或使用EventTrigger实现:
创建ButtonTriggerAction.cs
public class ButtonTriggerAction : TriggerAction<VisualElement>
{
public Color BackgroundColor { get; set; }
protected override void Invoke(VisualElement visual)
{
var button = visual as Button;
if (button == null) return;
if (BackgroundColor != null) button.BackgroundColor = BackgroundColor;
}
}
和用法:
<Button Text="using EventTrigger">
<Button.Triggers>
<EventTrigger Event="Pressed">
<local:ButtonTriggerAction BackgroundColor="Red" />
</EventTrigger>
<EventTrigger Event="Released">
<local:ButtonTriggerAction BackgroundColor="Default" />
</EventTrigger>
</Button.Triggers>
</Button>
我想做的只是在单击按钮时使用 c# 更改按钮的主题。
我在xaml中有什么:
BackgroundColor="{x:AppThemeBinding Light={StaticResource Body_Default_Background}, Dark={StaticResource Body_Default_Background_Dark}}"
我想用c#设置App主题绑定
我试过了:
button.BackgroundColor = "{x:AppThemeBinding Light={StaticResource Body_Default_Background}, Dark={StaticResource Body_Default_Background_Dark}}"
change the button's theme when the button is clicked
您可以使用 VisualStateManager
或 EventTrigger
简单地更改按钮的 BackgroundColor
。
您可以参考以下代码:
<Button Text="Click Me!">
<VisualStateManager.VisualStateGroups>
<VisualStateGroup x:Name="CommonStates">
<VisualState x:Name="Normal">
<VisualState.Setters>
<Setter Property="BackgroundColor" Value="Green" />
</VisualState.Setters>
</VisualState>
<VisualState x:Name="Pressed">
<VisualState.Setters>
<Setter Property="BackgroundColor" Value="Red" />
</VisualState.Setters>
</VisualState>
</VisualStateGroup>
</VisualStateManager.VisualStateGroups>
</Button>
或使用EventTrigger实现:
创建ButtonTriggerAction.cs
public class ButtonTriggerAction : TriggerAction<VisualElement>
{
public Color BackgroundColor { get; set; }
protected override void Invoke(VisualElement visual)
{
var button = visual as Button;
if (button == null) return;
if (BackgroundColor != null) button.BackgroundColor = BackgroundColor;
}
}
和用法:
<Button Text="using EventTrigger">
<Button.Triggers>
<EventTrigger Event="Pressed">
<local:ButtonTriggerAction BackgroundColor="Red" />
</EventTrigger>
<EventTrigger Event="Released">
<local:ButtonTriggerAction BackgroundColor="Default" />
</EventTrigger>
</Button.Triggers>
</Button>