这是我的XAML:
<Button x:Name = "Helloworld"
Content = "Hello world"
Grid.Column = "0"
Grid.Row = "0"
Click="playEvent"
Hold="setEvent"
Tag="/Sounds/helloworld.mp3"
/>
<Button x:Name = "Helloworld_ring"
Grid.Column = "1"
Grid.Row = "0"
HorizontalAlignment="Stretch"
VerticalAlignment="Stretch"
Click="setEvent"
Tag="/Sounds/helloworld.mp3">
<Image Source="/Images/note.png" Stretch="Fill" Height="30" Width="30" />
</Button>
背后的代码如下:
namespace MyApp
{
public partial class MainPage : PhoneApplicationPage
{
Button playSrcButton;
Button setSrcButton;
SaveRingtoneTask saveRingtoneChooser;
public MainPage()
{
InitializeComponent();
saveRingtoneChooser = new SaveRingtoneTask();
saveRingtoneChooser.Completed += new EventHandler<TaskEventArgs>(saveRingtoneChooser_Completed);
}
private void playEvent(object sender, RoutedEventArgs e)
{
playSrcButton = e.OriginalSource as Button;
playFile(playSrcButton.Tag.ToString(), playSrcButton.Content.ToString());
}
private void setEvent(object sender, RoutedEventArgs e)
{
setSrcButton = e.OriginalSource as Button;
saveRingtoneChooser.Source = new Uri(setSrcButton.Tag.ToString());
saveRingtoneChooser.DisplayName = setSrcButton.Content.ToString();
saveRingtoneChooser.Show();
}
}
}
我遇到的问题是,如果我在点击Helloworld_ring按钮时发生了setEvent函数,那么一切正常。如果我尝试消除这些按钮并在Helloworld函数上使用Hold事件,我会收到错误
An unhandled exception of type 'System.NullReferenceException' occurred in MyApp.dll
我理解的是相当不起眼的。我注意到的问题是,在调试过程中, setSrcButton 变量没有获得 e.OriginalSource 的值,但是当我使用时,仅 Helloworld上的Hold事件。当我在Helloworld_ring上使用Click事件时,一切正常。
感谢所有帮助。谢谢:))
答案 0 :(得分:0)
也许e.OriginalSource不是你的按钮。
在这种情况下,发件人应该是你的Button,所以请尝试改为。
setSrcButton = sender as Button;