RSSリーダーから以下のコードを使用して、以前のMainPage.xamlに戻ろうとすると、null参照例外が発生します。誰かがこのエラーを回避するために私が何をする必要があるかをアドバイスすることによって私を助けてくれますか?
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;
}
}
例外はオンラインでスローされます
wc.DownloadStringAsync(new Uri(currentFeed.FeedUrl));