- XaeroDegreaz
- Posts: 29
- Joined:
Unity tutorials seems to have invalid attributes listed for UserControl
On this tutorial: http://www.noesisengine.com/docs/Gui.Co ... unity.html
It mentions setting up a class like this:
however those attribute classes don't seem to exist in the 2.0.0 unity package I just downloaded.
I Looked through some of the sample classes that are bundled with the package and it seems they are using
to set their classes up?
What should I do? Is the documentation correct or not?
It mentions setting up a class like this:
Code: Select all
[Noesis.Extended]
[Noesis.UserControlSource("Assets/Test/ColorPicker.xaml")]
public class ColorPicker : Noesis.UserControl
I Looked through some of the sample classes that are bundled with the package and it seems they are using
Code: Select all
void InitializeComponent()
{
Noesis.GUI.LoadComponent(this, "Assets/NoesisGUI/Samples/Login/LoginWindow.xaml");
}
What should I do? Is the documentation correct or not?
- XaeroDegreaz
- Posts: 29
- Joined:
Re: Unity tutorials seems to have invalid attributes listed for UserControl
I seem to also be having problems getting Noesis to work out of the box. The code-behind doesn't seem to work properly.
I have a button with a "Click" event on it, and the method on the code-behind, but it doesn't fire. I can see that the OnInitialized method does fire, so it appears that the code-behind class is being constructed properly.
I also had problems naming the button, and manually registering the click event in the code. I'm sure there's some sort of set-up problem. Can anyone assist me?
I have a button with a "Click" event on it, and the method on the code-behind, but it doesn't fire. I can see that the OnInitialized method does fire, so it appears that the code-behind class is being constructed properly.
I also had problems naming the button, and manually registering the click event in the code. I'm sure there's some sort of set-up problem. Can anyone assist me?
Code: Select all
<UserControl
x:Class="KazBall.Ui.StartScreen"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:KazBall.Ui"
Width="800" Height="600">
<Grid>
<Button
Content="Button"
HorizontalAlignment="Left"
Margin="361,296,0,0"
VerticalAlignment="Top"
Width="75"
Click="OnButtonClick" />
</Grid>
</UserControl>
Code: Select all
using Noesis;
using UnityEngine;
namespace KazBall.Ui
{
public class StartScreen : UserControl
{
public StartScreen()
{
Initialized += OnInitialized;
Noesis.GUI.LoadComponent( this, "Assets/Ui/StartScreen.xaml" );
}
private void OnInitialized( object sender, EventArgs eventArgs )
{
Debug.Log( "OnInitialized" );
}
public void OnButtonClick( object sender, RoutedEventArgs routedEventArgs )
{
Debug.Log( "CLICKED" );
}
}
}
- ai_enabled
- Posts: 231
- Joined:
- Contact:
Re: Unity tutorials seems to have invalid attributes listed for UserControl
This Unity tutorial is outdated - there are no need for these attributes in NoesisGUI 2.0. You need to use Noesis.GUI.LoadComponent() method to load the XAML for your user control.
Code-behind generation in NoesisGUI is not yet released but was promised by the NoesisGUI team (perhaps coming in 2.0.1?) . When it will be released, there is also will be no need to call Noesis.GUI.LoadComponent(xamlPath) as it will be done automatically in the generated code-behind.
So you cannot use event handlers defined in XAML now. You need to manually register the event in code.
Code-behind generation in NoesisGUI is not yet released but was promised by the NoesisGUI team (perhaps coming in 2.0.1?) . When it will be released, there is also will be no need to call Noesis.GUI.LoadComponent(xamlPath) as it will be done automatically in the generated code-behind.
So you cannot use event handlers defined in XAML now. You need to manually register the event in code.
AtomicTorch Studio Pte. Ltd. http://atomictorch.com
- XaeroDegreaz
- Posts: 29
- Joined:
Re: Unity tutorials seems to have invalid attributes listed for UserControl
Thanks for the information.
So basically the Click attributes don't work, and we have to use the Command attribute, and define the delegates in our view models for now?
What about accessing elements objects in the code-behind as a property field by their x:Name? Is that also coming in the future?
So basically the Click attributes don't work, and we have to use the Command attribute, and define the delegates in our view models for now?
What about accessing elements objects in the code-behind as a property field by their x:Name? Is that also coming in the future?
- ai_enabled
- Posts: 231
- Joined:
- Contact:
Re: Unity tutorials seems to have invalid attributes listed for UserControl
Yes, we prefer to use MVVM approach when possible so each user control has a view model (which is provided to this.DataContext during initialization) and define properties and commands there.
There is no code-behind generation right now. It means there is no mirror-representation of XAML named objects in partial C# file. You have to access all the controls you need by their names by using FindName(name) method. Then you can hook up the event handlers.
There is no code-behind generation right now. It means there is no mirror-representation of XAML named objects in partial C# file. You have to access all the controls you need by their names by using FindName(name) method. Then you can hook up the event handlers.
AtomicTorch Studio Pte. Ltd. http://atomictorch.com
Re: Unity tutorials seems to have invalid attributes listed for UserControl
Sorry for the confusion about this. The documentation is not updated properly for v2.0, we are working on that. We recommend having a look at our provided examples:
As you can see this is exactly the same code that is generated by Blend. We plan to also provide this automatically in the future. For now, it must be done manually. Regarding events, I recommend reading the following tutorial:
http://www.noesisengine.com/docs/Gui.Co ... orial.html
And yes, for elements marked with x:Name you need to manually do the FindName() and assign to member variables.
Code: Select all
namespace Noesis.Samples
{
/// <summary>
/// Interaction logic for LoginWindow.xaml
/// </summary>
public partial class LoginWindow : UserControl
{
public LoginWindow()
{
this.Initialized += OnInitialized;
this.InitializeComponent();
}
void InitializeComponent()
{
Noesis.GUI.LoadComponent(this, "Assets/NoesisGUI/Samples/Login/LoginWindow.xaml");
}
private void OnInitialized(object sender, EventArgs args)
{
this.DataContext = new LoginViewModel();
}
}
}
http://www.noesisengine.com/docs/Gui.Co ... orial.html
And yes, for elements marked with x:Name you need to manually do the FindName() and assign to member variables.
- XaeroDegreaz
- Posts: 29
- Joined:
Re: Unity tutorials seems to have invalid attributes listed for UserControl
So if we are supposed to use the view model for hooking our UI bindings / events up, what is the best practice for passing in other UI dependencies?
Just
for every UI element that we plan on interacting with? Are there any other recommended or best-practices for this?
Also, since we use the DataContext for all of the important stuff, what use is the main class? Doe anything ever really go in there? Is it mostly for just initializing the user interface? What are some best practices for the type of code that would go in this class?
Thanks for all of your help, I'm well on my way now!
Just
Code: Select all
Initialized += ( sender, args ) =>
{
DataContext = new MyScreenViewModel
{
MyViewDependency = FindName( "MyView" ) as MyViewType
};
};
Noesis.GUI.LoadComponent(this, "Assets/Ui/MyScreen.xaml");
Also, since we use the DataContext for all of the important stuff, what use is the main class? Doe anything ever really go in there? Is it mostly for just initializing the user interface? What are some best practices for the type of code that would go in this class?
Thanks for all of your help, I'm well on my way now!
-
sfernandez
Site Admin
- Posts: 2991
- Joined:
Re: Unity tutorials seems to have invalid attributes listed for UserControl
Hi,
You can follow 2 approaches to interact with UI elements:
- Use code-behind class to manage all the logic. In that case you will specify a name for the elements you want to access, so they can be found in the InitializeComponent, and event handlers implemented in the code-behind. For example:
- Or you can use a MVVM pattern to separate logic from UI completely:
You can follow 2 approaches to interact with UI elements:
- Use code-behind class to manage all the logic. In that case you will specify a name for the elements you want to access, so they can be found in the InitializeComponent, and event handlers implemented in the code-behind. For example:
Code: Select all
<UserControl x:Class="Test.TestControl"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Grid>
<Rectangle x:Name="rect" Margin="200" Fill="Red"/>
<Button HorizontalAlignment="Center" VerticalAlignment="Bottom" Margin="10" Content="Change color" Click="ChangeColor"/>
</Grid>
</UserControl>
Code: Select all
#if UNITY_5_3_OR_NEWER
#define NOESIS
using Noesis;
#else
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
#endif
namespace Test
{
/// <summary>
/// Interaction logic for TestControl.xaml
/// </summary>
public partial class TestControl : UserControl
{
public TestControl()
{
InitializeComponent();
}
#if NOESIS
Rectangle rect;
private void InitializeComponent()
{
Noesis.GUI.LoadComponent(this, "Assets/UI/TestControl.xaml");
this.rect = FindName<Rectangle>("rect");
}
protected override void Connect(object source, string eventName, string handlerName)
{
if (eventName == "Click" && handlerName == "ChangeColor")
{
((Button)source).Click += this.ChangeColor;
}
}
#endif
private void ChangeColor(object sender, RoutedEventArgs e)
{
if (this.rect.Fill == Brushes.Blue)
{
this.rect.Fill = Brushes.Red;
}
else
{
this.rect.Fill = Brushes.Blue;
}
}
}
}
Code: Select all
<UserControl x:Class="Test.TestControl"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Grid>
<Rectangle Margin="200" Fill="{Binding Color}"/>
<Button HorizontalAlignment="Center" VerticalAlignment="Bottom" Margin="10" Content="Change color" Command="{Binding ChangeColor}"/>
</Grid>
</UserControl>
Code: Select all
#if UNITY_5_3_OR_NEWER
#define NOESIS
using Noesis;
#else
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
#endif
using System.ComponentModel;
namespace Test
{
public class ViewModel: INotifyPropertyChanged
{
public ViewModel()
{
ChangeColor = new Noesis.Samples.DelegateCommand(this.OnChangeColor);
}
private SolidColorBrush color;
public SolidColorBrush Color
{
get { return color; }
set
{
if (color != value)
{
color = value;
if (PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs("Color"));
}
}
}
public Noesis.Samples.DelegateCommand ChangeColor { get; private set; }
private void OnChangeColor(object param)
{
if (Color == Brushes.Blue)
{
Color = Brushes.Red;
}
else
{
Color = Brushes.Blue;
}
}
public event PropertyChangedEventHandler PropertyChanged;
}
/// <summary>
/// Interaction logic for TestControl.xaml
/// </summary>
public partial class TestControl : UserControl
{
public TestControl()
{
InitializeComponent();
// this context may be set outside from application logic and inherited here
this.DataContext = new ViewModel { Color = Brushes.Red };
}
#if NOESIS
private void InitializeComponent()
{
Noesis.GUI.LoadComponent(this, "Assets/UI/TestControl.xaml");
}
#endif
}
}
Who is online
Users browsing this forum: Bing [Bot], Semrush [Bot] and 4 guests