Kirkules
Topic Author
Posts: 2
Joined: 06 Aug 2023, 15:58

NoesisView for Unity on macOS

30 Oct 2023, 14:32

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
 
Kirkules
Topic Author
Posts: 2
Joined: 06 Aug 2023, 15:58

Re: NoesisView for Unity on macOS

30 Oct 2023, 15:44

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?
 
User avatar
sfernandez
Site Admin
Posts: 2908
Joined: 22 Dec 2011, 19:20

Re: NoesisView for Unity on macOS

31 Oct 2023, 13:04

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?

Who is online

Users browsing this forum: Ahrefs [Bot] and 1 guest