I'm new in programming C# and xaml. I started to modify the KinectExplorer-WPF solution for Microsoft Kinect, in order to make it useful for my purposes. I modify the Kinect Window to add a new panel in which I show a sequence of images that represent some positions that could be reached by a person standing in front of the Kinect. This is the code that I've added to KinectWindow.xaml:
<StackPanel Orientation="Vertical" Grid.Row="1" Grid.RowSpan="2" Margin="0,10" HorizontalAlignment="Center" Width="400" >
<Grid Name="ExerciseViewerHost" Width="398" Height="645" Margin="0,0,0,0">
<Grid Name="ExerciseVis" Background="{StaticResource DarkNeutralBrush}">
<Viewbox Stretch="Uniform" HorizontalAlignment="Center" VerticalAlignment="Center">
<Image Name="Exerc" CommandManager.Executed="Change_Position" Margin="5 5 5 5" />
</Viewbox>
<TextBlock x:Name="txtBox1" VerticalAlignment="Top" HorizontalAlignment="Center" FontSize="30" FontWeight="DemiBold" Text="{Binding TextBox1Text, ElementName=root}" TextAlignment="Center" Margin="36,105,36,0" Height="45" Width="328" />
</Grid>
</Grid>
</StackPanel>
"Change_Position" is the event that change image when a parameter is reached (in particular a max counter value) and it is defined in KinectWindow.xaml.cs as
private void Change_Position(object sender, EventArgs e)
and it is triggered by a DispatcherTimer
every 0 seconds in
DispatcherTimer dispatcherTimer = new DispatcherTimer();
dispatcherTimer.Tick += new EventHandler(Change_Position);
dispatcherTimer.Interval = new TimeSpan(0, 0, 0);
dispatcherTimer.Start();
The code that I've described works properly, but my question is very simple... there's a more "elegant" way to trigger this event continuously (not with a Timer)? Has anyone some parts of code that could share with me to solve my problem? Thanks in advance!
You can use the CompositionTarget.Rendering
event, which fires every time WPF renders a scene. Don't forget to unhook the event handler when you don't need it as it can be rather expensive.