Spiix
Topic Author
Posts: 2
Joined: 22 Sep 2022, 09:36

Namespace prefix not declared

30 Dec 2024, 17:58

Hello,

I am experimenting with NoesisGUI in C# with Unity, just following the first step guide right now and I have hit a snag with trying to show a secondary UserControl as described here:
https://www.noesisengine.com/docs/Gui.C ... aml-assets

So I have the auto-generated main view

XAML
<UserControl x:Class="Plundersea.PlunderseaMainView"
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
> 
  
  <Border Background="DodgerBlue" HorizontalAlignment="Center" VerticalAlignment="Center" Padding="50,20">
    <StackPanel>
      <TextBlock Text="Hello World" FontSize="30" Foreground="White"/>
      <Button Content="Button" Margin="0,10,0,0"/>
      <local:CrewInfo />
    </StackPanel>
  </Border>

</UserControl>
C#
#if UNITY_5_3_OR_NEWER
#define NOESIS
using Noesis;
#else
using System;
using System.Windows.Controls;
#endif

namespace Plundersea
{
    /// <summary>
    /// Interaction logic for PlunderseaMainView.xaml
    /// </summary>
    public partial class PlunderseaMainView : UserControl
    {
        public PlunderseaMainView()
        {
            InitializeComponent();
        }

#if NOESIS
        private void InitializeComponent()
        {
            NoesisUnity.LoadComponent(this);
        }
#endif
    }
}
And a custom control (made through tools -> NoesisGUI -> Create -> UserControl

XAML
<UserControl
   xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
   xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
   x:Class="Plundersea.CrewInfo"
   x:Name="CrewInfo"
>

  
  <StackPanel Orientation="Horizontal" HorizontalAlignment="Center" VerticalAlignment="Center">
    <Button Content="Click me" Click="Button_Click"/>
    <TextBlock Text="{Binding Counter, ElementName=Root, StringFormat='Button clicked {0} time(s)'}" Margin="5,0,0,0" VerticalAlignment="Center"/>
  </StackPanel>
</UserControl>
C#
#if UNITY_5_3_OR_NEWER
    #define NOESIS
    using Noesis;
#else
    using System.Windows;
    using System.Windows.Controls;
#endif

namespace Plundersea
{
    public partial class CrewInfo: Noesis.UserControl
    {
        public CrewInfo()
        {
            InitializeComponent();
        }

        public int Counter
        {
            get { return (int)GetValue(CounterProperty); }
            set { SetValue(CounterProperty, value); }
        }

        public static readonly DependencyProperty CounterProperty =  DependencyProperty.Register(
            "Counter", typeof(int), typeof(CrewInfo), new PropertyMetadata(0));

    #if NOESIS
        protected override bool ConnectEvent(object source, string eventName, string handlerName)
        {
            if (eventName == "Click" && handlerName == "Button_Click")
            {
                ((Button)source).Click += this.Button_Click;
                return true;
            }

            return false;
        }

        private void InitializeComponent()
        {
            NoesisUnity.LoadComponent(this);
        }
    #endif

        private void Button_Click(object sender, RoutedEventArgs args)
        {
            Counter++;
        }
    };
}
The only changes from the auto generated code are:
- I changed the namespace for 'CrewInfo' from 'Testing' to 'Plundersea'
- I added '<local:CrewInfo />' to PlunderseaMainView.xaml

Now it seems CrewInfo is not able to be found because I get an error "Namespace prefix not declared" in both Unity and VS Code.
I tried changing 'local:' to 'Plundersea:' to no avail and now I have run out of ideas. Checking the samples also does not help identifying what I am doing wrong.
Could someone point me in the right direction please?
 
User avatar
sfernandez
Site Admin
Posts: 3363
Joined: 22 Dec 2011, 19:20

Re: Namespace prefix not declared

30 Dec 2024, 18:49

Hello,

You just need to add the namespace definition for using the corresponding prefix:
<UserControl x:Class="Plundersea.PlunderseaMainView"
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  xmlns:local="clr-namespace:Plundersea">
  ...
  <local:CrewInfo />
  ...
</UserControl>
 
Spiix
Topic Author
Posts: 2
Joined: 22 Sep 2022, 09:36

Re: Namespace prefix not declared

02 Jan 2025, 13:28

That was the missing key.
Thank you very much! :)

Who is online

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