需求是:一个button,四种状态
如有更好实现,烦劳转告。
以下实现或许有些伪,用了Pressed="btnclicked"
而不是Clicked="btnclicked",但是效果还是可以的。
用图如下,动图脑补:
主要代码一:
<ImageButton x:Name="testIMBTN" WidthRequest="50" HeightRequest="50" Pressed="btnclicked" Source="" BackgroundColor="Transparent"
VerticalOptions="CenterAndExpand" HorizontalOptions="CenterAndExpand" >
<VisualStateManager.VisualStateGroups>
<VisualStateGroup x:Name="StateGroup">
<VisualState x:Name="Normal">
<VisualState.Setters>
<Setter Property="Scale" Value="1" />
<Setter Property="Source" Value="{local:ImageResource xxxxelapse.Shared.images.pause.png}" />
</VisualState.Setters>
</VisualState>
<VisualState x:Name="Pressed">
<VisualState.Setters>
<Setter Property="Scale" Value="1" />
<Setter Property="Source" Value="{local:ImageResource xxxxxelapse.Shared.images.pause_pressed.png}" />
</VisualState.Setters>
</VisualState>
</VisualStateGroup>
</VisualStateManager.VisualStateGroups>
</ImageButton>
主要代碼二:
int flag = 0;
private async void btnclicked(object sender, EventArgs e)
{
var visualStateGroups = VisualStateManager.GetVisualStateGroups(testIMBTN);
foreach (VisualStateGroup vsg in visualStateGroups)
{
foreach (VisualState vs in vsg.States)
{
if (vs.Name == "Normal")
{
foreach(Setter setter in vs.Setters)
{
if(setter.Property.PropertyName == "Source")
{
if(flag == 0)
setter.Value = ImageSource.FromResource("xxxelapse.Shared.images.play.png", typeof(Page2).Assembly);
else
setter.Value = ImageSource.FromResource("xxxelapse.Shared.images.pause.png", typeof(Page2).Assembly);
}
}
}
if (vs.Name == "Pressed")
{
foreach (Setter setter in vs.Setters)
{
if (setter.Property.PropertyName == "Source")
{
if (flag == 0)
setter.Value = ImageSource.FromResource("xxxlapse.Shared.images.play_pressed.png", typeof(Page2).Assembly);
else
setter.Value = ImageSource.FromResource("xxxelapse.Shared.images.pause_pressed.png", typeof(Page2).Assembly);
}
}
}
}
}
if (flag == 0)
flag = 1;
else
flag = 0;
}
网友评论