我在Windows Phone Silverlight 8.1中制作了简单的应用程序。 它看起来像带有液晶显示屏的手机键盘 - 看起来像屏幕,显示当前点击按钮的数量并播放该数字的音调。
我一直在寻找一段时间,但我没有找到具体的内容。
我有Play()的问题;方法,因为它在我正在调试时执行,但在应用程序部署到我的设备后实时发现我没有听到...
我研究了很多主题,我可以说它取决于
mediaElementObject.CurrentState;
有时它处于“关闭”状态,有时处于“打开”状态(然后我会在调试时听到声音)。
C#
private void KeypadButtonClick(object sender, RoutedEventArgs e)
{
Button currentButton = sender as Button;
MediaElement keySound = null;
if (currentButton != null)
{
string buttonContentValue = currentButton.Content.ToString();
keySound = new MediaElement();
ContentPanel.Children.Add(keySound);
PlayTargetKeypadSound(buttonContentValue, keySound);
RefreshNumber(buttonContentValue);
}
if (keySound != null)
{
ContentPanel.Children.Remove(keySound);
}
}
private void PlayTargetKeypadSound(string buttonContentValue, MediaElement keySound)
{
String path = "/Assets/Sounds/keyNumber_" + buttonContentValue + ".wav";
keySound.CurrentStateChanged += KeySoundOnCurrentStateChanged;
keySound.Source = new Uri(path, UriKind.RelativeOrAbsolute);
keySound.Volume = 1.0;
keySound.AutoPlay = false;
}
private void KeySoundOnCurrentStateChanged(object sender, RoutedEventArgs routedEventArgs)
{
MediaElement sound = sender as MediaElement;
if (sound != null) sound.Play();
}
private void RefreshNumber(string buttonContentValue)
{
NumberTextBlock.Text = buttonContentValue;
}
XAML
<Grid x:Name="ContentPanel" Grid.Row="2" Margin="8,0,8,12">
<Grid.RowDefinitions>
<RowDefinition Height="*" />
<RowDefinition Height="*" />
<RowDefinition Height="*" />
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
<ColumnDefinition Width="*" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<!-- First Row -->
<Button Click="KeypadButtonClick" Content="1" Style="{StaticResource KeypadButtonStyle}" />
<Button Click="KeypadButtonClick" Content="2" Style="{StaticResource KeypadButtonStyle}" Grid.Column="1" />
<Button Click="KeypadButtonClick" Content="3" Style="{StaticResource KeypadButtonStyle}" Grid.Column="2" />
<!-- Second Row -->
<Button Click="KeypadButtonClick" Content="4" Style="{StaticResource KeypadButtonStyle}" Grid.Row="1" />
<Button Click="KeypadButtonClick" Content="5" Style="{StaticResource KeypadButtonStyle}" Grid.Row="1" Grid.Column="1" />
<Button Click="KeypadButtonClick" Content="6" Style="{StaticResource KeypadButtonStyle}" Grid.Row="1" Grid.Column="2" />
<!-- Third Row -->
<Button Click="KeypadButtonClick" Content="7" Style="{StaticResource KeypadButtonStyle}" Grid.Row="2" />
<Button Click="KeypadButtonClick" Content="8" Style="{StaticResource KeypadButtonStyle}" Grid.Row="2" Grid.Column="1" />
<Button Click="KeypadButtonClick" Content="9" Style="{StaticResource KeypadButtonStyle}" Grid.Row="2" Grid.Column="2" />
<!-- Fourth Row -->
<Button Click="KeypadButtonClick" Content="*" Style="{StaticResource KeypadButtonStyle}" Grid.Row="3" />
<Button Click="KeypadButtonClick" Content="0" Style="{StaticResource KeypadButtonStyle}" Grid.Row="3" Grid.Column="1" />
<Button Click="KeypadButtonClick" Content="#" Style="{StaticResource KeypadButtonStyle}" Grid.Row="3" Grid.Column="2" />
</Grid>
非常感谢帮助
答案 0 :(得分:0)
确定。
我解决了......
<强> C#强>
private void KeypadButtonClick(object sender, RoutedEventArgs e)
{
Button currentButton = sender as Button;
if (currentButton != null)
{
string buttonContentValue = currentButton.Content.ToString();
PlayTargetKeypadSound(buttonContentValue);
RefreshNumber(buttonContentValue);
}
}
private void PlayTargetKeypadSound(string buttonContentValue)
{
String path = "/Assets/Sounds/keyNumber_" + buttonContentValue + ".wav";
KeySoundMediaElement.MediaOpened += KeySoundMediaElementOnMediaOpened;
KeySoundMediaElement.Source = new Uri(path, UriKind.RelativeOrAbsolute);
}
private void KeySoundMediaElementOnMediaOpened(object sender, RoutedEventArgs routedEventArgs)
{
MediaElement sound = sender as MediaElement;
if (sound != null) sound.Play();
}
private void RefreshNumber(string buttonContentValue)
{
NumberTextBlock.Text = buttonContentValue;
}
<强> XAML 强>
<!-- Fourth Row -->
<Button Click="KeypadButtonClick" Content="*" Style="{StaticResource KeypadButtonStyle}" Grid.Row="3" />
<Button Click="KeypadButtonClick" Content="0" Style="{StaticResource KeypadButtonStyle}" Grid.Row="3" Grid.Column="1" />
<Button Click="KeypadButtonClick" Content="#" Style="{StaticResource KeypadButtonStyle}" Grid.Row="3" Grid.Column="2" />
<MediaElement
x:Name="KeySoundMediaElement"
Volume="1"
AutoPlay="False"
/>