KeldorKatarn
Topic Author
Posts: 193
Joined: 30 May 2014, 10:26

Assigning view to NoesisView in code

07 Apr 2021, 23:43

This is kind of a simple question. How do I assign a view to a NoesisView exactly?

Let's say I'd like to do something like this:
var view = new MyView();
view.DataContext = new MyViewModel();

NoesisView.View = view;
How do I do that exactly?
Content property seems to be null, and if I call LoadXaml() beforehand it crashes the entire UnityEditor. So what's the workflow here?

I must say the Unity workflow for simply stuff like this is still extremely unintuitive.
 
User avatar
sfernandez
Site Admin
Posts: 2983
Joined: 22 Dec 2011, 19:20

Re: Assigning view to NoesisView in code

09 Apr 2021, 12:58

In Unity you can have a root xaml, let's say MainPage, that you can add to the scene using the NoesisView component.
<Page
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
   <Grid x:Name="LayoutRoot"/>
 </Page>
This will generate a NoesisXaml asset that you can drag, for example, into the Main Camera object. This will automatically create a NoesisView component in the MainCamera and assign that NoesisXaml in the Xaml property.

You can then add to the "LayoutRoot" any control you create in code.
class MainPageBehavior : MonoBehavior
{
  void Start()
  {
    NoesisView noesisView = GetComponent<NoesisView>();
    Grid layoutRoot = (Grid)noesisView.Content.FindName("LayoutRoot");
    
    var view = new MyView();
    view.DataContext = new MyViewModel();
    layoutRoot.Children.Add(view);
  }
}
Content property seems to be null, and if I call LoadXaml() beforehand it crashes the entire UnityEditor.
Could you please report this crash and attach the crash dump that Unity generates? We shouldn't crash Unity editor.
I must say the Unity workflow for simply stuff like this is still extremely unintuitive.
Do you have any suggestion on how we can improve that workflow? We will be glad to hear it.
 
KeldorKatarn
Topic Author
Posts: 193
Joined: 30 May 2014, 10:26

Re: Assigning view to NoesisView in code

10 Apr 2021, 17:32

Could you please report this crash and attach the crash dump that Unity generates? We shouldn't crash Unity editor.
Crash report:
https://www.noesisengine.com/bugs/view.php?id=1964
Do you have any suggestion on how we can improve that workflow? We will be glad to hear it.
I cannot recommend anything beyond getting as close to the WPF API as possible. Any changes that are necessary are an additional obstacle and it is really
hard to find any documentation on your page that describes the differences. So usually when you run into a problem you're just on your own to figure out how your version of the API
is intended to be used.

So my recommendations: Massive effort to keep every bit of API identical to WPF and where that isn't possible or changed for a reason add easy to find and easy to understand documentation explaining the difference and how to use it, how to work around the difference to achieve the same result. And that documentation needs to be both online AND in the code itself so people find it with intellisense.
When I use one of your APIs and a parameter is missing compared to WPF then I have no idea why or how I'm supposed to adjust for that. That's usually the main issues I run into.
It's not that Noesis doesn't work, it's that I keep running into API differences that I don't have any information on, so every single one of them blocks me in my work as I have no information on how to proceed.
And coming to the forums and wait a couple hours for someone to explain it is not a sustainable workflow. That needs to be easier. I understand API changes are sometimes appropriate or necessary, but that needs to be explained.

Unlike a completely new API, this is an API that is based on an incredibly well established one. One I for example use every day of the week. When something doesn't work as I am used to, I get stuck. I rely on WPF's documentation on how to solve problems. When Noesis does it differently there's basically no documentation at all on the why or how.
 
KeldorKatarn
Topic Author
Posts: 193
Joined: 30 May 2014, 10:26

Re: Assigning view to NoesisView in code

10 Apr 2021, 17:45

In Unity you can have a root xaml, let's say MainPage, that you can add to the scene using the NoesisView component.
<Page
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
   <Grid x:Name="LayoutRoot"/>
 </Page>
This will generate a NoesisXaml asset that you can drag, for example, into the Main Camera object. This will automatically create a NoesisView component in the MainCamera and assign that NoesisXaml in the Xaml property.

You can then add to the "LayoutRoot" any control you create in code.
class MainPageBehavior : MonoBehavior
{
  void Start()
  {
    NoesisView noesisView = GetComponent<NoesisView>();
    Grid layoutRoot = (Grid)noesisView.Content.FindName("LayoutRoot");
    
    var view = new MyView();
    view.DataContext = new MyViewModel();
    layoutRoot.Children.Add(view);
  }
}
As I said, I cannot do this since the Content propery is null. There's nothing in there despite an assigned xaml which also gets correctly displayed.
Maybe I'm doing this in an application state where Noesis isn't fully initialized yet? (I'm doing it in a Zenject monoinstaller)

Edit: I just confirmed this, In "InstallBindings" of a Zenject MonoInstaller this all doesn't seem to be initialized yet. Maybe that's also the cause of the crash.
Zenject might set its scriptexecution order very low so calling this stuff early might cause Noesis to crash the editor and Content to still be null.
 
User avatar
jsantos
Site Admin
Posts: 3905
Joined: 20 Jan 2012, 17:18
Contact:

Re: Assigning view to NoesisView in code

12 Apr 2021, 11:16

Edit: I just confirmed this, In "InstallBindings" of a Zenject MonoInstaller this all doesn't seem to be initialized yet. Maybe that's also the cause of the crash.
Zenject might set its scriptexecution order very low so calling this stuff early might cause Noesis to crash the editor and Content to still be null.
Thanks for the bug report. Before interacting with our API, NoesisGUI needs to be fully initialized. We will analyze your report to at least avoid crashing and emit a clear log message.

Who is online

Users browsing this forum: Ahrefs [Bot], Google [Bot] and 86 guests