官术网_书友最值得收藏!

Visual states

Visual states and VSM will be familiar concepts for WPF and UWP developers; however, they were missing from Xamarin.Forms' runtime until recently. Visual states define various conditions that a control can be rendered according to certain conditions. For instance, an Entry element can be in Normal, Focused, or Disabled states and each state defines a different visual setter for the element. Additionally, custom states can also be defined for a visual element and, depending on triggers or explicit calls to VisualStateManager, can manage the visual state of elements.

In order to demonstrate this, we can create three different states for our label (for example, Released, UnReleased, and Unknown) and deal with states using our triggers.

First, we need to define states for our label control (which can then be moved to a resource dictionary as part of a style):

 <Label x:Name="ReleaseDate" ...>
<Label.Triggers>
<!-- Removed for Brevity -->
</Label.Triggers>
<VisualStateManager.VisualStateGroups>
<VisualStateGroup x:Name="CommonStates">
<VisualState x:Name="Released">
<VisualState.Setters>
<Setter
Property="BackgroundColor"
Value="Lime" />
<Setter
Property="TextColor"
Value="Black" />
</VisualState.Setters>
</VisualState>
<VisualState x:Name="UnReleased">
<VisualState.Setters>
<Setter Property="TextColor" Value="Black" />
</VisualState.Setters>
</VisualState>
<VisualState x:Name="Unknown">
<VisualState.Setters>
<Setter Property="TextColor" Value="Red" />
</VisualState.Setters>
</VisualState>
</VisualStateGroup>
</VisualStateManager.VisualStateGroups>
</Label>

As you can see, one of the defined states is Unknown, and it should set the text color to red. In order to change the state of the label using a trigger, we can implement a trigger action:

 public class ChangeStateAction : TriggerAction<VisualElement>
{
public ChangeStateAction() { }

public string State { set; get; }

protected override void Invoke(VisualElement visual)
{
if(visual.HasVisualStateGroups())
{
VisualStateManager.GoToState(visual, State);
}
}
}

And we can use this action as our EnterAction for the previously defined multi-trigger:

<MultiTrigger TargetType="Label">
<MultiTrigger.Conditions>
<!-- Removed for brevity -->
</MultiTrigger.Conditions>
<MultiTrigger.EnterActions>
<actions:ChangeStateAction State="Unknown" />
</MultiTrigger.EnterActions>
</MultiTrigger>

We can achieve the same result as using setters. However, it is important to mention that, without defining an ExitAction once the label is set to the given state, it will not revert to the previous state.

主站蜘蛛池模板: 灵寿县| 长春市| 新安县| 正镶白旗| 红河县| 宁晋县| 清苑县| 宁陵县| 沙坪坝区| 澎湖县| 常州市| 福州市| 蓬莱市| 项城市| 云安县| 马关县| 西昌市| 通江县| 白玉县| 诸城市| 尚义县| 蒲江县| 凯里市| 泸州市| 宁化县| 巴林右旗| 宝清县| 互助| 永靖县| 南充市| 木兰县| 黄梅县| 神池县| 庄浪县| 淄博市| 海安县| 呼玛县| 盐津县| 洪洞县| 会泽县| 乌拉特后旗|