Page 1 of 1

[RECOVERED] Dynamic runtime xaml construction with modular windows

Posted: 03 Nov 2018, 23:32
by jsantos
Our database got corrupted and we lost the content of this topic. Please, help us to recover this discussion.

Re: [RECOVERED] Dynamic runtime xaml construction with modular windows

Posted: 24 Nov 2018, 14:08
by digimbyte
I was seeking information on how to Enable and build a GUI through code,
Not using code behind, what would be the best integration for the base Xaml in Unity?
<usercontrol> or <grid>
Do they have pro's and cons?

one part was how to implement
<UserControl
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    d:DesignWidth="1280" d:DesignHeight="720">
    <Grid>
    </Grid>
</UserControl>
I've been experiencing X:Class type errors, is there a naming convention or should I ditch it?

Re: [RECOVERED] Dynamic runtime xaml construction with modular windows

Posted: 25 Nov 2018, 21:42
by digimbyte
this is my base Xaml
<UserControl
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="d"
    xmlns:noesis="clr-namespace:NoesisGUIExtensions"
    x:Class="GUI.MainCanvas"
    x:Name="MainCanvas"
    d:DesignWidth="1280" d:DesignHeight="720"
    FontFamily="Fonts/#WeblySleek UI Semilight">

    <Grid x:Name="LayoutRoot" RenderTransformOrigin="0.5,0.5">
    </Grid>
</UserControl>

Re: [RECOVERED] Dynamic runtime xaml construction with modular windows

Posted: 26 Nov 2018, 16:52
by sfernandez
The x:Class attribute refers to the code-behind class corresponding to that xaml.

If you have an x:Class value of "GUI.MainCanvas" then you need to define that code-behind class like this:
using Noesis;

namespace GUI
{
  public partial class MainCanvas : UserControl { ... }
}

Re: [RECOVERED] Dynamic runtime xaml construction with modular windows

Posted: 26 Nov 2018, 17:10
by sfernandez
The same UI from a XAML can be built using only code.

Depending on how many children do you expect to add to the root element you will choose the type:
  • If only a single content will be added you can have a UserControl (ContentControl) as root, and you will set its Content property.
    UserControl root = new UserControl();
    root.Content = new Button();
  • If you plan to add many children, then you can choose any of the available Panels, for example a Grid. In this case you will use the Children property, and call Add() on the returned collection.
    Grid root = new Grid();
    root.Children.Add(new Button());
    root.Children.Add(new Ellipse { Fill = Brushes.Blue });

Re: [RECOVERED] Dynamic runtime xaml construction with modular windows

Posted: 26 Nov 2018, 17:56
by sfernandez
To connect the UI created in code to the NoesisView component in Unity you would need a minimal xaml that will serve as the root of all your UI:
<Grid
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <!-- your UI will be added here -->
</Grid>
When you create that xaml inside Unity Assets folder an asset will be automatically generated. You can drag that asset into the Main Camera game object to create a NoesisView component which will load the root xaml. From a script attached to the Main Camera you can access the NoesisView and add all the UI elements you need:
using UnityEngine;

public LoadUIBehavior : MonoBehavior
{
  void Start()
  {
    NoesisView view = GetComponent<NoesisView>();
    Noesis.Grid root = (Noesis.Grid)view.Content;
    root.Children.Add(new Button());
    ...
  }
}
This will render the created UI on top of the 3D scene.

Re: [RECOVERED] Dynamic runtime xaml construction with modular windows

Posted: 28 Nov 2018, 13:09
by digimbyte
Thanks for the wonderful replies!
when the forums crashed, I lost this, and my work as my changes got corrupted
so I'm now able to catch up to where I was before

also, shouldn't that be
Grid root = new Grid();
root.Children.Add(new Button());
root.Children.Add(new Ellipse { Fill = Brushes.Blue });
one last question, does NoesisGui integrate with Unity's GuiUtility?
such as set focus?

Re: [RECOVERED] Dynamic runtime xaml construction with modular windows

Posted: 28 Nov 2018, 13:31
by sfernandez
shouldn't that be
Grid root = new Grid();
root.Children.Add(new Button());
root.Children.Add(new Ellipse { Fill = Brushes.Blue });
You are right, I fixed the typo, thanks.
does NoesisGui integrate with Unity's GuiUtility?
such as set focus?
No, Noesis keeps track of its own focus.
You can change it by calling Focus() on any UIElement.
button.Focus();
And to query about current focused element you can get Keyboard device from any UIElement and do:
UIElement focused = root.Keyboard.FocusedElement;