joshmond
Topic Author
Posts: 22
Joined: 02 Jun 2018, 00:10

Issue With Custom views in NoesisGUI

13 Nov 2020, 03:46

Hello guys, I recently have installed the Noesis plugin for Unity and I'm trying to create and load a smaple view. I have created a separate WPF project and created a very basic view. I have dragged both the MainWindow.xaml view and the MainWindow.xaml.cs file over to my Unity project but I get errors when I load in the files and set the view. I was wondering how I could fix this issue.

Here is a screenshot of the errors I get when trying to load in the views:


https://ibb.co/mJ3SvtY

This is the code I have for both the xaml and the xaml.cs respectively:

MainWindow.xaml
<Window x:Class="WpfApp1.MainWindow"
        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"
        xmlns:local="clr-namespace:WpfApp1"
        mc:Ignorable="d"
        Title="Hello NoesisGUI" Height="450" Width="800">
    <Grid>
        <TextBlock FontSize="60" HorizontalAlignment="Center" VerticalAlignment="Center">Hello World</TextBlock>
    </Grid>
</Window>
MainWindow.xaml.cs
#if UNITY_5_3_OR_NEWER
#define NOESIS
using Noesis;
#else
using System;
using System.Windows;
using System.Windows.Controls;
#endif

namespace WpfApp1
{
    /// <summary>
    /// Interaction logic for MainWindowxaml.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

#if NOESIS
        private void InitializeComponent()
        {
            Noesis.GUI.LoadComponent(this, "Assets/MainWindow.xaml");
        }
#endif
    }
}

Any help on this would be greatly appreciated.

-Joshmond

Tags:
 
JinxxNoesis
Posts: 7
Joined: 12 Nov 2020, 23:00

Re: Issue With Custom views in NoesisGUI

13 Nov 2020, 16:13

Same here
 
User avatar
sfernandez
Site Admin
Posts: 2262
Joined: 22 Dec 2011, 19:20

Re: Issue With Custom views in NoesisGUI

13 Nov 2020, 19:31

Hi, sorry for the inconvenience, I'm gonna need some information to determine what could be happening.

Did you have a way to always reproduce this situation?
Which steps did you follow to import the files? Copied them all at once, or first the xaml and then the cs, or viceversa?
After importing the cs file, did the assembly correctly build or did you have compiler errors?
Are you getting any parsing errors on the xaml when you import it? You should, because the xaml root is a Window, and Noesis doesn't support Window elements in Unity, as the operating system window is provided by Unity itself. You should use a UserControl as root instead (or a simple panel like Grid will also work).

In the meantime I'll try to reproduce those errors myself and let you know.
 
joshmond
Topic Author
Posts: 22
Joined: 02 Jun 2018, 00:10

Re: Issue With Custom views in NoesisGUI

15 Nov 2020, 06:51

I thought I would report back and give an update on where I am now with Noesis. I have no idea what the issue was or what was causing it, I got around the issue by creating a new Unity project, installing the NoesisGUI package and everything just worked. I could not get the current project to work, even after re-installing the package, nor can I seem to reproduce the error. So I wiped everything and started fresh and that seemed to have worked out.

I do have two questions about working NoesisGUI in Unity and am hoping someone could provide some feedback.

The first question is when working with Noesis, what is the best way to switch between views. Would you have multiple User controls in one main User Control and switch between them based on the data-context?

The second question I have is whether or not there is a way to get Noesis working with Unity's new input system?

-Joshmond
 
User avatar
sfernandez
Site Admin
Posts: 2262
Joined: 22 Dec 2011, 19:20

Re: Issue With Custom views in NoesisGUI

17 Nov 2020, 11:29

I tried to reproduce the "Unexpected empty xaml" error without luck.
What I get when importing those files is a compiler error because of the unknown Window class:
Assets\MainWindow.xaml.cs(15,39): error CS0246: The type or namespace name 'Window' could not be found (are you missing a using directive or an assembly reference?)
And xaml parser errors for the same reason:
[noesis] Assets/MainWindow.xaml(1): Unknown type 'Window'.
Do you still have the project that is not working? Can you create a ticket in our bugtracker and attach it so we can investigate what is happening there?
The first question is when working with Noesis, what is the best way to switch between views. Would you have multiple User controls in one main User Control and switch between them based on the data-context?
The best approach is to use UserControls for each View. The easiest way to switch between views is if you have them all in a parent control, toggle the visibility (or fade animation) to show the appropriate view.
<Grid
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  xmlns:local="clr-namespace:Testing">
  <local:MainMenuView/>
  <local:StartGameView Visibility="Collapsed"/>
  <local:SettingsView Visibility="Collapsed"/>
</Grid>
There are more advanced ways of doing this, for example, having a ContentControl where the content would be the view model associated with a View, then you will have a DataTemplate for that view model which consists in the user control that defines the view. Then navigation is handled from the ViewModel by changing ContentControl bound content.
<Grid
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  xmlns:local="clr-namespace:Testing">
  <Grid.Resources>
    <DataTemplate DataType="{x:Type local:MainMenuViewModel}">
      <local:MainMenuView/>
    </DataTemplate>
    <DataTemplate DataType="{x:Type local:StartGameViewModel}">
      <local:StartGameView/>
    </DataTemplate>
    <DataTemplate DataType="{x:Type local:SettingsViewModel}">
      <local:SettingsView/>
    </DataTemplate> 
  </Grid.Resources>
  <ContentControl Content="{Binding ActiveView}"/>
</Grid>
This keeps only one View loaded in the tree, but it also means when switching views it should load it again. You should choose which one best fits your needs.
The second question I have is whether or not there is a way to get Noesis working with Unity's new input system?
We have plans to incorporate support in a future release, here is the ticket in our bugtracker: #1828

Cheers.

Who is online

Users browsing this forum: Google [Bot] and 3 guests