Connect with us

Microsoft

Adding event handlers: Sounds, Dark Mode & Windowing (Part 2)

Note that in part 1 of the WinAppSDK Blog Series you implemented the visual layer of the DrumPad using WinUI 3 controls. However, if you click on any buttons, nothing happens, because we have not defined any actions, which we will do now. You will be guided to accomplish the following in this post:  Play…

Published

on

Note that in part 1 of the WinAppSDK Blog Series you implemented the visual layer of the DrumPad using WinUI 3 controls. However, if you click on any buttons, nothing happens, because we have not defined any actions, which we will do now. You will be guided to accomplish the following in this post: 

  • Play a sound when a drum pad button is clicked
  • Switch into dark mode when the toggle switch is turned on
  • Go into a specific windowing mode when a presenter mode is selected

Play a sound when a drum pad button is clicked

  1. Download sound clips to your project’s Assets folder

NOTE: You may download any .wav sound clips for this part. Optionally, you could use my sound clips here as well. Store the sound clips under your project’s Assets folder because that is the folder path we will provide to specify which sound to play.

    2. Install the System.Windows.Extensions NuGet Package

NOTE: Playing a sound in WinUI apps requires using a library called System.Media, accessible through a NuGet Package. Following these two steps to install the NuGet Package in Visual Studio:

  • Navigate to Solution Explorer, under Dependencies, right-click on Packages, select Manage NuGet Packages
  • Select the Browse tab, search System.Windows.Extensions and install it

    3. Add the following namespace to MainAppWindow.xaml.cs:

using System.Media;

4. Create a call-back function that plays a sound in class MainWindow : Window :

MainAppWindow.xaml.cs:

private void pad_clicked(object sender, RoutedEventArgs e) { // get the full path to your app’s folder where it is installed var installedPath = Windows.ApplicationModel.Package.Current.InstalledLocation.Path; // join path above with the sub-paths in your Assets folder and the specific sound file var soundFile = Path.Join(installedPath, “Assets”, “rim.wav”); SoundPlayer player = new System.Media.SoundPlayer(soundFile); player.Play(); }

5. link the call-back function above to a button via the Click parameter:

MainWindow.xaml:

Continue Reading
Click to comment

Leave a Reply

Your email address will not be published. Required fields are marked *

Business

Introducing CoreAI – Platform and Tools

Satya Nadella, Chairman and CEO, shared the below communication with Microsoft employees this morning. As we begin the new year, it’s clear that we’re entering the next innings of this AI platform shift. 2025 will be about model-forward applications that reshape all application categories. More so than any previous platform shift, every layer of the……

Published

on

By

Satya Nadella, Chairman and CEO, shared the below communication with Microsoft employees this morning. As we begin the new year, it’s clear that we’re entering the next innings of this AI platform shift. 2025 will be about model-forward applications that reshape all application categories. More so than any previous platform shift, every layer of the…

Source

Continue Reading

Microsoft

Celebrating Windows 11 PC innovations announced at CES

As we reflect on the incredible innovations showcased at CES 2025, it’s evident that the future of Windows 11 will enable new possibilities and experiences for our customers. Throughout the last year and this week at CES, our partners continue to adv Source

Published

on

By

As we reflect on the incredible innovations showcased at CES 2025, it’s evident that the future of Windows 11 will enable new possibilities and experiences for our customers. Throughout the last year and this week at CES, our partners continue to adv

Source

Continue Reading

Business

The future of retail with Dynamics 365 AI-powered ERP solutions

This year at NRF, we will showcase the latest AI and agent innovations in Dynamics 365 AI-powered ERP and Service solution. Learn more. Source

Published

on

By

This year at NRF, we will showcase the latest AI and agent innovations in Dynamics 365 AI-powered ERP and Service solution. Learn more.

Source

Continue Reading

Trending

Copyright © 2021 Today's Digital.