【发布时间】:2016-04-06 14:32:32
【问题描述】:
我相信我无法附加 PlatformEffect。似乎它正在解决但没有附加。这是名为EffectTestLib.iOS 的iOS 库项目中的PlatformEffect:
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);
}
}
}
所以,如果它附加了,它应该输出“Effect Attached”并抛出一个NotImplementedException。
这是使用此 PlatformEffect 的应用程序的 PCL 代码:
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);
}
}
}
请注意,我正在将 PlatformEffect 写入应用程序的输出以验证它是否已解决。如果没有解决,我应该看到effect=[Xamarin.Forms.NullEffect]。
以下是应用程序 iOS 平台代码: 使用系统; 使用 System.Collections.Generic; 使用 System.Linq; 使用基金会; 使用 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);
}
}
}
请注意,我正在调用 new EffectTestLib.iOS.MyEffect (); 以确保我的库正在加载。
此外,iOS 平台项目包含对EffectTestLib.iOS 库的引用。
所以,当我运行应用程序时,会发生以下情况:
2016-04-06 10:17:18.342 EffectTest.iOS[26578:8696061] effect=[EffectTestLib.iOS.MyEffect]
因此,PlatformEffect 已正确解析。但是,它不会输出“Effect Attached”,也不会抛出 NotImplementedException。另请注意,如果我更改 MainPage 的属性,则永远不会调用 MyEffect.OnPropertyChanged。
参考https://developer.xamarin.com/guides/xamarin-forms/effects/creating/,看来我是:
- 创建 PlatformEffect 类的子类
- 重写 OnAttached 方法
- 重写 OnDetached 方法
- 将 ResolutionGroupName 属性添加到效果类。
- 将 ExportEffect 属性添加到效果类。
并且,参考https://developer.xamarin.com/guides/xamarin-forms/effects/creating/#Consuming_the_Effect_in_C,看来我正在将效果添加到页面效果集合中。
我在这里做错了什么?
【问题讨论】:
-
重复我对 Saket 的回复,看来 PlatformEffects 确实适用于 Android 中的 Page 元素,但不适用于 iOS。我更新了the github sample project 来说明这一点。
标签: xamarin.forms effects