0
我創建了Windows Phone 8應用 和我做了LongListSelector在App玩家的NullReferenceException在訪問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
at string fname = tb.Text;
我做錯了什麼?
你使用哪個類來綁定'longlist' –
你檢查過這種情況下'sender'的實際類型嗎?顯然它不是'TextBlock'。這可能是一個'LongListSelector'。 – JLRishe
@MuhammadUmar Player.cs(包含信息字段),無關緊要 – GabourX