我正在创建一个Windows Phone 8应用程序 我在应用程序中为玩家做了一个LongListSelector
LongListSelector
<phone:LongListSelector x:Name="playersLongList" LayoutMode="List" IsGroupingEnabled="False" HorizontalAlignment="Left" Height="653" Margin="15,190,0,0" VerticalAlignment="Top" Width="412" SelectionChanged="playersLongList_SelectionChanged">
<phone:LongListSelector.ItemTemplate>
<DataTemplate>
<StackPanel x:Name="s1">
<TextBlock x:Name="playerName" Text="{Binding FirstName}" FontFamily="/Assets/Fonts/Moire Light.ttf#Moire Light" FontSize="48" />
</StackPanel>
</DataTemplate>
</phone:LongListSelector.ItemTemplate>
</phone:LongListSelector>
现在,我需要的是当玩家点击某个项目时,该应用会显示一个MessageBox,显示所选项目的玩家名称,这就是我在EventHandler中的表现方式
private void playersLongList_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
var tb = sender as TextBlock;
string fname = tb.Text;
MessageBox.Show("hello"+fname);
}
调试在NullReferenceException
string fname = tb.Text;
我做错了什么?
答案 0 :(得分:2)
试试这个。
private void playersLongList_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
if (e.AddedItems.Count > 0)
{
Player p = e.AddedItems[0] as Player;
string fname = p.FirstName;
MessageBox.Show("hello"+fname);
}
}