[英]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.