gpt4 book ai didi

c# - 为 RSS 加载 XDocument 时出现 MonoDroid ObjectDisposedException

转载 作者:太空宇宙 更新时间:2023-11-03 14:00:33 63 4
gpt4 key购买 nike

我正在摆弄 MonoDroid(又名 Android 版 Mono),试图构建一个简单的播客下载应用程序,目前只是测试 RSS 提要内容的检索。

我的简单应用是:

  • 输入 RSS 提要 url 的编辑文本
  • 一个“下载”RSS 的按钮
  • 下载操作输出的文本显示

当用户点击下载时,我获取 RSS 内容,当前使用 XDocument (我也试过 just WebRequest / Stream s etc. ),这是简短的版本:

void button_Click(object sender, EventArgs e)
{
output.Text += "user input: " + rssUriInput.Text + "\n";

try
{
output.Text += "### document ###";
output.Text += XDocument.Load(rssUriInput.Text).ToString();
output.Text += "### document ###";
}
catch (Exception ex)
{
output.Text += "OOOPS something went wrong:\n" + ex.ToString();
}
}

我尝试了几种方法,但一直收到 ObjectDisposedException当我做的时候 XDocument.Load(/*my uri here*/);

我的 AssemblyInfo.cs 包括

[assembly: UsesPermission(Android.Manifest.Permission.Internet)]

现在,免责声明,在让 hello world 运行之后我有点自己离开了,所以我可能缺少一些 MonoDroid 基础知识,但是这段代码在普通的 C# 应用程序中工作(我测试过)但是
为什么我在这里做错了?我是否必须做一些特殊的事情才能在 MonoDroid 中使用 xml/linq/网络访问?

这里是完整的事件代码:

using System;

using Android.App;
using Android.Content;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using Android.OS;
using System.Xml.Linq;

namespace RssDownlowd
{
[Activity(Label = "RssDownlowd", MainLauncher = true, Icon = "@drawable/icon")]
public class Activity1 : Activity
{
EditText rssUriInput;
Button button;
TextView output;

protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);

SetContentView(Resource.Layout.RssDownload);

rssUriInput = FindViewById<EditText>(Resource.Id.rssUriText);
button = FindViewById<Button>(Resource.Id.downloadButton);
output = FindViewById<TextView>(Resource.Id.outputTextView);

button.Click += new EventHandler(button_Click);
}

void button_Click(object sender, EventArgs e)
{
output.Text += "user input: " + rssUriInput.Text + "\n";

try
{
output.Text += "### document ###";
output.Text += XDocument.Load(rssUriInput.Text).ToString();
output.Text += "### document ###";
}
catch (Exception ex)
{
output.Text += "OOOPS something went wrong:\n" + ex.ToString();
}
}

}
}

这里是完整的异常(exception):

"System.ObjectDisposedException: The object was used after being disposed.

at System.Net.WebConnection.BeginRead (System.Net.HttpWebRequest request, System.Byte[] buffer, Int32 offset, Int32 size, System.AsyncCallback cb, System.Object state) [0x00000] in <filename unknown>:0

at System.Net.WebConnectionStream.BeginRead (System.Byte[] buffer, Int32 offset, Int32 size, System.AsyncCallback cb, System.Object state) [0x00000] in <filename unknown>:0

at System.Net.WebConnectionStream.Read (System.Byte[] buffer, Int32 offset, Int32 size) [0x00000] in <filename unknown>:0

at System.Xml.XmlInputStream.Read (System.Byte[] buffer, Int32 offset, Int32 count) [0x00000] in <filename unknown>:0

at System.Xml.NonBlockingStreamReader.ReadBuffer () [0x00000] in <filename unknown>:0

at System.Xml.NonBlockingStreamReader.Read (System.Char[] dest_buffer, Int32 index, Int32 count) [0x00000] in <filename unknown>:0

at System.Xml.XmlStreamReader.Read (System.Char[] dest_buffer, Int32 index, Int32 count) [0x00000] in <filename unknown>:0

at Mono.Xml2.XmlTextReader.ReadTextReader (Int32 remained) [0x00000] in <filename unknown>:0

at Mono.Xml2.XmlTextReader.PeekChar () [0x00000] in <filename unknown>:0

at Mono.Xml2.XmlTextReader.ReadChar () [0x00000] in <filename unknown>:0

at Mono.Xml2.XmlTextReader.Expect (Int32 expected) [0x00000] in <filename unknown>:0

at Mono.Xml2.XmlTextReader.ReadEntityReference (Boolean ignoreEntityReferences) [0x00000] in <filename unknown>:0

at Mono.Xml2.XmlTextReader.ReadReference (Boolean ignoreEntityReferences) [0x00000] in <filename unknown>:0

at Mono.Xml2.XmlTextReader.ReadText (Boolean notWhitespace) [0x00000] in <filename unknown>:0

at Mono.Xml2.XmlTextReader.ReadContent () [0x00000] in <filename unknown>:0

at Mono.Xml2.XmlTextReader.Read () [0x00000] in <filename unknown>:0

at System.Xml.XmlTextReader.Read () [0x00000] in <filename unknown>:0

at Mono.Xml.XmlFilterReader.Read () [0x00000] in <filename unknown>:0

at System.Xml.Linq.XElement.LoadCore (System.Xml.XmlReader r, LoadOptions options) [0x00000] in <filename unknown>:0

at System.Xml.Linq.XNode.ReadFrom (System.Xml.XmlReader r, LoadOptions options) [0x00000] in <filename unknown>:0

at System.Xml.Linq.XContainer.ReadContentFrom (System.Xml.XmlReader reader, LoadOptions options) [0x00000] in <filename unknown>:0

at System.Xml.Linq.XElement.LoadCore (System.Xml.XmlReader r, LoadOptions options) [0x00000] in <filename unknown>:0

at System.Xml.Linq.XNode.ReadFrom (System.Xml.XmlReader r, LoadOptions options) [0x00000] in <filename unknown>:0

at System.Xml.Linq.XContainer.ReadContentFrom (System.Xml.XmlReader reader, LoadOptions options) [0x00000] in <filename unknown>:0

at System.Xml.Linq.XElement.LoadCore (System.Xml.XmlReader r, LoadOptions options) [0x00000] in <filename unknown>:0

at System.Xml.Linq.XNode.ReadFrom (System.Xml.XmlReader r, LoadOptions options) [0x00000] in <filename unknown>:0

at System.Xml.Linq.XContainer.ReadContentFrom (System.Xml.XmlReader reader, LoadOptions options) [0x00000] in <filename unknown>:0

at System.Xml.Linq.XElement.LoadCore (System.Xml.XmlReader r, LoadOptions options) [0x00000] in <filename unknown>:0

at System.Xml.Linq.XNode.ReadFrom (System.Xml.XmlReader r, LoadOptions options) [0x00000] in <filename unknown>:0

at System.Xml.Linq.XContainer.ReadContentFrom (System.Xml.XmlReader reader, LoadOptions options) [0x00000] in <filename unknown>:0

at System.Xml.Linq.XDocument.ReadContent (System.Xml.XmlReader reader, LoadOptions options) [0x00000] in <filename unknown>:0

at System.Xml.Linq.XDocument.LoadCore (System.Xml.XmlReader reader, LoadOptions options) [0x00000] in <filename unknown>:0

at System.Xml.Linq.XDocument.Load (System.String uri, LoadOptions options) [0x00000] in <filename unknown>:0

at System.Xml.Linq.XDocument.Load (System.String uri) [0x00000] in <filename unknown>:0

at RssDownlowd.Activity1.button_Click (System.Object sender, System.EventArgs e) [0x0004a] in C:\\Users\\khd483\\code\\HelloMonoDroid\\RssDownlowd\\Activity1.cs:47 "

最佳答案

奇怪的是,当我尝试你的代码时它工作得很好:

Screen shot

您是否在 Mono for Android 选项的项目中启用了链接?

还可以考虑对 RSS 进行异步下载,因为它会阻塞 UI 线程直到完成,从而使应用无响应。

关于c# - 为 RSS 加载 XDocument 时出现 MonoDroid ObjectDisposedException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10830651/

63 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com