Question about OnPostInit
Posted: 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
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
I hope i'm clear thank you.
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
Code: Select all
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 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
Code: Select all
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 () {
}
}
Code: Select all
<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>