Ninject:方法未在 Silverlight 中被拦截
我在 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
有人可以帮忙吗?谢谢
I use Ninject for DI in Silverlight app. Now I am trying to implement interception and having issue. My methhod is not getting intercepted. Below is sample implementation
public class InfrastructureModule : NinjectModule
{
public override void Load()
{
Bind<IGlobalEventManager>().To<GlobalEventManager>().InSingletonScope();
Bind<IViewModel>().To<ViewModel>().InSingletonScope();
}
}
In App.xaml --> Application_Startup event, i do the binding and call method i expect to be intercepted:
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();
}
Implementation of ViewModel:
public class ViewModel : IViewModel
{
[LockUI]
public virtual void SomeMethod()
{
MessageBox.Show("Hello");
}
}
LockUI attribute:
[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 implementation:
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 implementation:
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);
}
}
}
I have added reference to Castle.Core, Castle.DynamicProxy2, Ninject, Ninject.Extensions.Interception, Ninject.Extensions.Interception.DynamicProxy2
Could someone please help. Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论