我在 Silverlight 应用程序中使用 Ninject 进行 DI。现在我正在尝试实施拦截并遇到问题。我的方法没有被拦截。下面是示例实现
public class InfrastructureModule : NinjectModule
{
public override void Load()
{
Bind<IGlobalEventManager>().To<GlobalEventManager>().InSingletonScope();
Bind<IViewModel>().To<ViewModel>().InSingletonScope();
}
}
在 App.xaml --> Application_Startup 事件中,我执行了我希望被拦截的绑定和调用方法:
private void Application_Startup(object sender, StartupEventArgs e)
{
IKernel kernel = new StandardKernel(new InfrastructureModule());
IViewModel vm = kernel.Get<IViewModel>();
vm.SomeMethod();
this.RootVisual = new MainPage();
}
ViewModel 的实现:
public class ViewModel : IViewModel
{
[LockUI]
public virtual void SomeMethod()
{
MessageBox.Show("Hello");
}
}
LockUI 属性:
[AttributeUsage(AttributeTargets.Property | AttributeTargets.Method, AllowMultiple = false,
Inherited = true)]
public class LockUIAttribute : InterceptAttribute
{
public override IInterceptor CreateInterceptor(IProxyRequest request)
{
return request.Context.Kernel.Get<LockUIInterceptor>();
}
}
LockUIInterceptor 实现:
public class LockUIInterceptor : SimpleInterceptor
{
private IGlobalEventManager _globalEventManager;
public LockUIInterceptor(IGlobalEventManager globalEventManager)
{
_globalEventManager = globalEventManager;
}
protected override void BeforeInvoke(IInvocation invocation)
{
_globalEventManager.OnLockUI(this, EventArgs.Empty);
}
}
GlobalEventManager 实现:
public class GlobalEventManager : IGlobalEventManager
{
private static int _asyncCallCount;
public event EventHandler UnLockUI;
public event EventHandler LockUI;
public void OnLockUI(object sender, EventArgs e)
{
if (LockUI != null)
{
_asyncCallCount += 1;
LockUI(sender, e);
}
}
public void OnUnlockUI(object sender, EventArgs e)
{
if (UnLockUI != null)
{
_asyncCallCount -= 1;
if (_asyncCallCount == 0)
UnLockUI(sender, e);
}
}
}
我添加了对 Castle.Core、Castle.DynamicProxy2、Ninject、Ninject.Extensions.Interception、Ninject.Extensions.Interception.DynamicProxy2 的引用
有人可以帮忙吗。谢谢