【发布时间】:2012-04-04 06:47:13
【问题描述】:
这是我的导航窗口
<NavigationWindow x:Class="WpfApplication1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="800" Width="600" Source="Page1.xaml">
这是我的页面1
<Page x:Class="WpfApplication1.Page1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
mc:Ignorable="d"
d:DesignHeight="600" d:DesignWidth="800"
Title="Page1" Name="IndexPage">
<ListView Name="myListView" ItemsSource="{Binding ElementName=IndexPage, Path=SeriesCollection}" ScrollViewer.HorizontalScrollBarVisibility="Disabled" IsSynchronizedWithCurrentItem="True" SelectionChanged="handleSelected">
<ListView.ItemsPanel >
<ItemsPanelTemplate>
<WrapPanel>
</WrapPanel>
</ItemsPanelTemplate>
</ListView.ItemsPanel>
<ListView.ItemTemplate>
<DataTemplate>
<StackPanel >
<Image Width="214" Height="317" Source="{Binding Image}"/>
<Label Content="{Binding Name}" />
</StackPanel>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
第 2 页只是一个空骨架
后面的代码
namespace WpfApplication1
{
/// <summary>
/// Interaktionslogik für Page1.xaml
/// </summary>
public partial class Page1 : Page
{
private ObservableCollection<Series> _series =
new ObservableCollection<Series>();
public ObservableCollection<Series> SeriesCollection
{
get { return _series; }
}
public Page1()
{
InitializeComponent();
DirectoryInfo baseDir = new DirectoryInfo(@"C:\Serien");
DirectoryInfo[] dirs = baseDir.GetDirectories();
foreach (DirectoryInfo dir in dirs)
{
Series serie = new Series(dir);
Console.WriteLine("adding " + serie.Name);
_series.Add(serie);
}
Console.WriteLine(_series.Count);
}
public void handleSelected(object sender, RoutedEventArgs args)
{
Series currentSerie = (Series) myListView.Items.CurrentItem;
Page2 page = new Page2();
this.NavigationService.Navigate(page);
Console.WriteLine(currentSerie.Name);
Console.WriteLine(currentSerie.GetType());
Console.WriteLine(currentSerie.ToString());
}
}
}
所以我单击一个项目以触发 SelectionChanged 事件以在我导航到 page2 的 SelectionChanged 中处理它,到目前为止一切顺利。
然后我使用导航窗口中的后退按钮并在
处遇到 NullpointerExceptionthis.NavigationService.Navigate(page);
我什至不知道为什么会触发此方法。所以很明显我在做一些愚蠢的事情。请告诉我它是什么。感谢您的时间和努力。
【问题讨论】:
-
你确定你在那里得到了异常,而不是在下一行?
CurrentItem因此currentSerie很可能是null。 -
@Clemens 这就是 VS2010 告诉我的。我也尝试在没有控制台输出的情况下运行它并得到相同的错误
-
null到底是什么?你有堆栈跟踪吗? -
@Clemens 你去pastebin.com/qzzhbCvt sry 德国输出
-
凯因问题。对不起,但堆栈跟踪没有帮助。尝试在该行设置断点以找出
null是什么。
标签: c# wpf nullpointerexception navigationwindow