简体   繁体   English

导航回WP7 NullReferenceException

[英]WP7 NullReferenceException on navigation back

I am getting a null reference exception while trying to navigate back to the pervious MainPage.xaml using the below code from my RSS reader. 尝试使用RSS阅读器中的以下代码导航回以前的MainPage.xaml时,出现空引用异常。 Can someone please help me by advising what I need to do to avoide this error? 有人可以建议我需要做些什么来避免此错误,以帮助我吗?

public partial class FeedDetailsPage : PhoneApplicationPage
{
    object _selectedFeedItem;
    object _selectedFeed;

    public FeedDetailsPage()
    {
        InitializeComponent();
        Loaded += new RoutedEventHandler(PhoneApplicationPage_Loaded);
        LoadFeed();
    }

    private void LoadFeed()
    {
        FrameworkElement root = Application.Current.RootVisual as FrameworkElement;
        var currentFeed = root.DataContext as ATP_Tennis_App.ViewModels.FeedViewModel;
        WebClient wc = new WebClient();
        wc.DownloadStringCompleted += new DownloadStringCompletedEventHandler(wc_DownloadStringCompleted);
        wc.DownloadStringAsync(new Uri(currentFeed.FeedUrl));
        _selectedFeed = currentFeed;
    }

    void wc_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
    {
      //  if (e.Error != null) { return; }

        var rssFeed = XElement.Parse(e.Result);

        var currentFeed = this.DataContext as ATP_Tennis_App.ViewModels.FeedViewModel;
        var items = from item in rssFeed.Descendants("item")

                    select new ATP_Tennis_App.ViewModels.FeedItemViewModel()
                    {

                        Title = item.Element("title").Value,
                        DatePublished = DateTime.Parse(item.Element("pubDate").Value),
                        Url = item.Element("link").Value,
                        Description = item.Element("description").Value
                    };
        foreach (var item in items)
            currentFeed.Items.Add(item);

    }

    private void PhoneApplicationPage_BackKeyPress(object sender, System.ComponentModel.CancelEventArgs e)
    {
        // Cancel default navigation

        e.Cancel = true;
        PhoneApplicationFrame root = (PhoneApplicationFrame)Application.Current.RootVisual;
        root.GoBack();



    }


    private void PhoneApplicationPage_Loaded(object sender, RoutedEventArgs e)
    {
        this.DataContext = _selectedFeed;
    }



    private void lstFeeds_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
        _selectedFeedItem = (sender as ListBox).SelectedItem;
        NavigationService.Navigate(new Uri("/FeedItemDetailsPage.xaml", UriKind.Relative));
        FrameworkElement root = Application.Current.RootVisual as FrameworkElement;
        root.DataContext = _selectedFeedItem;
    }


}

the exception is thrown on line 抛出异常

wc.DownloadStringAsync(new Uri(currentFeed.FeedUrl));

I have resolved this. 我已经解决了。 The error was in my MainPage script. 错误出在我的MainPage脚本中。 I have moved the datacontext setting into the main script and now the error is no longer thrown 我已经将datacontext设置移到了主脚本中,现在不再抛出错误

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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