WP8应用程序崩溃,我无法修复此代码

时间:2013-12-17 01:22:39

标签: c# windows-phone-8

我已经缩小了为什么我的WP8 rss阅读器应用程序突然崩溃(安装GDR3更新后)的问题。该应用程序在GDR3更新之前工作正常。这一切都与这段代码有关,我无法弄清楚如何修复它。

void get_News()
    {

        news_wc = new WebClient();
        news_wc.DownloadStringCompleted += (s, ea) =>
            {
                if (ea.Error == null && !ea.Cancelled)
                {

                    StringReader string_reader = new StringReader(ea.Result);
                    XmlReader xml_reader = XmlReader.Create(string_reader);
                    SyndicationFeed feed = SyndicationFeed.Load(xml_reader);
                    news_data = (from f in feed.Items
                                 select new News { ID = Convert.ToInt32(f.Id.Substring(0, f.Id.IndexOf(' '))), Title = f.Title.Text, Date = f.PublishDate.Date.ToLongDateString(), Article = Regex.Replace(Regex.Replace(f.Summary.Text,@"</p>","\n"), @"<[^>]*>", String.Empty).Substring(0,Regex.Replace(Regex.Replace(f.Summary.Text,@"</p>","\n"), @"<[^>]*>", String.Empty).LastIndexOf("Tags")), Thumb = get_Thumb(f.Summary.Text), Uri = f.Links.First().Uri.AbsoluteUri }).ToList();
                    MainLongListSelector.ItemsSource = news_data;
                    IsolatedStorageSettings settings = IsolatedStorageSettings.ApplicationSettings;
                    if (!settings.Contains("LatestID"))
                        settings.Add("LatestID", news_data.First().ID);
                    else
                        settings["LatestID"] = news_data.First().ID;
                    settings.Save();
                    ShellTile.ActiveTiles.First().Update(new FlipTileData {WideBackContent = news_data.First().Title });

                }
                else
                    MessageBox.Show(ea.Error.Message);
            };
        news_wc.DownloadStringAsync(new Uri("http://www.winbeta.org/metrofeed/rss.xml"));


    }

任何人都可以帮我解决这个问题吗?我正试图解决这个问题。这是我用VS得到的错误:

$exception  {System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> System.ArgumentOutOfRangeException: Length cannot be less than zero.
Parameter name: length
at System.String.InternalSubStringWithChecks(Int32 startIndex, Int32 length, Boolean     fAlwaysCopy)
at System.String.Substring(Int32 startIndex, Int32 length)
at WinBeta.MainPage.<get_News>b__4(SyndicationItem f)
at System.Linq.Enumerable.WhereSelectEnumerableIterator`2.MoveNext()
at System.Collections.Generic.List`1..ctor(IEnumerable`1 collection)
at System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source)
at WinBeta.MainPage.<get_News>b__3(Object s, DownloadStringCompletedEventArgs ea)
at System.Net.WebClient.OnDownloadStringCompleted(DownloadStringCompletedEventArgs e)
at System.Net.WebClient.DownloadStringOperationCompleted(Object arg)
--- End of inner exception stack trace ---
at System.RuntimeMethodHandle.InvokeMethod(Object target, Object[] arguments, Signature     sig, Boolean constructor)
at System.Reflection.RuntimeMethodInfo.UnsafeInvokeInternal(Object obj, Object[] parameters, Object[] arguments)
at System.Reflection.RuntimeMethodInfo.UnsafeInvoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
at System.Delegate.DynamicInvokeImpl(Object[] args)
at System.Windows.Threading.DispatcherOperation.Invoke()}   System.Exception {System.Reflection.TargetInvocationException}

1 个答案:

答案 0 :(得分:2)

我相信这是在这一部分:

f.Id.Substring(0, f.Id.IndexOf(' '))

f.Id可能不包含空格,因此IndexOf返回-1。您不能将-1作为子字符串操作的长度传递。

该怎么做

这取决于你要解决的问题是什么。您是否因为f.Id包含尾随空格而使用子字符串?应该没有必要,因为Convert.ToInt32将处理尾随和前导空格。如果你想将f.Id的第一部分中包含的数字放到第一个空格,因为在第一个空格之后有非数字,你可以使用

Convert.ToInt32(f.Id.Split(' ')[0]);

这应该处理没有空间的情况。