我在第一次webrequest上总是得到nullreference异常

时间:2012-02-23 08:22:18

标签: windows-phone-7.1 agfx

部署到模拟器并第一次运行时,我的应用程序总是在nullreference异常时崩溃。在后续运行中,它工作得很好。用户互动不是很好。

我的应用尝试从http://www.choomba.org/mxuutiset.xml下载xml文件,只是模拟未来的网络服务。

我的问题是,为什么我会得到空引用异常?我想我遵循了在线教程的信,但它仍然不起作用。气象服务样本工作得很好。

我做错了什么?

我将整个代码放在skydrive上,看看:link

这是我的模特课:

using System;
using AgFx;
using System.Collections.ObjectModel;
using MxSuomi.Models;
using System.Collections.Generic;
using System.Xml.Linq;
using System.Linq;
using System.Globalization;

namespace AgFxTest
{
[CachePolicy(CachePolicy.NoCache)]
public class UutisetServiceModel : ModelItemBase<UutinenLoadContext>
{

    public UutisetServiceModel() { }

    public UutisetServiceModel(int id)
        : base(new UutinenLoadContext(id))
    {

    }

    private ObservableCollection<Uutinen> _uutiset = new ObservableCollection<Uutinen>();

    public ObservableCollection<Uutinen> Uutiset
    {
        get { return _uutiset; }
        set
        {

            if (value == null) throw new ArgumentNullException();

            if (_uutiset != null)
            {
                _uutiset.Clear();

                foreach (var item in value)
                {
                    _uutiset.Add(item);
                }
            }
            RaisePropertyChanged("Uutiset");

        }
    }

    public class UutisetServiceModelLoader : IDataLoader<UutinenLoadContext>
    {
        private const string feedUri = "http://www.choomba.org/mxuutiset.xml?rnd={0}";

        public LoadRequest GetLoadRequest(UutinenLoadContext loadContext, Type objectType)
        {
            string uri = string.Format(feedUri, loadContext.Id);
            return new WebLoadRequest(loadContext, new Uri(uri));
        }

        public object Deserialize(UutinenLoadContext loadContext, Type objectType, System.IO.Stream stream)
        {
            XElement uutisetXml = XElement.Load(stream);

            var temp = (from uutinen in uutisetXml.Descendants("uutinen")
                        select new Uutinen()
                        {
                            Id = int.Parse(uutinen.Element("id").Value),
                            Otsikko = uutinen.Element("otsikko").Value,
                            Pvm = DateTime.Parse(uutinen.Element("pvm").Value, new CultureInfo("fi-FI")),
                            Linkki = uutinen.Element("linkki").Value,
                            Kuva = uutinen.Element("kuva").Value

                        }).ToList();


            temp = temp.OrderByDescending(p => p.Pvm).ToList();

            var model = new UutisetServiceModel(loadContext.Id);

            model.Uutiset = new ObservableCollection<Uutinen>();
            foreach (var item in temp)
            {
                model.Uutiset.Add(item);
            }


            return model;
        }

    }
}
}

这是我的主页代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using AgFx;
using System.Threading;
using System.Diagnostics;

namespace AgFxTest {
public partial class MainPage : PhoneApplicationPage
{
    // Constructor
    public MainPage()
    {
        InitializeComponent();
    }

    private void btnLoadData_Click(object sender, RoutedEventArgs e)
    {
        this.DataContext = DataManager.Current.Load<UutisetServiceModel>(int.Parse(tbUutinenId.Text));

    }
}
}

这是错误的调试输出:

No cache found for AgFxTest.UutisetServiceModel (ID=
2/23/2012 10:59:44 AM: Queuing load for UutisetServiceModel (ID=0)
A first chance exception of type 'System.NullReferenceException' occurred in System.Windows.dll
A first chance exception of type 'System.NullReferenceException' occurred in System.Windows.dll
A first chance exception of type 'System.NullReferenceException' occurred in System.Windows.dll
A first chance exception of type 'System.Threading.ThreadAbortException' occurred in mscorlib.dll
The thread 'Storage Thread' (0xe050182) has exited with code 0 (0x0).
A first chance exception of type 'System.Threading.ThreadAbortException' occurred in mscorlib.dll
The thread 'General Worker' (0xee403f2) has exited with code 0 (0x0).
The thread '<No Name>' (0x1b71ae2) has exited with code 0 (0x0).
The thread '<No Name>' (0xe39016e) has exited with code 0 (0x0).
The thread '<No Name>' (0xfd003a6) has exited with code 0 (0x0).
The thread '<No Name>' (0xeab03ea) has exited with code 0 (0x0).
The program '[248251222] UI Task: Managed' has exited with code 0 (0x0).

0 个答案:

没有答案