OpenNetCF.IOC 事件订阅未触发

发布于 2024-12-07 16:01:42 字数 1981 浏览 1 评论 0原文

我正在使用 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

I am developing a Vb.net .Net 3.5 PDA application using the OpenNetCF IOC framework. I have set up and event to handle the navigation through the smart parts but when I raise the event the EventSubscription does not fire.

I am sure I have missed something simple but would appreciate so advice.

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

木有鱼丸 2024-12-14 16:01:42

好吧,想通了。

这是为了满足启动对象是 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

它需要这样才能允许项目、智能部件和服务正确连接事件。

菲尔

Ok, figured it out.

It was do to a requirement that the startup object is Sub Main and that class inherits from 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

It requires this in order to allow the items, smart parts and services to wire up the event correctly.

Phil

风铃鹿 2024-12-14 16:01:42

感谢克里斯的回复,

我在创建具有 EventPublication 设置的新智能部件时也收到一条错误消息。

MAIN FORM

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

NEW SMART PART

<EventPublication(EventNames.Navigate)> _
Public Event NavigateToSmartPart As EventHandler(Of GenericEventArgs(Of String))

它在 else if (ctors.Count() == 1) 行的以下方法中抛出目标调用错误(内部异常 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 属性,则智能部分将成功创建。

Thanks for the response Chris,

I am also getting an error message when creating new smart parts that have EventPublication setup.

MAIN FORM

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

NEW SMART PART

<EventPublication(EventNames.Navigate)> _
Public Event NavigateToSmartPart As EventHandler(Of GenericEventArgs(Of String))

Its throwing a Target Invokation error (Inner Exception NullReferenceException) in the below method on the else if (ctors.Count() == 1) line.

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

Call stack

   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()

If I remove the EventPublication attribute from the event then the smart part is created successfully.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文