PlatformEffect fails to attach?

BuildCalcBuildCalc USMember ✭✭✭

I believe I am having trouble getting a PlatformEffect to attach. It appears that it is resolving but not attaching. Here is the PlatformEffect in an iOS library project named EffectTestLib.iOS:

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

[assembly: ResolutionGroupName ("MyCompany")]
[assembly: ExportEffect (typeof (EffectTestLib.iOS.MyEffect), "MyEffect")]
namespace EffectTestLib.iOS {

    public class MyEffect : PlatformEffect {

        protected override void OnAttached () {
            System.Diagnostics.Debug.WriteLine ("Effect Attached");
            throw new NotImplementedException ();
        }

        protected override void OnDetached () {
            throw new NotImplementedException ();
        }

        protected override void OnElementPropertyChanged (System.ComponentModel.PropertyChangedEventArgs args) {
            base.OnElementPropertyChanged (args);
        }
    }
}

So, if it attaches, it SHOULD output "Effect Attached" and throw a NotImplementedException.

Here is the PCL code for the application that uses this PlatformEffect:

using Xamarin.Forms;

namespace EffectTest { 
    public class App : Application {
        public App () {
            MainPage = new ContentPage {
                Content = new StackLayout {
                    VerticalOptions = LayoutOptions.Center,
                    Children = {
                        new Label {
                            XAlign = TextAlignment.Center,
                            Text = "Welcome to Xamarin Forms!"
                        }
                    }
                }
            };
            var effect = Effect.Resolve ("MyCompany.MyEffect");
            System.Diagnostics.Debug.WriteLine ("effect=[{0}]",effect);
            MainPage.Effects.Add(effect);
        }
    }
}

Notice I am writing the PlatformEffect to the application's output to verify it is resolved. If it doesn't resolve, I should see effect=[Xamarin.Forms.NullEffect].

Here is the applications iOS platform code:

using System;
using System.Collections.Generic;
using System.Linq;
using Foundation;
using UIKit;

namespace EffectTest.iOS
{
    [Register ("AppDelegate")]
    public partial class AppDelegate : global::Xamarin.Forms.Platform.iOS.FormsApplicationDelegate
    {
        public override bool FinishedLaunching (UIApplication app, NSDictionary options)
        {
            global::Xamarin.Forms.Forms.Init ();
            new EffectTestLib.iOS.MyEffect ();

            LoadApplication (new App ());

            return base.FinishedLaunching (app, options);
        }
    }
}

Notice that I am calling new EffectTestLib.iOS.MyEffect (); to be sure my library is loading.

Also, iOS platform project includes a reference to the EffectTestLib.iOS library.

So, when I run the application, this is what happens:

2016-04-06 10:17:18.342 EffectTest.iOS[26578:8696061] effect=[EffectTestLib.iOS.MyEffect]

So, the PlatformEffect is resolved correctly. However, it does not output "Effect Attached" nor throw NotImplementedException. Also note that, if I change a property of MainPage, MyEffect.OnPropertyChanged is never called.

Referring back to https://developer.xamarin.com/guides/xamarin-forms/effects/creating/, it appears I am:

  1. Creating a subclass of the PlatformEffect class
  2. Overriding the OnAttached method
  3. Overriding the OnDetached method
  4. Adding a ResolutionGroupName attribute to the effect class.
  5. Adding an ExportEffect attribute to the effect class.

And, referring back to https://developer.xamarin.com/guides/xamarin-forms/effects/creating/#Consuming_the_Effect_in_C, it appears that I am adding the effect to the pages Effects collection.

What am I doing wrong here?

Best Answers

Answers

  • MichaelRumplerMichaelRumpler ATMember ✭✭✭✭✭

    Sounds good to me.

    You could try

    • setting the Effect on the page in App.cs before you assign it to MainPage
    • moving MyEffect and the attributes from EffectTestLib.iOS to EffectTest.iOS to see if this is the problem

    I also filed a bug regarding Effects on Cells and attached a sample project to it. That project doesn't work because it demonstrates the bug, but the Effect can be attached to any non Cell element and then it is called. So you can have a look at that project and see if you spot a difference.

  • BuildCalcBuildCalc USMember ✭✭✭

    @MichaelRumpler

    Good to hear from you and hope all is well with you. I just tried your suggestions.

    setting the Effect on the page in App.cs before you assign it to MainPage

    Still resolves but doesn't attach

    moving MyEffect and the attributes from EffectTestLib.iOS to EffectTest.iOS to see if this is the problem

    Still resolves but doesn't attach! I must be doing something wrong!

  • BuildCalcBuildCalc USMember ✭✭✭

    Very interesting. I'll take a closer look this morning.

  • BuildCalcBuildCalc USMember ✭✭✭

    Argh! I've attempted to reapply PlatformEffects to the original project that led me to create the test project presented at the beginning of this thread. Originally, I had only implemented it on iOS on that project. I implemented it on Android - PlatformEffects attached! But, as before iOS PlatformEffects do resolve but don't attach.

    I then went back to the EffectsTest project and both Android and iOS PlatformEffects attach. Going back to my original project, iOS still doesn't (but Android does). I'm giving up for now and going back to something more productive.

  • BuildCalcBuildCalc USMember ✭✭✭

    @MichaelRumpler -

    Looks like I was wrong (because I was still trying to attach the platform effects to a Label element rather than a Page element). Yes, as you found, PlatformEffects do work on Android but, no, they don't on iOS. I've updated the GitHub sample project to demonstrate this.

  • BuildCalcBuildCalc USMember ✭✭✭

    @MichaelRumpler -

    Looks like the root cause has something to do with Page.effectControlProvider not getting set on iOS (but is on Android). I've updated the sample project to illustrate this.

  • AlejandrolvarezAlejandrolvarez USMember

    Hi!

    Is there any issue opened concerning this problem of PlatformEffects on iOS?

  • j2bmwj2bmw Member

    The issue "PlatformEffect fails to attach to Page elements in iOS" should have been fixed long time ago.
    I use Xamarin.forms 3.1.0.697729, but I still have the same problem.

    For iOS, I have to add an EntryRenderer in iOS project, and a derived Entry control, which is placed in the Renders folder in shared project.

    In this way I got it working. But this is just a workaround because PlatformEffect doesn't call OnAttached in iOS.

  • j2bmwj2bmw Member

    I posted the code for EntryRenderer EntryWithClearButonRenderer in iOS project, and a derived Entry control EntryWithClearButon: bettereducation.com.au/forum/it.aspx?g=posts&t=7941

Sign In or Register to comment.