Kir-Antipov / HotAvalonia

πŸ”₯ Supercharge your Avalonia development experience with hot reload capabilities
MIT License
202 stars 7 forks source link

Support for CodeBehind Binding (somehow) #13

Open Gawidev opened 1 month ago

Gawidev commented 1 month ago

I'm assuming that since the only things being rehydrated are the axaml and some parts related to it, then its expected that any code-behind binding done in the constructor inevitably gets discarded on hot reload. The issue that arises from this is simply that it forces the use of Avalonia's MarkupExtension for all binding, which is pretty limiting and might be incompatible with a lot of projects. I'm really not sure what would be the best option for handling this, I guess something as simple as an OnHotReloaded() event could be enough. It's a rather crude example, I don't quite know enough about what's going on inside to make a better suggestion at the moment. I'd be curious to hear what other possibilities might be viable.

Regardless, this library is absolutely incredible already :) thanks for the hassle-free productivity boost πŸ‘

Kir-Antipov commented 1 month ago

Big preesh on the kind words! They're always nice to hear!

Regarding the issue itself, I had a completely unhinged idea of rerunning a constructor (or at least parts of it) on its corresponding rehydrated component whenever a hot reload occurs. However, I quickly dismissed it, as it creates a host of other problems.

And I much prefer your suggestion of allowing users to explicitly opt-in for such behavior! I think an attribute would suffice since C# allows you to slap Conditional on top of the attributes, meaning they can be completely eradicated from the non-relevant environments (like production), thus avoiding unnecessary metadata pollution. It should also be pretty straightforward for users:

  public FooControl()
  {
    InitializeComponent();
-   DoSomething();
+   Initialize();
  }

+ [HotReload]
+ private void Initialize()
+ {
+   DoSomething();
+ }

How does that sound?

Gawidev commented 2 weeks ago

How does that sound?

Wonderful! Sorry for the late reply, but yes this does sound like a perfect way of doing things. Looking forward to hearing more about itπŸ˜„