Xamarin.forms: [iOS,UWP] Button Released event not fired if Release occurs outside of Button's Layout

Created on 15 Jul 2018  路  10Comments  路  Source: xamarin/Xamarin.Forms

Description

If you attach a Release event to a button in Xamarin Forms you'll notice that it won't get fired when running on iOS if you press the button, maintain your press and move your finger off of the button and then release.

Steps to Reproduce

  1. In any XF project, add a button to your XAML with x:Name="Btn"
  2. In code-behind, add:
    Btn.Pressed += (sender, e) => { Btn.BackgroundColor = Color.LightSeaGreen; };
    Btn.Released+= (sender, e) => { Btn.BackgroundColor = Color.Orange; };
  3. Run on iOS simulator/device and press the button, maintain your press and move your finger off of the button and then release

Expected Behavior

Released event would fire and change the button background color to Orange

Actual Behavior

Released event does NOT fire and button background color remains LightSeaGreen that was set by the Pressed event.

Basic Information

  • Version with issue: latest
  • Last known good version: n/a
  • IDE: VS2017 Enterprise latest
  • Platform Target Frameworks:

    • iOS: 11.4

      -UWP

  • Nuget Packages: irrelevant
  • Affected Devices: all

iOSButtonRelease.zip

4 UWP iOS 馃崕 bug

Most helpful comment

There should be a property in the event args that allows us to know whether the release occured inside or outside the button. That's important.

All 10 comments

It looks like only Android fires the release event

I was browsing the iOS code and saw that the Released event is only fired from OnButtonTouchUpInside.

There should be a property in the event args that allows us to know whether the release occured inside or outside the button. That's important.

Hello, any news on this bugfix or there is some workaround?
Thanks

I agree with @SuperCorks , then dev can decide whether to ignore and not fire release event if release occurred outside of button bounds.

Hey all,
Altough @PureWeen had reported that the event DOES fire on android, I would like to report that I am using the latest nuget version of XamarinForms and the event does NOT fire on android.
steps to reproduce are the same as the OP of the issue suggested.
thanks in advance.

Edit:
A (not the best) workaround that I found for now is to have a boolean field that states if the button was not realeased properly, as following:
...
private bool WORKAROUND_BtnReleasedProblem;
...
private void Button_Released(object sender, EventArgs e) { WORKAROUND_BtnReleasedProblem = false; ViewBackgroundColor = Color.Transparent; WhiteBackground = !WhiteBackground; SetColor(); }

private void Button_Pressed(object sender, EventArgs e) { if (WORKAROUND_BtnReleasedProblem) Button_Released(sender, null); ViewBackgroundColor = BorderColor; WhiteBackground = !WhiteBackground; SetColor(); WORKAROUND_BtnReleasedProblem = true; }

thus when pressed again, the button would return to the previous state of 'released'

Hi, just to mention that as an iOS workaround, I wrote a platform effect that triggers the released event when the touch up occurs outside the button bounds.

using System;
using UIKit;
using Xamarin.Forms;
using Xamarin.Forms.Platform.iOS;

[assembly: ResolutionGroupName("Effects")]
[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();
        }
    }
}

I experience this problem on Android. Forms 4.5.

Thanks for the suggestion @Peseur. I bumped into a similar issue with the ImageButton on iOS, reported in #10859, then discovered that the same happens with regular Buttons on iOS too, so ended up here. Your solution inspired me to write my own, which will not just send the Released-event, but will also fix reverting the visual state to normal (which was my original issue.

Same problem on Android (XF 3.6) : Released event not triggered when releasing the button outside.

With a LongPressBehavior :

` public class LongPressBehavior : Behavior

Was this page helpful?
0 / 5 - 0 ratings