ImageButton 状态不重置为正常

ImageButton state don't reset to Normal

遇到这样的问题,如果你同时使用ImageButtonVisualStateManager点击它,然后将手指滑出控件并松开,则状态不会重置为Normal .

有谁知道如何解决这个问题?

这是 ImageButton 样式的示例:

<Style x:Key="ProductButton" TargetType="ImageButton">
<Setter Property="HeightRequest" Value="70"/>
<Setter Property="WidthRequest" Value="210"/>
<Setter Property="Aspect" Value="Fill"/>
<Setter Property="CornerRadius" Value="4"/>
<Setter Property="VisualStateManager.VisualStateGroups">
    <VisualStateGroupList>
        <VisualStateGroup x:Name="CommonStates">
            <VisualState x:Name="Normal">
                <VisualState.Setters>
                    <Setter Property="Opacity" Value="1"/>
                </VisualState.Setters>
            </VisualState>
            <VisualState x:Name="Pressed">
                <VisualState.Setters>
                    <Setter Property="Opacity" Value="0.8"/>
                </VisualState.Setters>
            </VisualState>
        </VisualStateGroup>
    </VisualStateGroupList>
</Setter>

我找到了一种解决方法,它使用平台效果,当在 Imagebutton 外部发生触摸时触发释放事件。

在 .NET Standard 库中创建效果(共享代码)。

public class SpecialButtonEffect : RoutingEffect
{
    public SpecialButtonEffect() : base($"MyCompany.{nameof(SpecialButtonEffect)}")
    {
    }
}

在 ios 中实现 SpecialButtonEffect:

[assembly: ResolutionGroupName("MyCompany")]
[assembly: ExportEffect(typeof(EffectTest.iOS.Effects.SpecialButtonEffect), 
nameof(EffectTest.iOS.Effects.SpecialButtonEffect))]
namespace EffectTest.iOS.Effects
{
public class SpecialButtonEffect : PlatformEffect
{
    protected override void OnAttached()
    {
        (Control as UIButton).TouchUpOutside += OnTouchUpOutside;
    }

    protected override void OnDetached()
    {
        (Control as UIButton).TouchUpOutside -= OnTouchUpOutside;
    }

    private void OnTouchUpOutside(object sender, EventArgs e)
    {
        (Element as IButtonController).SendReleased();
    }
}
}

消耗 xaml 中的效果。

<ImageButton
            HorizontalOptions="Center"
           
            Source="favorite.png"
            VerticalOptions="CenterAndExpand">
            <ImageButton.Effects>
                <local:SpecialButtonEffect  />
            </ImageButton.Effects>
            <VisualStateManager.VisualStateGroups>
                <VisualStateGroup x:Name="CommonStates">
                    <VisualState x:Name="Normal">
                        <VisualState.Setters>
                            <Setter Property="Scale" Value="1" />
                        </VisualState.Setters>
                    </VisualState>

                    <VisualState x:Name="Pressed">
                        <VisualState.Setters>
                            <Setter Property="Scale" Value="0.5" />
                        </VisualState.Setters>
                    </VisualState>
                    
                </VisualStateGroup>
            </VisualStateManager.VisualStateGroups>
        </ImageButton>