繁体   English   中英

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

[英]WP8 app crashes and I can't fix this code

我已缩小了有关为什么我的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}

我相信在这部分中:

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

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

怎么办

这取决于您要解决的问题。 您是否因为f.Id包含尾随空格而占用了子字符串? 不需要,因为Convert.ToInt32将处理尾随和前导空格。 如果您想获取f.Id的第一部分中f.Id的数字,直到第一个空格,因为第一个空格之后是非数字,则可以使用

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

这样可以处理没有空间的情况。

暂无
暂无

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

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