当我点击listview上的任何项目时,我收到此错误。 System.NullReferenceException - {“对象引用未设置为对象的实例。”}
我的MainPage.xaml中心代码
<Hub x:Name="MainHub" Header="My Hub">
<HubSection x:Name="Test" Header="Online" Visibility="Collapsed" Margin="0,30,0,0">
<DataTemplate>
<ListView Name="ItemData1" ItemsSource="{Binding}" SelectionChanged="Catergory_Click">
<ListView.ItemContainerStyle>
<Style TargetType="ListViewItem">
<Setter Property="HorizontalContentAlignment" Value="Stretch" />
<Setter Property="FontSize" Value="18"/>
</Style>
</ListView.ItemContainerStyle>
</ListView>
</DataTemplate>
</HubSection>
</Hub>
我有3个具有相同SelectionChanged功能的ListView
MainPage.xaml.cs中
public class RootObject
{
public int id { get; set; }
public string name { get; set; }
public string message { get; set; }
}
private void Catergory_Click(object sender, SelectionChangedEventArgs e)
{
RootObject obj = (sender as ListView).SelectedItem as RootObject;
User.Setting.AddOrUpdateValue("SectionName", obj.name);
}
相同的代码在没有Hub控件和Section的情况下工作正常。
答案 0 :(得分:0)
立即将SelectionChanged更改为ItemClick..Works
<Hub x:Name="MainHub" Header="My Hub" Foreground="White">
<HubSection x:Name="Test" Header="Online" Visibility="Collapsed" Margin="0,30,0,0">
<DataTemplate>
<ListView x:Name="TestListView" ItemsSource="{Binding}" ItemClick="TestListView_ItemClick" IsItemClickEnabled="True" >
<ListView.ItemTemplate>
<DataTemplate>
<TextBlock x:Name="textBlock" Text="{Binding name}"/>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</DataTemplate>
</HubSection>
private void TestListView_ItemClickobject sender, ItemClickEventArgs e)
{
RootObject obj = e.ClickedItem as RootObject;
User.Setting.AddOrUpdateValue("CourseSectionName", obj.name);
}