Page 1 of 1

Portability suggestions - InitializeComponent and ConnectEvent

Posted: 18 Jan 2022, 22:58
by asthomas
I am looking at porting a substantial application from WPF to Noesis. I have some suggestions that I think would make this easier, and eliminate the need for NOESIS-specific code in the window setup (at least in simple cases).

1. Make InitializeComponent virtual for Window, so it only needs to be overridden in cases where the default it not enough. The default could be:
        protected virtual void InitializeComponent()
        {
            GUI.LoadComponent(this, this.GetType().Name + ".xaml");
        }
2. Provide a default implementation for ConnectEvent that does the appropriate reflection to automatically add the event handler method to the event, something like this:
        protected virtual bool ConnectEvent(object source, string eventName, string handlerName)
        {
            EventInfo eventInfo = source.GetType().GetEvent(eventName);
            MethodInfo method = this.GetType().GetMethod(handlerName, BindingFlags.NonPublic | BindingFlags.Instance);

            if (eventInfo != null && method != null)
            {
                System.Type tDelegate = eventInfo.EventHandlerType;
                System.Delegate d = System.Delegate.CreateDelegate(tDelegate, this, method);
                eventInfo.AddEventHandler(source, d);
                return true;
            }
            return false;
        }

Re: Portability suggestions - InitializeComponent and ConnectEvent

Posted: 19 Jan 2022, 12:51
by jsantos
These are super good ideas! I think we tried something similar long time ago but as far as I remember reflection was not working as expected for AOT platforms (iOS, Xbox...). Could you please create a ticket about this to check again? Thank you!

Re: Portability suggestions - InitializeComponent and ConnectEvent

Posted: 19 Jan 2022, 15:19
by asthomas
I have added a ticket.

Incidentally, almost the same effect can be achieved by creating a derived class from Window, and then placing those two functions in that class. When porting from existing WPF code that class can be referenced with a "using" statement.
#if NOESIS
using Window = MyNamespace.MyDerivedWindow;
#endif
Since all WPF files need to have boilerplate changes to their using statements anyway, this limits the NOESIS-specific code to just one place.

Re: Portability suggestions - InitializeComponent and ConnectEvent

Posted: 20 Jan 2022, 13:03
by jsantos
Thanks for the ticket (#2244)