Tobii Interaction Library SDK

[Back to the SDK start page]

C# WPF Sample Walkthrough

This section describes the typical workflow when using the C# WPF Binding API of the Interaction Library SDK.

In our WPF application XAML code, we tell the Interaction Library about the 3 rectangles in our WPF window (A, B and C in the picture below) and request that they are gaze aware, using the WPF dependency property IsGazeAware. Such a region is called an interactor in the Interaction Library API.

As the user's gaze enters and leaves the interactors, the WPF dependency property HazGaze changes, so that we can react to it in our WPF application XAML code.

The above dependency properties exists in the Tobii.InteractionLib.Wpf.Behaviors class.

A WPF window with 3 vertical interactors A, B and C:
+-----------------------------------+
| |
| +-------+ +-------+ +-------+ |
| | | | | | | |
| | | | | | | |
| | | | | | | |
| | A | | B | | C | |
| | | | | | | |
| | | | | | | |
| | | | | | | |
| +-------+ +-------+ +-------+ |
| |
+-----------------------------------+

The Interaction Library WPF host instance is kept until the application exits - the window accesses it during construction:

namespace Tobii.InteractionLib.Wpf.SampleApp
{
public partial class App : Application
{
// Exposes the Interaction Library WPF host. Makes it possible for windows (like MainWindow in this sample)
// to add themselves to the Interaction Library WPF binding of the host.
public Tobii.InteractionLib.Wpf.InteractionLibWpfHost IntlibWpf { get; private set; }

We create the host when the application starts up:

// This function is called when the application loads very first time. On startup, we instantiate the
// Interaction Library WPF host and assign it to the IntlibWpf property which can later be used by other windows.
protected override void OnStartup(StartupEventArgs e) => IntlibWpf = new Tobii.InteractionLib.Wpf.InteractionLibWpfHost();

The host is disposed to release any remaining resources held by it when the application exits:

// This function is called when the application exits.
protected override void OnExit(ExitEventArgs e)
{
// Cleans up the Interaction Library WPF host instance before the application exits.
IntlibWpf?.Dispose();
base.OnExit(e);
}
}
}

When the main application window is constructed, it adds itself to the host:

public MainWindow()
{
// Here we attach the current window to the already created instance of Interaction Library WpfBinding host.
// Note, we are doing this before calling InitializeComponent so that WpfBinding has proper values
// as soon as the window is loaded, without waiting for NotifyPropertyChanged method to be called.
((App)Application.Current).IntlibWpf?.WpfBinding?.AddWindow(this);
InitializeComponent();
}

The window XAML gets access to the dependency properties IsGazeAware and HasGaze by referring to the Tobii.InteractionLib.Wpf namespace in the assembly with the same name:

<Window x:Class="Tobii.InteractionLib.Wpf.SampleApp.MainWindow"
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"
xmlns:wpf="clr-namespace:Tobii.InteractionLib.Wpf;assembly=tobii_interaction_lib_wpf"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">

When the references are set up, the window can decribe the behaviors of the 3 interactors using IsGazeAware and HasGaze:

<Window.Resources>
<ResourceDictionary>
<Style x:Key="RectangleWithGazeAwareAnimation" TargetType="Rectangle">
<Setter Property="Fill" Value="LightSkyBlue" />
<Setter Property="Margin" Value="40, 50" />
<Setter Property="wpf:Behaviors.IsGazeAware" Value="True"></Setter>
<Style.Triggers>
<Trigger Property="wpf:Behaviors.HasGaze" Value="True">