/// <summary> /// Use the custom Oven and BreadBakedEventArgs Windows Runtime components. /// </summary> /// <param name="sender">Contains information about the button that fired the event.</param> /// <param name="e">Contains state information and event data associated with a routed event.</param> private void Start_Click(object sender, RoutedEventArgs e) { // Component Creation if (_myOven == null) { Dimensions dimensions; dimensions.Width = 2; dimensions.Height = 2; dimensions.Depth = 2; _myOven = new Oven(dimensions); } // Getters and setters are accessed using property syntax OvenClientOutput.Text += "Oven volume is: " + _myOven.Volume.ToString() + "\n"; // Register even listeners _myOven.BreadBaked += new TypedEventHandler<Oven, BreadBakedEventArgs>(BreadCompletedHandler1); _myOven.BreadBaked += new TypedEventHandler<Oven, BreadBakedEventArgs>(BreadCompletedHandler2); _myOven.BreadBaked += new TypedEventHandler<Oven, BreadBakedEventArgs>(BreadCompletedHandler3); // Unregister from an event using the -= syntax and a delegate instance _myOven.BreadBaked -= new TypedEventHandler<Oven, BreadBakedEventArgs>(BreadCompletedHandler3); // Bake a loaf of bread. This will trigger the BreadBaked event. _myOven.BakeBread("Sourdough"); // Trigger the event again with a different preheat time _myOven.ConfigurePreheatTemperature(OvenTemperature.High); _myOven.BakeBread("Wheat"); }
async private void BreadCompletedHandler1(Oven oven, BreadBakedEventArgs args) { Action updateOutputText = () => { OvenClientOutput.Text += "Event Handler 1: Invoked\n"; OvenClientOutput.Text += "Event Handler 1: Oven volume is: " + oven.Volume.ToString() + "\n"; OvenClientOutput.Text += "Event Handler 1: BreadBakedEventArgs flavor is: " + args.Bread.Flavor + "\n"; }; await OvenClientOutput.Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, new Windows.UI.Core.DispatchedHandler(updateOutputText)); }
/// <summary> /// Handle a Windows Runtime error originated by the Oven Windows Runtime component. /// </summary> /// <param name="sender">Contains information about the button that fired the event.</param> /// <param name="e">Contains state information and event data associated with a routed event.</param> private void Start_Click(object sender, RoutedEventArgs e) { Oven myOven = new Oven(); try { // Intentionally pass a bad parameter myOven.ConfigurePreheatTemperature((OvenTemperature)5); } catch (System.ArgumentException) { CustomExceptionOutput.Text += "Exception caught. Please attach a debugger and enable first chance native exceptions to view exception details.\n"; } }
async private void BreadCompletedHandler1(Oven oven, Bread bread) { Action updateOutputText = () => { OvenClientOutput.Text += "Event Handler 1: Invoked\n"; OvenClientOutput.Text += "Event Handler 1: Oven volume is: " + oven.Volume.ToString() + "\n"; OvenClientOutput.Text += "Event Handler 1: Bread flavor is: " + bread.Flavor + "\n"; }; if (OvenClientOutput.Dispatcher.HasThreadAccess) { // If the current thread is the UI thread then execute the lambda. updateOutputText(); } else { // If the current thread is not the UI thread use the dispatcher to execute the lambda on the UI thread. await OvenClientOutput.Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, new Windows.UI.Core.DispatchedHandler(updateOutputText)); } }
/// <summary> /// Use the custom Oven and BreadBakedEventArgs Windows Runtime components. /// </summary> /// <param name="sender">Contains information about the button that fired the event.</param> /// <param name="e">Contains state information and event data associated with a routed event.</param> private async void Start_Click(object sender, RoutedEventArgs e) { AppServiceConnection appConnection = new AppServiceConnection(); appConnection.AppServiceName = "VoipTasks.AppService"; appConnection.PackageFamilyName = Windows.ApplicationModel.Package.Current.Id.FamilyName; AppServiceConnectionStatus status = await appConnection.OpenAsync(); if (status == AppServiceConnectionStatus.Success) { // Component Creation if (_myOven == null) { Dimensions dimensions; dimensions.Width = 2; dimensions.Height = 2; dimensions.Depth = 2; _myOven = new Oven(dimensions); } // Getters and setters are accessed using property syntax OvenClientOutput.Text += "Oven volume is: " + _myOven.Volume.ToString() + "\n"; // Register even listeners _myOven.BreadBaked += new TypedEventHandler<Oven, BreadBakedEventArgs>(BreadCompletedHandler1); _myOven.BreadBaked += new TypedEventHandler<Oven, BreadBakedEventArgs>(BreadCompletedHandler2); _myOven.BreadBaked += new TypedEventHandler<Oven, BreadBakedEventArgs>(BreadCompletedHandler3); // Unregister from an event using the -= syntax and a delegate instance _myOven.BreadBaked -= new TypedEventHandler<Oven, BreadBakedEventArgs>(BreadCompletedHandler3); // Bake a loaf of bread. This will trigger the BreadBaked event. _myOven.BakeBread("Sourdough"); // Trigger the event again with a different preheat time _myOven.ConfigurePreheatTemperature(OvenTemperature.High); _myOven.BakeBread("Wheat"); } }
async private void BreadCompletedHandler2(Oven oven, BreadBakedEventArgs bread) { Action updateOutputText = () => { OvenClientOutput.Text += "Event Handler 2: Invoked\n"; }; await OvenClientOutput.Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, new Windows.UI.Core.DispatchedHandler(updateOutputText)); }
async private void BreadCompletedHandler3(Oven oven, Bread bread) { // Event handler 3 was removed and will not be invoked Action updateOutputText = () => { OvenClientOutput.Text += "Event Handler 3: Invoked\n"; }; if (OvenClientOutput.Dispatcher.HasThreadAccess) { // If the current thread is the UI thread then execute the lambda. updateOutputText(); } else { // If the current thread is not the UI thread use the dispatcher to execute the lambda on the UI thread. await OvenClientOutput.Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, new Windows.UI.Core.DispatchedHandler(updateOutputText)); } }