简体   繁体   中英

UWP Overriding Frame.Navigate to pass data from ListViewItem to new Page casues Null Exception

I have this XAML ListViewItem:

<ListView HorizontalAlignment="Center" Height="500" Margin="0,130,0,0"
              VerticalAlignment="Top" Width="400" x:Name="RoutesListView"
              SelectionChanged="RoutesListView_SelectionChanged"
              IsItemClickEnabled="True" SelectionMode="Single"
              ItemClick="RoutesListView_ItemClick">
        <ListView.ItemTemplate>
            <DataTemplate>
                <StackPanel >
                    <TextBlock Text="{Binding RouteName}" 
                       Margin="20,0,20,8"
                       FontFamily="Calibri"
                       FontSize="24
                       FontWeight="SemiBold"
                       Foreground="Gray" 
                       />
                </StackPanel>
            </DataTemplate>
        </ListView.ItemTemplate>
    </ListView>

In the click-handler I want to send the Route object's ID to a new page like this:

private void RoutesListView_ItemClick(object sender, ItemClickEventArgs e)
    {

        var items = (sender as ListView).Items;
        var route = items[index] as Route;
        var id = route.ID;
        var data = new RouteParameters { ID = id };
        Frame.Navigate(typeof(RouteView), data);

    }

The RouteParameters class looks like this:

 public class RouteParameters
{
    public int ID { get; set; }

}

Finally in the RouteView I override the OnNavigatedTo like this:

protected override void OnNavigatedTo(NavigationEventArgs e)
    {
        var p = e.Parameter as RouteParameters;
        int id = p.ID;


    }

I get a Null exception in the Frame.Navigate method. Sorry if I missed something obvious but I can't find out what is causing the error after debugging every line and not seeing any null object

Okay, so I found out that I had previously added a parameter to the RouteView class and forgotten to remove it while trying different methods of passing data to the new view. This is not a problem anymore and I will close the thread in 48 hours.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM