User avatar
jsantos
Site Admin
Topic Author
Posts: 3906
Joined: 20 Jan 2012, 17:18
Contact:

[RECOVERED] Dynamic runtime xaml construction with modular windows

03 Nov 2018, 23:32

Our database got corrupted and we lost the content of this topic. Please, help us to recover this discussion.
 
User avatar
digimbyte
Posts: 47
Joined: 14 Nov 2017, 21:42

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

24 Nov 2018, 14:08

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?
Last edited by digimbyte on 25 Nov 2018, 22:22, edited 2 times in total.
 
User avatar
digimbyte
Posts: 47
Joined: 14 Nov 2017, 21:42

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

25 Nov 2018, 21:42

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

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

26 Nov 2018, 16:52

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 { ... }
}
 
User avatar
sfernandez
Site Admin
Posts: 2984
Joined: 22 Dec 2011, 19:20

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

26 Nov 2018, 17:10

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 });
 
User avatar
sfernandez
Site Admin
Posts: 2984
Joined: 22 Dec 2011, 19:20

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

26 Nov 2018, 17:56

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.
 
User avatar
digimbyte
Posts: 47
Joined: 14 Nov 2017, 21:42

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

28 Nov 2018, 13:09

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

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

28 Nov 2018, 13:31

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;

Who is online

Users browsing this forum: Ahrefs [Bot], Bing [Bot] and 7 guests