Answered by:
New to programming. Button Click Event Question with DispatcherTimer

Question
-
Hi All,
I am working on my first app and am very excited to learn c# and xaml. I've watch some of the jump start videos and have read most of a head first c# book. I am new and am finding small things taking me hours to figure out and research but I am slowly making progress. I'm not sure where to look for this one so I'm posting a question. Thanks in advance!
I have a Windows store app made from the Universal app template in Visual Studio 2013 Express. After the initialize component method is run I create an object called Simulator that contains an observable collection. I then create a dispatch timer that runs every "x" number of miliseconds and calls a method in the Simulator class that modifies the data in the observable collection. This data is bound to a chart so the chart updates.
Here is the problem. I have a button that I want to use to set the "IsEnabled" Property of the Dispatcher object to false to effectively pause the trend. When I click it again it will set it to true. I added a click event handler and thought I would look at the current state of the "IsEnabled" property of the Dispatcher object and then set it to the opposite. I can't access the dispatcher object from the event handler. In fact I don't seem to be able to access anything used from within the page from the event handler. I'm sure this is extremely basic but I have been trying to figure out the solution for about 3 hours now. Thanks for the help! The code is below. The problem is in the event handler at the very bottom.
using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Runtime.InteropServices.WindowsRuntime; using Windows.Foundation; using Windows.Foundation.Collections; using Windows.UI.Xaml; using Windows.UI.Xaml.Controls; using Windows.UI.Xaml.Controls.Primitives; using Windows.UI.Xaml.Data; using Windows.UI.Xaml.Input; using Windows.UI.Xaml.Media; using Windows.UI.Xaml.Navigation; using System.Threading.Tasks; // The Blank Page item template is documented at http://go.microsoft.com/fwlink/?LinkId=234238 namespace Trainer { /// <summary> /// An empty page that can be used on its own or navigated to within a Frame. /// </summary> public sealed partial class MainPage : Page { public MainPage() { this.InitializeComponent(); Simulator PVData = new Simulator(); this.PIDChart.DataContext = PVData; ///Add code for timer here DispatcherTimer _dispatcherTimer; // Set up a DispatchTimer _dispatcherTimer = new DispatcherTimer(); PVData.count = 1; _dispatcherTimer.Tick += PVData.AddVals; _dispatcherTimer.Interval = new TimeSpan(0, 0, 0, 0, 250); _dispatcherTimer.Start(); this.btnPauseResume.DataContext = _dispatcherTimer.IsEnabled; } private void btnPauseResume_Click(object sender, RoutedEventArgs e) { //This is where the problem is. _dispatcherTimer does not exist in the current context _dispatcherTimer.IsEnabled = false; } } }
Thursday, May 22, 2014 4:46 AM
Answers
-
The _dispatcherTimer is defined inside the MainPage() constructor, so it is visible only within that function. Where a variable is visible is called the variable's scope of you want to read up on it.
If you move the variable declaration outside of the constructor then it will have class scope and can be used by functions within the class, such as your event handler.
public sealed partial class MainPage : Page { // Visible throughout the class if declared here DispatcherTimer _dispatcherTimer; public MainPage() { this.InitializeComponent(); Simulator PVData = new Simulator(); this.PIDChart.DataContext = PVData; ///Add code for timer here // Only visible in this function if declared here: // DispatcherTimer _dispatcherTimer; // Set up a DispatchTimer _dispatcherTimer = new DispatcherTimer(); PVData.count = 1; _dispatcherTimer.Tick += PVData.AddVals; _dispatcherTimer.Interval = new TimeSpan(0, 0, 0, 0, 250); _dispatcherTimer.Start(); this.btnPauseResume.DataContext = _dispatcherTimer.IsEnabled; // ...... } }
- Marked as answer by emanbyu Thursday, May 22, 2014 2:22 PM
Thursday, May 22, 2014 1:52 PMModerator
All replies
-
The _dispatcherTimer is defined inside the MainPage() constructor, so it is visible only within that function. Where a variable is visible is called the variable's scope of you want to read up on it.
If you move the variable declaration outside of the constructor then it will have class scope and can be used by functions within the class, such as your event handler.
public sealed partial class MainPage : Page { // Visible throughout the class if declared here DispatcherTimer _dispatcherTimer; public MainPage() { this.InitializeComponent(); Simulator PVData = new Simulator(); this.PIDChart.DataContext = PVData; ///Add code for timer here // Only visible in this function if declared here: // DispatcherTimer _dispatcherTimer; // Set up a DispatchTimer _dispatcherTimer = new DispatcherTimer(); PVData.count = 1; _dispatcherTimer.Tick += PVData.AddVals; _dispatcherTimer.Interval = new TimeSpan(0, 0, 0, 0, 250); _dispatcherTimer.Start(); this.btnPauseResume.DataContext = _dispatcherTimer.IsEnabled; // ...... } }
- Marked as answer by emanbyu Thursday, May 22, 2014 2:22 PM
Thursday, May 22, 2014 1:52 PMModerator -
Hi Rob,
Thanks for the reply. That makes perfect sense and I knew it would be easy. I guess I thought that the public mainpage() was the class but I see that it is just the constructor method inside the class just like any other class. I'm coming from VBA and still getting the hang of it . . .
Thank you!
Thursday, May 22, 2014 2:22 PM