Maxime FRAPPAT

Hum …no thanks ! – Lordinaire

Tag: C# (Page 2 of 6)

[UWP] Data binding to method and from event

With the Windows 10 Anniversary update, XAML becomes smarter with some great features for the data binding. It is now possible to bind to a property or an event to a method very easily !

Here is a sample of different type of bindings (old and new) to compare the XAML synthax.

ViewModel

public class MainViewModel : INotifyPropertyChanged
{
    private string _firstName;
    public string FirstName
    {
        get { return _firstName; }
        set
        {
            _firstName = value;
            OnPropertyChanged();
        }
    }

    private string _lastName;
    public string LastName
    {
        get { return _lastName; }
        set
        {
            _lastName = value;
            OnPropertyChanged();
        }
    }

    public MainViewModel()
    {
        FirstName = "Maxime";
        LastName = "Frappat";
    }

    public string ToFullName(string firstName, string lastName)
    {
        return string.Concat(firstName, " ", lastName);
    }

    public void DeleteLastName()
    {
        LastName = string.Empty;
    }

    #region INotifyPropertyChanged

    public event PropertyChangedEventHandler PropertyChanged;

    protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
    {
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));  
    }

    #endregion
}

Helper class

public static class TextHelper
{
    public static string ToFullName(string firstName, string lastName)
    {
        return string.Concat(firstName, " ", lastName);
    }
}

View (Code-Behind)

public sealed partial class MainPage : Page
{
    public MainViewModel ViewModel { get; set; }

    public MainPage()
    {
        InitializeComponent();

        ViewModel = new MainViewModel();
        DataContext = ViewModel;
    }
}

View (XAML)

<!-- BINDING TO PROPERTIES -->
<StackPanel Orientation="Vertical">
    <TextBlock Text="BINDING TO PROPERTIES" Style="{StaticResource TitleStyle}"/>

    <TextBlock>
        <Run Text="{Binding FirstName}" />
        <Run Text="{Binding LastName}" />
    </TextBlock>
</StackPanel>

<!-- xBIND TO PROPERTIES -->
<StackPanel Orientation="Vertical">
    <TextBlock Text="xBIND TO PROPERTIES" Style="{StaticResource TitleStyle}" />

    <TextBlock>
        <Run Text="{x:Bind ViewModel.FirstName, Mode=OneWay}" />
        <Run Text="{x:Bind ViewModel.LastName, Mode=OneWay}" />
    </TextBlock>
</StackPanel>

<!-- xBIND TO INTERNAL METHODS -->
<StackPanel Orientation="Vertical">
    <TextBlock Text="xBIND TO INTERNAL METHODS" Style="{StaticResource TitleStyle}" />

    <TextBlock Text="{x:Bind ViewModel.ToFullName(ViewModel.FirstName, ViewModel.LastName), Mode=OneWay}" />
</StackPanel>

<!-- xBIND TO EXTERNAL METHODS -->
<StackPanel Orientation="Vertical">
    <TextBlock Text="xBIND TO EXTERNAL METHODS" Style="{StaticResource TitleStyle}" />

    <TextBlock Text="{x:Bind helper:TextHelper.ToFullName(ViewModel.FirstName, ViewModel.LastName), Mode=OneWay}" />
</StackPanel>

<!-- xBIND FROM EVENT -->
<StackPanel Orientation="Vertical">
    <TextBlock Text="xBIND FROM EVENT" Style="{StaticResource TitleStyle}" />

    <TextBlock Tapped="{x:Bind ViewModel.DeleteLastName}">
        <Run Text="{x:Bind ViewModel.FirstName, Mode=OneWay}" />
        <Run Text="{x:Bind ViewModel.LastName, Mode=OneWay}" />
    </TextBlock>
</StackPanel>

Result

Each bindings will display the same text.

anniverary_binding

Don’t forget to target the last SDK for your project.

So easy !

[Unity] Using Tobii EyeX Controller to interact with your game

Tobii EyeX Controller is an awesome product who give you the power with… your eyes ! I’m not joking, you can control things with your eyes and it works pretty well :) The firm who  product this controller also made a SDK for Unity so let’s play with him !Tobii_Device

How it works ?

With several sensors, the controller is able to track your eyes position to know where you are looking at. You just need to put the device at the bottom of your screen and to calibrate it! Ready to rock ?

Example

Create a new project in Unity  (any recent version will work).

Download the SDK

You can find the SDK here : http://developer.tobii.com/downloads/

unity_download_sdk

 Install the package

Unzip the downloaded file and double click on EyeXFramework.unityPackage. This will open a popup to import the package in your Unity project.

Interactive sphere

Select GameObject > Create Other > Sphere.

On that sphere, we will add 3 components :

  • Gaze Aware (from Tobii SDK)
  • Particle system
  • Custom script to show particles only when you look at the sphere

Gaze Aware

Select the sphere and in the Inspector panel, select Add component > Tobii EyeX > Gaze Aware. This script sets the sphere as reactive to the sensor, we can now know when the player focus the sphere.

Particle system

Select the sphere and in the Inspector panel, select Add component > Effects >Particle System. Set the property Start Lifetime to 1 and disable the property Emission.

Custom script

Once again, select the sphere and in the Inspector panel, select Add component > New script > Csharp > Create and Add. Open the script and copy this code snippet inside :

using UnityEngine;
using System.Collections;

public class ParticleBehavior : MonoBehaviour
{
	private ParticleSystem _particleComponent;
	private GazeAwareComponent _gazeAwareComponent;

	// Use this for initialization
	void Start ()
	{
		_particleComponent = GetComponent<ParticleSystem>();
		_gazeAwareComponent = GetComponent<GazeAwareComponent>();
	}

	// Update is called once per frame
	void Update ()
	{
		if (_gazeAwareComponent.HasGaze)
		{
			_particleComponent.enableEmission = true;
		}
		else
		{
			_particleComponent.enableEmission = false;
		}
	}
}

We use the property HasGaze() of GazeAwareComponent to determinate whether the sphere is focus or not. The property enableEmission of the class ParticleSystem is use to active the particle system.

unity_particles_playing

 

Have fun !

Page 2 of 6

Powered by WordPress & Theme by Anders Norén

%d bloggers like this: