Page 1 of 1

Connect events issue

Posted: 08 Feb 2018, 14:28
by Paxew
Hi

I can't get the button events working as in the third tutorial on events found here:
https://www.noesisengine.com/docs/Gui.C ... orial.html

I'm trying to use the code-behind approach in C# and I'm running on the 2.1.0rc2 release. When I try to connect the events using the suggested Connect method, see below:
protected override void Connect(object source, string eventName, string handlerName)
{
    if (eventName == "Click" && handlerName == "OnButtonClick")
    {
        ((Button)source).Click += this.OnButtonClick;
    }
}
But I do get the following error message.

error CS0115: `SpaceGui.Assets.Gui.InventoryGrid.Connect(object, string, string)' is marked as an override but no suitable method found to override

Is this approach still valid in the newest release?

Re: Connect events issue

Posted: 08 Feb 2018, 14:38
by jsantos
Yes, sorry, we did a few changes in the last version and the C# documentation is not yet updated (and also the samples for that part, we are working on it). The correct signature is (same as in C++):
protected virtual bool ConnectEvent(object source, string eventName, string handlerName)
{
    if (eventName == "Click" && handlerName == "OnButtonClick")
    {
        ((Button)source).Click += this.OnButtonClick;
        return true;
    }
    
    return false;
}

Re: Connect events issue

Posted: 08 Feb 2018, 15:13
by Paxew
Yes, sorry, we did a few changes in the last version and the C# documentation is not yet updated (and also the samples for that part, we are working on it). The correct signature is (same as in C++):
protected virtual bool ConnectEvent(object source, string eventName, string handlerName)
{
    if (eventName == "Click" && handlerName == "OnButtonClick")
    {
        ((Button)source).Click += this.OnButtonClick;
        return true;
    }
    
    return false;
}

Thanks that got rid of the error but now I get another one instead:
'SpaceGui.Assets.Gui.InventoryGrid' does not contain a definition for 'OnButtonClick'

I'm not too familiar with this framework yet so I'm probably doing something wrong so I posted the code that throws the error below below.
#if UNITY_STANDALONE || UNITY_ANDROID || UNITY_IOS || UNITY_WINRT_8_1
#define UNITY
#endif

#if UNITY
using Noesis;
using UnityEngine;
#else
using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
#endif
using System.Linq;
using System.Collections.ObjectModel;
using DataModels;

namespace SpaceGui.Assets.Gui
{
    /// <summary>
    /// Interaction logic for InventoryGrid.xaml
    /// </summary>
    public partial class InventoryGrid : UserControl
    {
        ObservableCollection<InventoryContentDto> _items;
        GameUiDataModel _model;

        public InventoryGrid()
        {
            this.Initialized += OnInitialized;
            InitializeComponent();
        }

#if UNITY
        void InitializeComponent()
        {
            Noesis.GUI.LoadComponent(this, "Assets/Gui/InventoryGrid/InventoryGrid.xaml");
        }
#endif

        private void OnInitialized(object sender, EventArgs e)
        {
            

#if UNITY
            GameManager.Instance.RegisterUI(this);
#endif
        }

        public void SetInventory(ObservableCollection<InventoryContentDto> items)
        {
            _items = items;

            _model = new GameUiDataModel(_items);
            this.DataContext = _model;
        }

        public Visibility IsInventoryVisible
        {
            get { return _model.IsInventoryVisible; }
            set { _model.IsInventoryVisible = value; }
        }

        protected virtual bool ConnectEvent(object source, string eventName, string handlerName)
        {
#if UNITY
            Debug.Log("Connecting Events");
#endif
            if (eventName == "Click" && handlerName == "OnButtonClick")
            {
                ((Button)source).Click += this.OnButtonClick;
                return true;
            }

            return false;
        }
        
        public void OnButtonClick(object sender, RoutedEventArgs args)
        {
            var btn = (Button)sender;
#if UNITY
            Debug.Log("Button was clicked"+btn.Name);
#endif
            System.Console.WriteLine("Button was clicked");
        }
        
    }
}


<UserControl x:Class="SpaceGui.Assets.Gui.InventoryGrid"
             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:SpaceGui.Assets.Gui.InventoryGrid"
             mc:Ignorable="d" 
             d:DesignHeight="300" d:DesignWidth="300" Width="Auto">

    <UserControl.Resources>
        <Style TargetType="GridViewColumnHeader">
            <Setter Property="HorizontalContentAlignment" Value="Stretch" />
            <Setter Property="VerticalContentAlignment" Value="Stretch" />
            <Setter Property="Padding" Value="5,4,25,4"/>
        </Style>
        <Style x:Key="HeaderRight">
            <Setter Property="GridViewColumnHeader.HorizontalContentAlignment" Value="Right"/>
            <Setter Property="GridViewColumnHeader.Padding" Value="10,0"/>
        </Style>
    </UserControl.Resources>

    <DockPanel LastChildFill="True" Visibility="{Binding IsInventoryVisible}">
        <Label TextElement.FontFamily="../Fonts/#Source Sans Pro" TextElement.FontSize="20" TextElement.FontWeight="Bold" DockPanel.Dock="Top" Content="{Binding LocalInventoryText}"/>
        <StackPanel DockPanel.Dock="Left">
            <ListBox TextElement.FontFamily="../Fonts/#Source Sans Pro">
                <ListBoxItem>All</ListBoxItem>
                <ListBoxItem>Resources</ListBoxItem>
                <ListBoxItem>Refined Resources</ListBoxItem>
                <ListBoxItem>Crafting Material</ListBoxItem>
            </ListBox>
            <Button x:Name="TestBtn" Content="Test" Click="OnButtonClick"></Button>
        </StackPanel>


        <ListView x:Name="ListItems" ItemsSource="{Binding Items}" TextElement.FontFamily="../Fonts/#Source Sans Pro">
            <ListView.View>
                <GridView>
                    <GridViewColumn Header="Name" Width="Auto">
                        <GridViewColumn.CellTemplate>
                            <DataTemplate>
                                <TextBlock Text="{Binding Item.Name}" TextAlignment="Left" Padding="5,0,25,0"/>
                            </DataTemplate>
                        </GridViewColumn.CellTemplate>
                    </GridViewColumn>

                    <GridViewColumn Header="Weight" Width="Auto" HeaderContainerStyle="{StaticResource HeaderRight}">
                        <GridViewColumn.CellTemplate>
                            <DataTemplate>
                                <TextBlock Text="{Binding Item.Weight}" TextAlignment="Right" Padding="10,0"/>
                            </DataTemplate>
                        </GridViewColumn.CellTemplate>
                    </GridViewColumn>

                    <GridViewColumn Header="Count" Width="Auto" HeaderContainerStyle="{StaticResource HeaderRight}">
                        <GridViewColumn.CellTemplate>
                            <DataTemplate>
                                <TextBlock Text="{Binding Count}" TextAlignment="Right" Padding="10,0"/>
                            </DataTemplate>
                        </GridViewColumn.CellTemplate>
                    </GridViewColumn>
                    <GridViewColumn Header="Type" Width="200" DisplayMemberBinding="{Binding Item.ItemType}"/>
                </GridView>
            </ListView.View>
        </ListView>
    </DockPanel>


</UserControl>


Re: Connect events issue

Posted: 09 Feb 2018, 20:42
by sfernandez
Is the ConnectEvent being called at all? Could you set a breakpoint there?

Looking at your code I see you are not using the override keyword, you must use it instead of virtual, otherwise you are hidding base class virtual function and yours is not probably called by our framework:
protected override bool ConnectEvent(object source, string eventName, string handlerName)