Xamarin.forms: PlatformEffects does not work with Frame on Android

Created on 12 Aug 2018  路  7Comments  路  Source: xamarin/Xamarin.Forms

Description

Adding an Effect to an Xamarin.Forms Frame does not call OnAttached().

Steps to Reproduce

  1. implement a simple PlattformEffect on Android
using Xamarin.Forms;
using Xamarin.Forms.Platform.Android;

[assembly: ResolutionGroupName("EffectOnFrame")]
[assembly: ExportEffect(typeof(EffectOnFrame.Droid.DemoEffectDroid), "DemoEffect")]
namespace EffectOnFrame.Droid
{
    public class DemoEffectDroid : PlatformEffect
    {
        protected override void OnAttached() => System.Console.WriteLine("Attached");
        protected override void OnDetached() => System.Console.WriteLine("Detached");
    }
}
  1. Add Effect to a Frame:
using System;
using Xamarin.Forms;

namespace EffectOnFrame
{
    public class App : Application
    {
        public App()
        {
            var view = new Frame();
            view.Effects.Add(Effect.Resolve("EffectOnFrame.DemoEffect"));

            MainPage = new ContentPage { Content = view };
        }
    }
}

Expected Behavior

When launching the App the OnAttached() method should be called.

Actual Behavior

The Effect is not called properly when view is a Frame. If using a Label, StackLayout or ContentView the Effect is called properly.

Basic Information

  • Version with issue: Xamarin.Forms 3.1.0
  • Last known good version: ?
  • IDE: Visual Studio for Mac 7.5.4
  • Platform Target Frameworks:

    • Android: 8.1

5 in-progress Android bug

Most helpful comment

Just upgraded to Xamarin Forms 4.6.0.800 (from 4.5) and it seems this issue has resurfaced as my PlatformEffect that's attached to a Frame (inside the ItemTemplate of a CollectionView) is no longer attaching. When i move the effect to the Grid that's contained inside the Frame, it then works as expected.

All 7 comments

Yep. As reported. No logging for frame but'll work for label.

I made a PR to fix this issue : #4195
Two possible workarounds :

  • Use explicitly old renderer instead of FastRenderer: create a custom renderer which extends _Xamarin.Forms.Platform.Android.FrameRenderer_
  • Create a customer renderer which extends _Xamarin.Forms.Platform.Android.FastRenderers.FrameRenderer_ and add effects registering to OnElementChanged :
protected override void OnElementChanged(ElementChangedEventArgs<Frame> e)
 {
    base.OnElementChanged(e);
    VisualElement oldElement = e.OldElement;
    IElementController controller = oldElement;
    if (controller != null && controller.EffectControlProvider == this)
        controller.EffectControlProvider = null;

    controller = e.NewElement;
    if (controller != null)
        controller.EffectControlProvider = this;
}

Almost a year has passed and this is still an issue...Are there any plans to fix it?

This is still an issue in v3.6.0.

Which version can I expect this release?

@KalyanXamarin this is already available from 4.1 onwards

Just upgraded to Xamarin Forms 4.6.0.800 (from 4.5) and it seems this issue has resurfaced as my PlatformEffect that's attached to a Frame (inside the ItemTemplate of a CollectionView) is no longer attaching. When i move the effect to the Grid that's contained inside the Frame, it then works as expected.

Was this page helpful?
0 / 5 - 0 ratings