Xamarin.forms 平台效应无法连接?

Xamarin.forms 平台效应无法连接?,xamarin.forms,effects,Xamarin.forms,Effects,我相信我在连接平台效果时遇到了问题。它似乎正在解析,但未附加。以下是名为EffectTestLib.iOS的iOS库项目中的PlatformEffect: using System; using Xamarin.Forms; using Xamarin.Forms.Platform.iOS; [assembly: ResolutionGroupName ("MyCompany")] [assembly: ExportEffect (typeof (EffectTestLib.iOS.MyEff

我相信我在连接平台效果时遇到了问题。它似乎正在解析,但未附加。以下是名为
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

以下是使用此平台效果的应用程序的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);
        }
    }
}
注意,我正在调用
neweffecttestlib.iOS.MyEffect()以确保我的库正在加载

此外,iOS平台项目还包括对
EffectTestLib.iOS
库的引用

因此,当我运行应用程序时,会发生以下情况:

2016-04-06 10:17:18.342 EffectTest.iOS[26578:8696061] effect=[EffectTestLib.iOS.MyEffect]
因此,平台效应得到了正确的解决。但是,它不会输出“附加效果”,也不会抛出NotImplementedException。还要注意,如果我更改了MainPage的属性,则永远不会调用MyEffect.OnPropertyChanged

回想起来,我似乎是:

  • 创建PlatformEffect类的子类
  • 重写附加的方法
  • 重写OnDepached方法
  • 将ResolutionGroupName属性添加到effect类
  • 将ExportEffect属性添加到effect类
  • 而且,回头看,我似乎正在将效果添加到pages Effects集合中


    我做错了什么?

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

    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;
            }
        }
    
    }
    

    源代码:重复我对Saket的回答,PlatformEffects似乎在Android中对页面元素有效,但在iOS中不起作用。我已经更新来说明这一点。谢谢你看这个。在Android系统中,PlatformEffects似乎对页面元素有效,但对iOS系统无效。我已经更新以显示这一点。