asthomas
Topic Author
Posts: 17
Joined: 28 Dec 2021, 16:20

Portability suggestions - InitializeComponent and ConnectEvent

18 Jan 2022, 22:58

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;
        }
 
User avatar
jsantos
Site Admin
Posts: 3906
Joined: 20 Jan 2012, 17:18
Contact:

Re: Portability suggestions - InitializeComponent and ConnectEvent

19 Jan 2022, 12:51

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!
 
asthomas
Topic Author
Posts: 17
Joined: 28 Dec 2021, 16:20

Re: Portability suggestions - InitializeComponent and ConnectEvent

19 Jan 2022, 15:19

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.
 
User avatar
jsantos
Site Admin
Posts: 3906
Joined: 20 Jan 2012, 17:18
Contact:

Re: Portability suggestions - InitializeComponent and ConnectEvent

20 Jan 2022, 13:03

Thanks for the ticket (#2244)

Who is online

Users browsing this forum: Ahrefs [Bot], Bing [Bot] and 7 guests