我正在使用OpenNetCF IOC框架开发Vb.net .Net 3.5 PDA应用程序。我已经设置了事件来处理智能部件的导航,但是当我举起事件时,EventSubscription不会触发。
我确信我错过了一些简单的事情,但我会非常感谢你的建议。
Imports OpenNETCF
Imports OpenNETCF.IoC
Imports OpenNETCF.IoC.UI
Public Class MainContainer
<EventPublication(EventNames.Navigate)> _
Public Event NavigateToSmartPart As EventHandler(Of GenericEventArgs(Of SmartPart))
Public Sub New()
' This call is required by the Windows Form Designer.
InitializeComponent()
' Add any initialization after the InitializeComponent() call.
RootWorkItem.Items.Add(workspace, WorkspaceNames.StackWorkspace)
RootWorkItem.Services.AddOnDemand(Of XMLWrapper)()
RootWorkItem.Services.AddOnDemand(Of DataInterface)()
'RootWorkItem.SmartParts.AddNewDisposable(Of ViewCamera)()
RootWorkItem.SmartParts.AddNew(Of ViewGoodInInspection)()
RootWorkItem.SmartParts.AddNew(Of ViewLogon)()
RootWorkItem.SmartParts.AddNew(Of ViewPartCentre)()
RootWorkItem.SmartParts.AddNew(Of ViewSplash)()
End Sub
Private Sub MainContainer_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load
RootWorkItem.Services.Get(Of DataInterface)().InitialseApplication()
If RootWorkItem.Services.Get(Of XMLWrapper)().LoadPartCentreID.Equals(UInt16.MinValue) Then
RaiseEvent NavigateToSmartPart(Me, New GenericEventArgs(Of SmartPart)(RootWorkItem.SmartParts.Get(Of ViewPartCentre).First))
Else
RaiseEvent NavigateToSmartPart(Me, New GenericEventArgs(Of SmartPart)(RootWorkItem.SmartParts.Get(Of ViewSplash).First))
End If
End Sub
<EventSubscription(EventNames.Navigate, ThreadOption.Caller)> _
Public Sub NavigateSmartPart(Of T As SmartPart)()
'Public Sub NavigateSmartPart(Of T As SmartPart)()
workspace.Show(RootWorkItem.SmartParts.Get(Of t).First)
End Sub
End Class
答案 0 :(得分:1)
好的,想通了。
要求启动对象是Sub Main,并且该类继承自SmartClientApplication(Of Form)。
Imports OpenNETCF.IoC.UI
Namespace OpenNetCF_Events_CSharp
Public Class Startup
Inherits SmartClientApplication(Of Form1)
Public Shared Sub Main()
Dim appStarter As New Startup
appStarter.Start()
End Sub
End Class
End Namespace
它需要这样才能让物品,智能部件和服务正确地连接事件。
菲尔
答案 1 :(得分:0)
感谢Chris的回复,
在创建具有EventPublication设置的新智能部件时,我也收到错误消息。
主要表格
RootWorkItem.SmartParts.AddNew(Of ViewLogon)()
<EventSubscription(EventNames.Navigate, ThreadOption.UserInterface)> _
Public Sub NavigateSmartPart(ByVal sender As Object, ByVal e As GenericEventArgs(Of String))
workspace.Show(RootWorkItem.SmartParts.Get(e.Value))
End Sub
新智能部分
<EventPublication(EventNames.Navigate)> _
Public Event NavigateToSmartPart As EventHandler(Of GenericEventArgs(Of String))
它在else if(ctors.Count()== 1)行的下面的方法中抛出了一个Target Invokation错误(Inner Exception NullReferenceException)。
internal static object CreateObject(Type t, WorkItem root)
{
object instance = null;
// first check the cache
if(m_constructorCache.ContainsKey(t))
{
return CreateObjectFromCache(t, root);
}
ConstructorInfo ci;
if (t.IsInterface)
{
throw new IOCException(string.Format("Cannot create an instance of an interface class ({0}). Check your registration code.", t.Name));
}
// see if there is an injection ctor
var ctors = (from c in t.GetConstructors(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance)
where c.IsPublic == true
&& c.GetCustomAttributes(typeof(InjectionConstructorAttribute), true).Count() > 0
select c);
if (ctors.Count() == 0)
{
// no injection ctor, get the default, parameterless ctor
var parameterlessCtors = (from c in t.GetConstructors(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance)
where c.GetParameters().Length == 0
select c);
if (parameterlessCtors.Count() == 0)
{
throw new ArgumentException(string.Format("Type '{0}' has no public parameterless constructor or injection constructor.\r\nAre you missing the InjectionConstructor attribute?", t));
}
// create the object
ci = parameterlessCtors.First();
try
{
instance = ci.Invoke(null);
m_constructorCache.Add(t, new InjectionConstructor { CI = ci });
}
catch (TargetInvocationException ex)
{
throw ex.InnerException;
}
}
else if (ctors.Count() == 1)
{
// call the injection ctor
ci = ctors.First();
ParameterInfo[] paramList = ci.GetParameters();
object[] inputs = GetParameterObjectsForParameterList(paramList, root, t.Name);
try
{
instance = ci.Invoke(inputs);
m_constructorCache.Add(t, new InjectionConstructor { CI = ci, ParameterList = paramList });
}
catch (TargetInvocationException ex)
{
throw ex.InnerException;
}
}
else
{
throw new ArgumentException(string.Format("Type '{0}' has {1} defined injection constructors. Only one is allowed", t.Name, ctors.Count()));
}
// NOTE: we don't do injections here, as if the created object has a dependency that requires this instance it would fail becasue this instance is not yet in the item list.
return instance;
}
调用堆栈
at OpenNETCF.IoC.ObjectFactory.AddCollectionEventHandlers[TKey,TItem](Object instance, IEnumerable`1 collection, PublicationDescriptor[] sourceEvents, SubscriptionDescriptor[] eventSinks)
at OpenNETCF.IoC.ObjectFactory.AddEventHandlers(Object instance, WorkItem root, Boolean walkUpToRoot)
at OpenNETCF.IoC.ObjectFactory.AddEventHandlers(Object instance, WorkItem root)
at OpenNETCF.IoC.ObjectFactory.DoInjections(Object instance, WorkItem root)
at OpenNETCF.IoC.ManagedObjectCollection`1.Add(ISmartPart item, String id, Boolean expectNullId)
at OpenNETCF.IoC.ManagedObjectCollection`1.AddNew(Type typeToBuild, String id, Boolean expectNullId, Boolean wrapDisposables)
at OpenNETCF.IoC.ManagedObjectCollection`1.AddNew(Type typeToBuild)
at OpenNETCF.IoC.ManagedObjectCollection`1.AddNew[TTypeToBuild]()
at GoodsInInspection.MainContainer..ctor()
at System.Reflection.RuntimeConstructorInfo.InternalInvoke(RuntimeConstructorInfo rtci, BindingFlags invokeAttr, Binder binder, Object parameters, CultureInfo culture, Boolean isBinderDefault, Assembly caller, Boolean verifyAccess, StackCrawlMark& stackMark)
at System.Reflection.RuntimeConstructorInfo.Invoke(BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
at System.Reflection.ConstructorInfo.Invoke(Object[] parameters)
at OpenNETCF.IoC.ObjectFactory.CreateObject(Type t, WorkItem root)
at OpenNETCF.IoC.ManagedObjectCollection`1.AddNew(Type typeToBuild, String id, Boolean expectNullId, Boolean wrapDisposables)
at OpenNETCF.IoC.ManagedObjectCollection`1.AddNew(Type typeToBuild)
at OpenNETCF.IoC.ManagedObjectCollection`1.AddNew[TTypeToBuild]()
at OpenNETCF.IoC.UI.SmartClientApplication`1.Start(IModuleInfoStore store)
at OpenNETCF.IoC.UI.SmartClientApplication`1.Start()
at GoodsInInspection.Startup.Main()
如果从事件中删除EventPublication属性,则会成功创建智能部件。