我在Silverlight app中使用Ninject进行DI。现在我正在尝试实施拦截并有问题。我的方法没有被截获。以下是示例实现
public class InfrastructureModule : NinjectModule
{
public override void Load()
{
Bind<IGlobalEventManager>().To<GlobalEventManager>().InSingletonScope();
Bind<IViewModel>().To<ViewModel>().InSingletonScope();
}
}
在App.xaml中 - &gt; 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
的引用请有人帮忙。感谢