Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/257.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 返回NavigationWindow后的NullPointerException_C#_Wpf_Nullpointerexception_Navigationwindow - Fatal编程技术网

C# 返回NavigationWindow后的NullPointerException

C# 返回NavigationWindow后的NullPointerException,c#,wpf,nullpointerexception,navigationwindow,C#,Wpf,Nullpointerexception,Navigationwindow,那是我的导航窗口 <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

那是我的导航窗口

<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">

我甚至不知道为什么会触发这个方法。很明显我在做一些愚蠢的事。请告诉我是什么。谢谢你的时间和支持

这里的问题是您处理了错误的事件。我假设您希望通过单击ListViewItem打开第2页。因此,您应该使用鼠标事件,而不是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>
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>

我们使用一个类似视图模型的按钮,它能够处理点击。

您确定在那里得到了异常,而不是在下一行吗?CurrentItem和currentSerie很可能是空的。@Clemens这就是VS2010告诉我的。我还尝试在没有控制台输出的情况下运行它,但得到了相同的错误,那里的null是什么?你有堆栈跟踪吗?@Clemens这里是关于德国输出的问题。很抱歉,堆栈跟踪没有帮助。请尝试在该行设置断点,以找出空值。此操作有效。但是,将mouseDown事件用作click事件真的是一种好的实践吗?我应该使用stackPanel中的按钮来进行真正的点击事件吗?此外,我仍然不知道为什么SelectionChanged对于这种类型的流来说是一个坏事件,除了它不起作用之外;-非常感谢你,如果没有更好的答案,我会接受你的。SelectionChanged的目的是在SelectedItem属性更改时做出反应。有很多方法可以做到这一点。用户可以单击ListViewItem或使用键盘键,代码可以通过为SelectedItem属性指定另一个值来切换选择。您确定要在所有这些情况下打开第2页吗?这就是为什么SelectionChanged对你不好。我同意如果需要点击,添加一个按钮会更好,所以我会更新我的答案。更好的解决方案应该使用命令而不是event handler.thx来获得帮助。我实现了open命令,因为我认为它很适合上下文。但后来发件人更改为Page,我返回myListView.Items.CurrentItem以获取当前选择。我还需要按钮吗?因为我使用的是命令而不是单击事件?thxUse CommandParameter而不是sender。看一看:您可以像{Binding}一样绑定CommandParameter,将单击的序列直接发送到处理程序。
this.NavigationService.Navigate(page);
<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;
    ...
}
<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>