picpic2006
Topic Author
Posts: 71
Joined: 07 Nov 2013, 15:59

Question about OnPostInit

05 Apr 2014, 14:19

Hi,

I have a question about the Onpostinit function, i wonder when it is called, i tryed to do a test with a usercontrol
and command binding.

I have this usercontrol
using UnityEngine;
using System.Collections;
using Noesis;
using System;

[Noesis.Extended]
public class ClickLstCommand : Noesis.BaseCommand
{
	////////////////////////////////////////////////////////////////////////////////////////////////////
	private readonly Action action;
	
	////////////////////////////////////////////////////////////////////////////////////////////////////
	public ClickLstCommand(Action action)
	{
		this.action = action;
	}
	
	////////////////////////////////////////////////////////////////////////////////////////////////////
	public bool CanExecuteCommand(Noesis.BaseComponent parameter)
	{
		return true;
	}
	
	////////////////////////////////////////////////////////////////////////////////////////////////////
	public void ExecuteCommand(Noesis.BaseComponent parameter)
	{
		action();
	}
}

[Noesis.Extended]
[Noesis.UserControlSource("Assets/MyGUI/PersonControlTemplate.xaml")]
public class Person: Noesis.UserControl
{

	private int id;
	public int ID { 
		get{return id;}
		set
		{
			if (id != value)
			{
				id = value; 
				NotifyPropertyChanged("ID");
			}
		}
	}

	private string username;
	public string UserName { 
		get{return username;}
		set
		{
			if (username != value)
			{
				username = value; 
				NotifyPropertyChanged("UserName");
			}
		}
	}

	private string designation;
	public string Designation { 
		get{return designation;}
		set
		{
			if (designation != value)
			{
				designation = value; 
				NotifyPropertyChanged("Designation");
			}
		}
	}

	public ClickLstCommand clickLstcommand
	{ 
		get;
		private set;
	}

	public Person()
	{
		//clickLstcommand = new ClickLstCommand(clicklistbox);
	}

	public void OnPostInit()
	{
		//clickLstcommand = new ClickLstCommand(clicklistbox);// didn't work when added to a root element
	}

	private void clicklistbox()
	{
		Debug.Log("click");
	}
}
I create a command in my postinit function, that will be called when i click on a button. I add this usercontrol to my root, when i click nothing happen it seem to not work, if i made it in my constructor it work well.
i have to made a constructor to create my command. But f i add this usercontrol to an item collection in a listbox i can make it in my onpostinit it work well in this case.
Why do i have this differ.

this is my menu controller
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using Noesis;

public class MainMenu: MonoBehaviour {

	private Noesis.Page root_;
	private ListBox personLstBox_;

	private ItemCollection peoplescollection_;
	public List<Person> peoplesLst_;

	// Use this for initialization
	void Start () {
		root_ = GetComponent<NoesisGUIPanel>().GetRoot<Noesis.Page>();

		Button _addELbtn = root_.FindName<Button>("AddEL_Btn");
		_addELbtn.Click += AddListBoxEL;

		Button _removeELbtn = root_.FindName<Button>("RemoveEL_Btn");
		_removeELbtn.Click += RemoveListBoxEL;

		personLstBox_ = root_.FindName<ListBox>("PersonLst_Box");
		personLstBox_.SelectionChanged += SelectListBoxChanged;

		peoplescollection_ = new ItemCollection();

		Person p = new Person();
		p.UserName = "robert";
		p.ID = 2;
		p.Designation = "un bof";

		root_.FindName<Grid>("Layout").GetChildren().Add(p);

	}

	void AddListBoxEL(Noesis.BaseComponent sender, Noesis.RoutedEventArgs e)
	{
		Debug.Log("add list element to :"+personLstBox_.GetName());

		for(int i = 0; i<10; i++)
		{
			Person p = new Person();
			p.UserName =  "toto "+i;
			p.Designation = "oui"+i;	
			peoplescollection_.Add(p);
		}

		personLstBox_.SetItemsSource(peoplescollection_);
	}

	void SelectListBoxChanged(Noesis.BaseComponent sender, Noesis.SelectionChangedEventArgs e)
	{
		Person selectedPerson =  personLstBox_.GetSelectedItem().As<Person>();
		Debug.Log("selection list element to :"+selectedPerson.UserName);
	}

	void RemoveListBoxEL(Noesis.BaseComponent sender, Noesis.RoutedEventArgs e)
	{
		Debug.Log("remove list element");
		//personLstBox_.GetItems().Remove(personLstBox_.GetSelectedItem().As<BaseComponent>());

	}
	
	// Update is called once per frame
	void Update () {
	
	}
}

<UserControl
	  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
	  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
	  x:Class="Person"
	  x:Name="MyControl"
	>
	
	<Grid Width="384" Height="118">
		<Rectangle Fill="#FFF4F4F5" HorizontalAlignment="Left" Stroke="Black" Width="384" Panel.ZIndex="-2"/>
		<StackPanel>
  				<TextBlock Name="ID" Text="{Binding ID, ElementName=MyControl}" Margin="0,0,5,0"/>
  				<TextBlock Name="Name" Text="{Binding UserName,ElementName=MyControl}" Margin="0,0,5,0"/>
  				<TextBlock Name="Designation" Text="{Binding Designation,ElementName=MyControl}" Margin="0,0,5,0"/>
  				<Button Content="clic" Command="{Binding clickLstcommand,ElementName=MyControl}"></Button>
  		</StackPanel>
	</Grid>
</UserControl>
I hope i'm clear thank you.
 
picpic2006
Topic Author
Posts: 71
Joined: 07 Nov 2013, 15:59

Re: Another kind of bug i though !

05 Apr 2014, 16:28

I have another error when i try to remove a item from my listbox
void RemoveListBoxEL(Noesis.BaseComponent sender, Noesis.RoutedEventArgs e)
	{
		//error
		if(personLstBox_.GetSelectedItem() != null)
		{
			Debug.Log("remove list element"+personLstBox_.GetSelectedItem().GetType());
			personLstBox_.GetItems().Remove(personLstBox_.GetSelectedItem());
		}
	}
[ 2] [0x0B1DE58F] mono.dll!mono_thread_force_interruption_checkpoint + 0x7 bytes
[ 3] [0x32E13F65] Noesis.dll!Noesis::Core::TypeClass::IsDescendantOf + 0x35 bytes
[ 4] [0x0B187AA9] mono.dll!mono_domain_get + 0xc bytes
[ 5] [0x0B24BF20] mono.dll!mono_jit_thread_attach + 0x2e bytes
[ 6] [0x3321D2A4] Noesis.dll!Noesis::Gui::IUnityDevice::Create + 0x234 bytes
[ 7] [0x3321D335] Noesis.dll!Noesis::Render::DX9FileTexture2D::GetVersion + 0x25 bytes
[ 8] [0x330A08A7] Noesis.dll!Noesis::Gui::UIElement::NotifyHandlers + 0xd7 bytes
[ 9] [0x330A3C28] Noesis.dll!Noesis::Gui::UIElement::BubblingEvent + 0x58 bytes
[10] [0x330A541B] Noesis.dll!Noesis::Gui::UIElement::OnGotFocus + 0x3b bytes
[11] [0x32F49AB3] Noesis.dll!Noesis::Gui::BaseButton::OnClick + 0x23 bytes
[12] [0x32F6E3F8] Noesis.dll!Noesis::Gui::Button::OnClick + 0x8 bytes
[13] [0x3309F3B4] Noesis.dll!Noesis::Gui::UIElement::StaticOnMouseLeftButtonUp + 0x14 bytes
[14] [0x32F839B0] Noesis.dll!Noesis::Core::TypeClass::GetBase + 0x20 bytes
[15] [0x330A083A] Noesis.dll!Noesis::Gui::UIElement::NotifyHandlers + 0x6a bytes
[16] [0x330A3C28] Noesis.dll!Noesis::Gui::UIElement::BubblingEvent + 0x58 bytes
[17] [0x330A541B] Noesis.dll!Noesis::Gui::UIElement::OnGotFocus + 0x3b bytes
[18] [0x3300D9F9] Noesis.dll!Noesis::Gui::Mouse::ButtonUp + 0xd9 bytes
[19] [0x3322D0B1] Noesis.dll!Noesis_MouseButtonUp + 0x81 bytes
[20] [0x0D405581]
[21] [0x00ED23D8] Unity.exe!Behaviour::Transfer<YAMLWrite> + 0x8361d8 bytes
[22] [0x00A9876D] Unity.exe!Behaviour::Transfer<YAMLWrite> + 0x3fc56d bytes
[23] [0x0B161E2E] mono.dll!g_free + 0x407 bytes
[24] [0x0B161DD6] mono.dll!g_free + 0x3af bytes
I can give you my exemple if you want !

Thanks
 
User avatar
sfernandez
Site Admin
Posts: 2908
Joined: 22 Dec 2011, 19:20

Re: Question about OnPostInit

07 Apr 2014, 18:27

Hi,

I have a question about the Onpostinit function, i wonder when it is called, i tried to do a test with a usercontrol
and command binding.

...

I create a command in my postinit function, that will be called when i click on a button. I add this usercontrol to my root, when i click nothing happen it seem to not work, if i made it in my constructor it work well.
i have to made a constructor to create my command. But f i add this usercontrol to an item collection in a listbox i can make it in my onpostinit it work well in this case.
Why do i have this differ.

...

I hope i'm clear thank you.
The OnPostInit method is called after xaml is loaded, attached to the UI Renderer and initialized. When this method gets called all the properties with a non-default value already notified that they changed. If you want that setting a (non DependencyProperty) property in the PostInit method correctly notifies about that change, you have to call the NotifyPropertyChanged() method as you are doing with other properties like Person.ID or Person.UserName.
private ClickLstCommand _cmd;
public ClickLstCommand ClickLstCommand
{
    get
    {
        return this._cmd;
    }
    private set
    {
        if (this._cmd != value)
        {
            this._cmd = value;
            NotifyPropertyChanged("ClickLstCommand");
        }
    }
}
I have another error when i try to remove a item from my listbox.
I can give you my exemple if you want !

Thanks
Please report this bug in the bugtracker and attach the sample so we can fix it for a next version.
 
picpic2006
Topic Author
Posts: 71
Joined: 07 Nov 2013, 15:59

Re: Question about OnPostInit

07 Apr 2014, 19:25

Great it's more clear for me.

the ticket is Done !

Thank you !

Who is online

Users browsing this forum: vogelLightword and 4 guests