3
Answers

Button-Click-Event SoundPlayer

Dmitrij Solov

Dmitrij Solov

9y
472
1
Hello Guys, can you please say me, what is wrong with the following code?:
The browser-wpf-application consists of files: App.xaml, App.xaml.cs, Page1.xaml, Page1-xaml.cs.
The files: App.xaml and App.xaml.cs are clean. There is a problem in regard to the connection between Button 6 and the Click-event of Button 6.

Filename: Page1.xaml:

 <Page x:Class="WpfBrowserApplication1.Page1"
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
  xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
  mc:Ignorable="d" 
  d:DesignHeight="400" d:DesignWidth="600"
  Title="Page1">
  <Grid>
  <Button>
  <Button.Name>btn1</Button.Name>
  <Button.Margin>445,20,5,330</Button.Margin>
  <!--Margin: Left, Top, Right, Bottom--> 
  <Button.FontSize>18</Button.FontSize>
  <Button.Height>50</Button.Height>
  <Button.Width>150</Button.Width>
  <Button.Foreground>DarkBlue</Button.Foreground>
  <Button.Content>Der erste Button</Button.Content>
  </Button>
  <Button>
  <Button.Name>btn6</Button.Name>
  <Button.Margin>290,300,160,50 </Button.Margin>
  <Button.FontSize> 18 </Button.FontSize>
  <Button.Height> 50 </Button.Height>
  <Button.Width> 150 </Button.Width>
  <Button.Content> Der sechst Button </Button.Content>
  <Button.Foreground> DarkBlue </Button.Foreground>
  <Button.Background> Green </Button.Background>
  </Button>
  </Grid>
</Page>

Filename: Page1.xaml.cs:

using System;
using System.Collections;
using System.ComponentModel;
using System.Media;//wegen der klasse "SoundPlayer"
using System.Windows;
using System.Windows.Controls;
using System.Diagnostics;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
 
namespace WpfBrowserApplication1
{
  public partial class Page1 : Page
  {
  public Page1()
  { 
  InitializeComponent();
  btn6.Click += new RoutedEventHandler(btnButton6_Click);
  btn1.Click += new RoutedEventHandler(btnButton1_Click);
  ///RoutedEventhandler is the basic routed event handler delegate
  }
 
  void btnButton1_Click(object sender, RoutedEventArgs e)
  { 
  MessageBoxResult result = MessageBox.Show("Do you want to close this window?",
  "Close",
  MessageBoxButton.YesNoCancel,
  MessageBoxImage.Question,
  MessageBoxResult.No);
  if (result == MessageBoxResult.Yes)
  { MessageBoxResult result2 = MessageBox.Show("Sorry, I don't know how to close the app"); }
  }
 
  void btnButton6_Click(object sender, RoutedEventArgs e)
  {
  InitializeSound();
  }
  public void InitializeSound()
  {
  SoundPlayer player = new SoundPlayer();
  player.SoundLocation = "SampleSounds/beatuful money.wav";

  player.Load();
  player.Play();

  }
 
  private void txtOben_TextChanged(object sender, TextChangedEventArgs e)
  {
 
  }
 
  }
}
Answers (3)