Page 1 of 1

NoesisView for Unity on macOS

Posted: 30 Oct 2023, 14:32
by Kirkules
My main UI is a NoesisView component on the main Camera GameObject.
At the start, I have a StartMPGame xaml.
I'd like to switch that xaml out with the game HUD xaml at runtime.
I'm trying all this stuff but it's not working. I'm new to Noesis.
                noesisView.enabled = false;
                noesisView.Xaml.UnregisterDependencies();
                noesisView.Content.DataContext = hudViewModel;
                noesisView.Xaml = hudXaml;
                noesisView.LoadXaml(true);
                noesisView.enabled = true;
Qs) Is this even possible to do at runtime? If so, how do I do this properly?
Thank you.

Unity 2022.3.12f1
Noesis 3.2.2

Re: NoesisView for Unity on macOS

Posted: 30 Oct 2023, 15:44
by Kirkules
I figured it out. It's an order of operations issue
                noesisView.enabled = false;
                noesisView.Xaml.UnregisterDependencies();
                noesisView.Xaml = hudXaml;
                noesisView.LoadXaml(true);
                noesisView.Content.DataContext = hudViewModel;
                noesisView.enabled = true;
Not sure if any of these are unnecessary?

Re: NoesisView for Unity on macOS

Posted: 31 Oct 2023, 13:04
by sfernandez
Hi,

It is better if you use a root xaml where you dynamically add or remove UI elements instead of changing the NoesisView component itself.

So you will have a xaml like this set in the NoesisView component:
<Grid
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
</Grid>
And then you can just load any xaml asset and add it to the root grid:
FrameworkElement LoadContent(NoesisView view, NoesisXaml xaml, object vm)
{
  FrameworkElement content = xaml.Load() as FrameworkElement; 
  if (content != null)
  {
    content.DataContext = vm;
    ((Grid)view.Content).Children.Add(content);
  }
  return content;
}
To unload that content you can just remove it from the root panel:
void RemoveContent(NoesisView view, FrameworkElement content)
{
  ((Grid)view.Content).Children.Remove(content);
}
Is this what you are looking for?