Grandpa3D
Topic Author
Posts: 4
Joined: 15 Jun 2019, 02:33

Unity and code-behind question

21 Jun 2019, 15:10

I was trying to see if I could get a button to work in Unity using code-behind. I found the documentation section on Events to be very useful, but it did leave a couple of questions in my mind. But after a little more research I thought I got the basics of it. The code compiles fine in Blend but I get this error in Unity: [noesis] Assets/Interface/MainWindow.xaml(11): 'Interface.MainWindow' does not contain a definition for 'Settings'
I am sure I have made a really basic error, but I just don't see it ( Letting the amateur in me shine through). Anyway here is my code.

MainWwindow.xaml
<UserControl x:Class="Interface.MainWindow"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
             xmlns:local="clr-namespace:Interface"
             mc:Ignorable="d" 
             d:DesignHeight="300" d:DesignWidth="300" Width="1920" Height="1080">
 
    <Grid Background="#FF6298A8">
        <Button x:Name="SettingsBtn" Content="Button" Height="68" Margin="820,176,834,0" VerticalAlignment="Top" Click="Settings"/>
    </Grid>
</UserControl>


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 Interface
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : UserControl
    {
        public MainWindow()
        {
            InitializeComponent();
        }

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

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

            return false;
        }
#endif
        private void Settings(object sender, RoutedEventArgs e)
        {
            //Console.WriteLine("Settings button was clicked");
        }
    } 
}

Tags:
 
User avatar
jsantos
Site Admin
Posts: 2905
Joined: 20 Jan 2012, 17:18
Contact:

Re: Unity and code-behind question

24 Jun 2019, 14:51

You need to return true when the event is being handled.
protected override bool ConnectEvent(object source, string eventName, string handlerName)
{
    if (eventName == "Click" && handlerName == "Settings")
    {
        ((Button)source).Click += this.Settings;
        return true;
    }

    return false;
}
 
Grandpa3D
Topic Author
Posts: 4
Joined: 15 Jun 2019, 02:33

Re: Unity and code-behind question

25 Jun 2019, 02:20

Thank you
 
User avatar
sfernandez
Site Admin
Posts: 1915
Joined: 22 Dec 2011, 19:20

Re: Unity and code-behind question

25 Jun 2019, 10:56

Marked this as solved.

Who is online

Users browsing this forum: No registered users and 0 guests