这是我的 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 事件时,一切正常。
感谢所有帮助。谢谢 :)