【问题标题】:PlatformEffect failing to attach?PlatformEffect 无法附加?
【发布时间】: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/,看来我是:

  1. 创建 PlatformEffect 类的子类
  2. 重写 OnAttached 方法
  3. 重写 OnDetached 方法
  4. 将 ResolutionGroupName 属性添加到效果类。
  5. 将 ExportEffect 属性添加到效果类。

并且,参考https://developer.xamarin.com/guides/xamarin-forms/effects/creating/#Consuming_the_Effect_in_C,看来我正在将效果添加到页面效果集合中。

我在这里做错了什么?

【问题讨论】:

标签: xamarin.forms effects


【解决方案1】:

我已经看过您的项目并修改了代码以使其正常工作, 它不会在 MainPage 上被调用, 观察是,在页面上它不起作用,在视图上它起作用(将在找到详细信息后编辑此观察)。 尝试这样做。

using Xamarin.Forms;

namespace EffectTest { 
    public class App : Application {
        public App () {
            var label = new Label();
            label.Effects.Add (Effect.Resolve ("MyCompany.EffectCheck"));
            MainPage = new RootPage ();
            var effect = Effect.Resolve ("MyCompany.EffectCheck");
            System.Diagnostics.Debug.WriteLine ("effect=[{0}]",effect);
            //MainPage.Effects.Add(Effect.Resolve ("MyCompany.EffectCheck"));
        }

        protected override void OnStart () {
        }

        protected override void OnSleep () {
        }

        protected override void OnResume () {
        }
    }
}

using Xamarin.Forms;

namespace EffectTest
{
    public class RootPage:ContentPage
    {
        public RootPage()
        {
            Content = new StackLayout () {
                Children = {
                    new Label ()
                }
            };
// this.Effects.Add(Effect.Resolve ("MyCompany.EffectCheck")); will not work
                    Content.Effects.Add(Effect.Resolve ("MyCompany.EffectCheck")); // it will work;
        }
    }

}

【讨论】:

  • 感谢您查看此内容。看来 PlatformEffects 确实适用于 Android 中的 Page 元素,但不适用于 iOS。我已更新 this sample github project 以显示此内容。
猜你喜欢
  • 2016-10-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-04-06
  • 2021-08-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多