lomoonmoonbird
Topic Author
Posts: 30
Joined: 15 Mar 2016, 04:41

Code-behind not work

08 Apr 2016, 09:42

Hi
I have a UserControl in the that are MyControl1.xaml and MyControl1.xaml.cs

MyControl1.xaml:
<UserControl x:Class="putaogame.usercontrols.MyControl1"
             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:putaogame.usercontrols"
             mc:Ignorable="d" 
             d:DesignHeight="300" d:DesignWidth="800">
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"></RowDefinition>
            <RowDefinition Height="Auto"></RowDefinition>
            <RowDefinition Height="Auto"></RowDefinition>
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="Auto"></ColumnDefinition>
            <ColumnDefinition Width="Auto"></ColumnDefinition>
            <ColumnDefinition Width="Auto"></ColumnDefinition>
        </Grid.ColumnDefinitions>

        <Border Grid.Row="0" Grid.Column="0" Margin="10">
            <StackPanel Orientation="Vertical">
                <StackPanel>
                    <Button Content="lbutton1" Name="lbutton1"  [color=#FF0000]KeyDown="lbutton1_KeyDown"[/color]></Button>
                    <Button Content="lbutton1" Name="lbutton2"></Button>
                    <Button Content="lbutton1" Name="lbutton3"></Button>
                    <Button Content="lbutton1" Name="lbutton4"></Button>
                    <Button Content="lbutton1" Name="lbutton5"></Button>
                    <Button Content="lbutton1" Name="lbutton6"></Button>
                    <Button Content="lbutton1" Name="lbutton7"></Button>
                    <Button Content="lbutton1" Name="lbutton8"></Button>
                    <Button Content="lbutton1" Name="lbutton9"></Button>
                </StackPanel>
            </StackPanel>
        </Border>
        <Border Grid.Row="0" Grid.Column="1" Margin="10">
            <StackPanel Orientation="Vertical">
                <StackPanel>
                    <Button Content="mbutton1" Name="mbutton1"></Button>
                    <Button Content="mbutton1" Name="mbutton2"></Button>
                    <Button Content="mbutton1" Name="mbutton3"></Button>
                    <Button Content="mbutton1" Name="mbutton4"></Button>
                    <Button Content="mbutton1" Name="mbutton5"></Button>
                    <Button Content="mbutton1" Name="mbutton6"></Button>
                    <Button Content="mbutton1" Name="mbutton7"></Button>
                    <Button Content="mbutton1" Name="mbutton8"></Button>
                    <Button Content="mbutton1" Name="mbutton9"></Button>
                </StackPanel>
            </StackPanel>
        </Border>
        <Border Grid.Row="0" Grid.Column="2" Margin="10">
            <StackPanel Orientation="Vertical">
                <StackPanel>
                    <Button Content="rbutton1" Name="rbutton1"></Button>
                    <Button Content="rbutton2" Name="rbutton2"></Button>
                    <Button Content="rbutton3" Name="rbutton3"></Button>
                    <Button Content="rbutton4" Name="rbutton4"></Button>
                    <Button Content="rbutton5" Name="rbutton5"></Button>
                    <Button Content="rbutton6" Name="rbutton6"></Button>
                    <Button Content="rbutton7" Name="rbutton7"></Button>
                    <Button Content="rbutton8" Name="rbutton8"></Button>
                    <Button Content="rbutton9" Name="rbutton9"></Button>
                </StackPanel>
            </StackPanel>
        </Border>
    </Grid>
</UserControl>

MyControl1.xaml.cs
#if UNITY_5
#define NOESIS
#endif

#if NOESIS
  using Noesis;
  using UnityEngine;
  using System;
  using System.Collections;
  using System.Windows.Input;

#else
using System;
using System.Collections;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Controls.Primitives;

#endif

namespace putaogame.usercontrols
{

    #if NOESIS
    [UserControlSource("MyControl1.xaml")]
    #endif
    public class TestControl : UserControl
    {
        //...
    }
    public partial class MyControl1 : UserControl
    {
        public MyControl1()
        {
            InitializeComponent();
            Debug.Log("this is initialize");
        }

       [color=#FF0040] private void lbutton1_KeyDown(object sender, KeyEventArgs e)
        {
            Debug.Log("sender" + sender);
            
        }[/color]
    }
}

and error

NoesisException: Loading Assets/putaogame/usercontrol/MyControl1.xaml
CodeBehind method 'lbutton1_KeyDown' not found
Noesis.Error.Check () (at Assets/Plugins/NoesisGUI/Scripts/Core/NoesisError.cs:26)
NoesisGUISystem.Noesis_LoadXAML (System.String xamlFile) (at Assets/Plugins/NoesisGUI/Scripts/Core/NoesisGUISystem.cs:406)
NoesisGUISystem.Load (System.String xamlFile) (at Assets/Plugins/NoesisGUI/Scripts/Core/NoesisGUISystem.cs:279)
NoesisGUISystem.LoadXaml (System.String xamlFile) (at Assets/Plugins/NoesisGUI/Scripts/Core/NoesisGUISystem.cs:47)
NoesisGUIPanel.LoadXaml () (at Assets/Plugins/NoesisGUI/Scripts/NoesisGUIPanel.cs:495)
NoesisGUIPanel.OnEnable () (at Assets/Plugins/NoesisGUI/Scripts/NoesisGUIPanel.cs:383)
it seems that this syntax
#if NOESIS
    [UserControlSource("MyControl1.xaml")]
    #endif
is useless,whether or not i remove this , it has no impact,it is the x:class attribute on the root does the work,which is
x:Class="putaogame.usercontrols.MyControl1"
am i missing something? thanks
 
User avatar
jsantos
Site Admin
Posts: 2904
Joined: 20 Jan 2012, 17:18
Contact:

Re: Code-behind not work

08 Apr 2016, 11:15

<Button Content="lbutton1" Name="lbutton1" KeyDown="lbutton1_KeyDown"></Button>
I am sorry to say that Code-Behind does not work in Unity. You have to manually connect to the KeyDown delegate. For now, we only support Code-Behind in user controls, but we have plans to improve this.

Thanks!

Who is online

Users browsing this forum: No registered users and 0 guests