Sample noesis test with simple button
Hi,
I'm a beginner in noesis. I'm trying to run this c#-code and unity3d below. It's a sample from the noesis dokumentation.
It throws this error:
InvalidCastException: Specified cast is not valid.
test.Start () (at Assets/Test01/test.cs:12)
I added Noesis to use Noesis.Grid.
Whats wrong with this cast? Hope anybody can help me.
I'm a beginner in noesis. I'm trying to run this c#-code and unity3d below. It's a sample from the noesis dokumentation.
It throws this error:
InvalidCastException: Specified cast is not valid.
test.Start () (at Assets/Test01/test.cs:12)
Code: Select all
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Noesis;
public class test : MonoBehaviour
{
// Start is called before the first frame update
void Start()
{
Noesis.Grid root = (Noesis.Grid) Noesis.GUI.LoadXaml("Assets/Test01/test.xaml"); // <<< throws error
Button button = (Button)root.FindName("button");
button.Click += (object sender, RoutedEventArgs args) =>
{
System.Console.WriteLine("Button was clicked");
};
}
// Update is called once per frame
void Update()
{
}
}
Whats wrong with this cast? Hope anybody can help me.
Re: Sample noesis test with simple button
I would rather use this approach:Hi,
I'm a beginner in noesis. I'm trying to run this c#-code and unity3d below. It's a sample from the noesis dokumentation.
It throws this error:
InvalidCastException: Specified cast is not valid.
test.Start () (at Assets/Test01/test.cs:12)
I added Noesis to use Noesis.Grid.Code: Select allusing System.Collections; using System.Collections.Generic; using UnityEngine; using Noesis; public class test : MonoBehaviour { // Start is called before the first frame update void Start() { Noesis.Grid root = (Noesis.Grid) Noesis.GUI.LoadXaml("Assets/Test01/test.xaml"); // <<< throws error Button button = (Button)root.FindName("button"); button.Click += (object sender, RoutedEventArgs args) => { System.Console.WriteLine("Button was clicked"); }; } // Update is called once per frame void Update() { } }
Whats wrong with this cast? Hope anybody can help me.
- create xaml, with/without code-behind
- attach noesis view component to GameObject, pick previously created xaml
- manipulate controls/set DataContext from script
Example from my old project (WARNING bad coding practices)
Code: Select all
public class StatsHUD_Presenter : MonoBehaviour {
private PlayerPawn playerPawn;
private Inventory playerInventory;
private FrameworkElement root;
private ProgressBar healthBar;
private TextBlock currentWeight;
private TextBlock maxWeight;
[Inject]
public void Construct(
PlayerPawn playerPawn,
Inventory playerInventory
) {
this.playerPawn = playerPawn;
this.playerInventory = playerInventory;
}
private void Start() {
root = GetComponent<NoesisView>().Content;
healthBar = (ProgressBar)root.FindName("HealthBar");
currentWeight = (TextBlock)root.FindName("CurrentWeight");
maxWeight = (TextBlock)root.FindName("MaxWeight");
InitSubscribes();
InitWeight();
}
private void InitSubscribes() {
SetProgressBarSubscribes(playerPawn.Health, healthBar);
}
private void InitWeight() {
currentWeight.Text = playerInventory.Weight.Current.Value.ToString();
maxWeight.Text = playerInventory.Weight.Max.Value.ToString();
playerInventory.Weight.Current
.Subscribe(newWeight => {
currentWeight.Text = newWeight.ToString();
})
.AddTo(this);
playerInventory.Weight.Max
.Subscribe(newMaxWeight => {
maxWeight.Text = newMaxWeight.ToString();
})
.AddTo(this);
}
private void SetProgressBarSubscribes(NumericalStat numStat, ProgressBar progBar) {
numStat.Min
.Subscribe(newMin => {
progBar.Minimum = newMin;
})
.AddTo(this);
numStat.Current
.Subscribe(newCurrent => {
progBar.Value = newCurrent;
})
.AddTo(this);
numStat.Max
.Subscribe(newMax => {
progBar.Maximum = newMax;
})
.AddTo(this);
}
}
Code: Select all
<Page
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
HorizontalAlignment="Left"
VerticalAlignment="Top"
mc:Ignorable="d">
<Viewbox
Width="100"
Height="75"
HorizontalAlignment="Stretch"
VerticalAlignment="Stretch">
<Grid Width="100" Height="75">
<Grid.RowDefinitions>
<RowDefinition />
<RowDefinition />
</Grid.RowDefinitions>
<Grid.Background>
<SolidColorBrush Opacity="0.5" Color="Black" />
</Grid.Background>
<ProgressBar
Name="HealthBar"
Grid.Row="0"
Margin="5"
Foreground="Red"
Value="50" />
<StackPanel
Name="CarryWeight"
Grid.Row="4"
HorizontalAlignment="Center"
VerticalAlignment="Center"
Orientation="Horizontal">
<TextBlock Foreground="White" Text="Weight : " />
<TextBlock
Name="CurrentWeight"
Foreground="White"
Text="12.5" />
<TextBlock Foreground="White" Text="/" />
<TextBlock
Name="MaxWeight"
Foreground="White"
Text="25.5" />
</StackPanel>
</Grid>
</Viewbox>
</Page>
-
sfernandez
Site Admin
- Posts: 2984
- Joined:
Re: Sample noesis test with simple button
I guess you extracted that code from the Events tutorial, right?It's a sample from the noesis dokumentation
We should probably add another code block for Unity like this to avoid confusion:
Code: Select all
NoesisView view = GetComponent<NoesisView>();
Button button = (Button)view.Content.FindName("button");
button.Click += (object sender, RoutedEventArgs args) =>
{
UnityEngine.Debug.Log("Button was clicked");
};
Please let me know if that works for you.
Re: Sample noesis test with simple button
Thanks for the answers.
It works!! I used parts from tkaczz sample, this was very helpful. thanks a lot to you. @tkaczz
Of course I'm still trying the 2nd code, but only tonight.
But thanks for that too. #by sfernandez
It works!! I used parts from tkaczz sample, this was very helpful. thanks a lot to you. @tkaczz
Of course I'm still trying the 2nd code, but only tonight.
But thanks for that too. #by sfernandez
Re: Sample noesis test with simple button
You welcome. Thanks for your feedback. Marking this as solved.
Who is online
Users browsing this forum: No registered users and 58 guests