Noesis 2 can't import XAMLs always because it runs code-behind
On our other project (non-blank) that we managed to get to work after import/reimport in Unity 5.6 I noticed that the code behind runs on import.
Some of the code accesses textures or resources that are not loaded until runtime. This causes the import to fail and in some cases Unity crash.
Ideally Noesis shouldn't run any user C# code on import.
As a workaround, we either make changes to the C# files that we revert, or we press "Play" then import - effectively importing only works in play mode.
Is there a reason why Noesis would run any C# code when importing XAML files to create .asset files?
Some of the code accesses textures or resources that are not loaded until runtime. This causes the import to fail and in some cases Unity crash.
Ideally Noesis shouldn't run any user C# code on import.
As a workaround, we either make changes to the C# files that we revert, or we press "Play" then import - effectively importing only works in play mode.
Is there a reason why Noesis would run any C# code when importing XAML files to create .asset files?
Last edited by nokola on 08 Apr 2017, 22:07, edited 1 time in total.
- ai_enabled
- Posts: 231
- Joined:
- Contact:
Re: Noesis 2 can't import XAMLs always because it runs code-behind
NoesisGUI 2.0 tries to instantiate the XAML control during import to generate the preview (Unity asset preview image). Not really useful and most likely will fail so I'm not really sure why it's enabled by default. You could disable the preview generation in the NoesisGUI settings panel.
Asset-files are basically XAML with another extension, there are no any pre-processing done. NoesisGUI 2.0 could directly load any XAML file, there is no build tool anymore.
Asset-files are basically XAML with another extension, there are no any pre-processing done. NoesisGUI 2.0 could directly load any XAML file, there is no build tool anymore.
AtomicTorch Studio Pte. Ltd. http://atomictorch.com
Re: Noesis 2 can't import XAMLs always because it runs code-behind
Thanks! I'll try it. Regardless, if the code-behind crashes, the preview should still work.
In my case the code behind doesn't really do anything with the XAML on init, just starts some operations that access resources on init.
If that ends up "scrambling" the preview or having partial preview for the XAML because the code-behind crashed that's OK. Much better than failing to reimport
In my case the code behind doesn't really do anything with the XAML on init, just starts some operations that access resources on init.
If that ends up "scrambling" the preview or having partial preview for the XAML because the code-behind crashed that's OK. Much better than failing to reimport
Re: Noesis 2 can't import XAMLs always because it runs code-behind
When importing a XAML, process that happens each time they are modified, we need to fully load the XAML because:
But it is true that it shouldn't be strictly needed just to check syntax errors and edit time... Anyway we have improved this process to behave correctly when the code-behind crashes and we hope to release a new version very soon.
- The thumbnail icon is regenerated (this can be disabled as ai_enabled indicated)
- We provide parsing errors at edit time. We think this is better than doing it when you hit Play.
But it is true that it shouldn't be strictly needed just to check syntax errors and edit time... Anyway we have improved this process to behave correctly when the code-behind crashes and we hope to release a new version very soon.
Re: Noesis 2 can't import XAMLs always because it runs code-behind
Awesome, thanks! Maybe having a bool IsInDesignMode (similar to WPF) can help avoid these. Anyway, if the errors from code behind are reported correctly, it would be easy to add checks "if null then skip part X of the code" etc. Looking forward to the new version
-
sfernandez
Site Admin
- Posts: 3154
- Joined:
Re: Noesis 2 can't import XAMLs always because it runs code-behind
Just to clarify this a bit.
When a XAML is imported to Unity we load it to show all the errors it can contain. This means we create the entire tree of elements associated with that xaml, so the constructor of each element will get called.
If an element happens to be a code-behind class, then its constructor should have a LoadComponent in its constructor, but only that. All the extra initialization code related to that class should go inside Initialized or Loaded event handlers. A code-behind class could look like this:
Code in Initialized or Loaded handlers won't be called until control gets added to a View. The thing is if you have previews enabled in Unity we are in fact creating a View, attaching the xaml content and trying to render a frame, so if your Initialized/Loaded code depends on application being running, it is better to disable the previews.
When a XAML is imported to Unity we load it to show all the errors it can contain. This means we create the entire tree of elements associated with that xaml, so the constructor of each element will get called.
If an element happens to be a code-behind class, then its constructor should have a LoadComponent in its constructor, but only that. All the extra initialization code related to that class should go inside Initialized or Loaded event handlers. A code-behind class could look like this:
Code: Select all
public class MyControl : UserControl
{
public MyControl()
{
Initialized += MyControl_Initialized;
Loaded += MyControl_Loaded;
InitializeComponent();
}
private void InitializeComponent()
{
Noesis.GUI.LoadComponent(this, "path_to_associated_xaml");
}
private void MyControl_Initialized(object sender, EventArgs e)
{
// this is raised when control gets added to NoesisView UI tree
}
private void MyControl_Loaded(object sender, RoutedEventArgs e)
{
// this is raised when control finishes its first layout pass, so everything in
// the UI tree is already measured and arranged, all templates are applied
}
}
Re: Noesis 2 can't import XAMLs always because it runs code-behind
Thanks!
Can Initialized be called more than once? e.g. if I attach, detach, then re-attach control to the UI tree? We do this often since we have screens/pages and navigation and swap in/out controls.
Similarly, does Loaded get called more than once?
We'll likely move all code out of the constructors or disable previews
Can Initialized be called more than once? e.g. if I attach, detach, then re-attach control to the UI tree? We do this often since we have screens/pages and navigation and swap in/out controls.
Similarly, does Loaded get called more than once?
We'll likely move all code out of the constructors or disable previews
-
sfernandez
Site Admin
- Posts: 3154
- Joined:
Re: Noesis 2 can't import XAMLs always because it runs code-behind
Initialized event is called only once, the first time an object is attached to a View.
Otherwise, the Loaded/Unloaded events are called many times, whenever the element is added (Loaded) and removed (Unloaded) from the tree.
Otherwise, the Loaded/Unloaded events are called many times, whenever the element is added (Loaded) and removed (Unloaded) from the tree.
Who is online
Users browsing this forum: Bing [Bot], Google [Bot] and 3 guests