簡體   English   中英

返回 NavigationWindow 后出現 NullPointerException

[英]NullPointerException after going back in a NavigationWindow

那是我的導航 window

<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 中處理它,到目前為止一切順利。

然后我使用導航 window 中的后退按鈕並卡在 NullpointerException 處

this.NavigationService.Navigate(page);

我什至不知道為什么會觸發此方法。 所以很明顯我在做一些愚蠢的事情。 請告訴我它是什么。 感謝您的時間和精力。

這里的問題是你處理了錯誤的事件。 我假設您想通過單擊 ListViewItem 打開 Page2。 因此,您應該使用鼠標事件而不是 SelectionChanged。

例如,您可以在 DataTemplate 中訂閱 StackPanel MouseDown 事件:

<DataTemplate>
    <StackPanel Background="Transparent"
                MouseDown="StackPanel_MouseDown">
        <Image Width="214" Height="317" Source="{Binding Image}"/>
        <Label Content="{Binding Name}"/>
    </StackPanel>
</DataTemplate>

您可以使用以下方法訪問點擊的系列:

private void StackPanel_MouseDown(object sender, MouseButtonEventArgs e)
{
    var currentSerie = (Series)((StackPanel)sender).DataContext;
    ...
}

UPD如果你需要真正的點擊,你可以使用這樣的技巧:

<DataTemplate>
    <Button Click="Button_Click">
        <Button.Template>
            <ControlTemplate TargetType="Button">
                <ContentPresenter/>
            </ControlTemplate>
        </Button.Template>
        <StackPanel Background="Transparent">
            <Image Width="214" Height="317" Source="{Binding Image}"/>
            <Label Content="{Binding Name}"/>
        </StackPanel>
    </Button>
</DataTemplate>

我們使用像視圖模型一樣的按鈕來處理點擊。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM