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:
- Creating a subclass of the PlatformEffect class
- Overriding the OnAttached method
- Overriding the OnDetached method
- Adding a ResolutionGroupName attribute to the effect class.
- 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?