none
Does KStudio.CreateClient() kill Bindings? RRS feed

  • Question

  • Hello,

    i have a very interesting behavior concerning bindings. I have a simple binding where a property of a class is bound to a property of the GUI. Everything is working fine. The binding has nothing to do with kinect stuff, just a simple binding. Adding KStudio.CreateClient() from Microsoft.Kinect.Tools to the code causes the binding to stop working.

    Does anybody know why this happens? Any solution keep normal binding behavior?

    Here is a minimalistic example causing this behavior / error.

    MainWindow.xaml:

    // just a window having a progressbar

    <Window x:Class="WpfApplication1.MainWindow"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            Title="MainWindow" Height="350" Width="525">
        <Grid>
            <ProgressBar Name="bufferProgress" Width="100" Height="500" Orientation="Vertical"/>
        </Grid>
    </Window>

    MAinWindow.xaml.cs

    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Data;

    namespace WpfApplication1
    {
      
        public partial class MainWindow : Window
        {
            public MainWindow()
            {
                this.DataContext = this;
                InitializeComponent();

                bufferProgress.Maximum = 100;

                Recorder rec = new Recorder();

                // bind the progressBar value to a value of a class

                Binding bufferBinding = new Binding("bufferSize");
                bufferBinding.Source = rec;
                bufferBinding.Mode = BindingMode.OneWay;
                bufferProgress.SetBinding(ProgressBar.ValueProperty, bufferBinding);
            }
        }
    }

    Recorder.cs

    // a class doing some stuff.

    using Microsoft.Kinect.Tools;
    using System;
    using System.ComponentModel;
    using System.Runtime.CompilerServices;
    using System.Threading;

    namespace WpfApplication1
    {
        class Recorder: INotifyPropertyChanged
        {
            Timer myTimer;  // a timer that changes the bound value every few milis
            KStudioClient kStudioClient;
            KStudioEventStreamSelectorCollection streamCollection;
            public KStudioRecording recording;

            private uint _bufferSize;
            public uint bufferSize
            {
                get
                {
                    return _bufferSize;
                }
                set
                {
                    _bufferSize = value;
                    NotifyPropertyChanged("bufferSize");
                }
            }


            public Recorder()
            {
                // commenting the next line and everything works fine, uncommenting it an the binding is lost, or may only the property changed event???

                kStudioClient = KStudio.CreateClient();

                // changing a value from time to time (usually there is more logic in changing the value, here ist just to shorten the example)
                myTimer = new Timer((s) => { bufferSize = (uint)new Random((int)DateTime.Now.Ticks).Next(100); }, null, 0, 100);
            }


            public event PropertyChangedEventHandler PropertyChanged;
            private void NotifyPropertyChanged([CallerMemberName] String propertyName = "")
            {
                if (PropertyChanged != null)
                {
                    PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
                }
            }

        }
    }

    Thursday, February 5, 2015 2:23 PM

Answers

  • Your app stops working because you are creating it on the main UI thread. This is by design so you need to start a separate thread and create the client object on that thread. You could also kick-off a simple console recording app that does recoding in the background of your main app if that is easier.

    Carmine Sirignano - MSFT


    Thursday, February 5, 2015 7:17 PM