Unity and code-behind question
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
Mainwindow.xaml.cs
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
Code: Select all
<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
Code: Select all
#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");
}
}
}
Re: Unity and code-behind question
You need to return true when the event is being handled.
Code: Select all
protected override bool ConnectEvent(object source, string eventName, string handlerName)
{
if (eventName == "Click" && handlerName == "Settings")
{
((Button)source).Click += this.Settings;
return true;
}
return false;
}
-
sfernandez
Site Admin
- Posts: 2991
- Joined:
Re: Unity and code-behind question
Marked this as solved.