【问题标题】:Xamarin Cannot access a disposed objectXamarin 无法访问已处置的对象
【发布时间】:2020-08-21 14:25:05
【问题描述】:

我有一个主详细信息页面,在我的菜单上,当我单击它导航到其他页面时,出现此错误:

System.ObjectDisposedException: '无法访问已释放的对象。 对象名称:'Android.Renderscripts.RenderScript'。'

但是,它只发生在我调用此代码时:

    protected override async void OnAppearing()
    {
        await LoadContent();
    }

如果我不调用 LoadContent 函数,菜单可以正常工作,我可以导航到其他页面。

我的加载内容代码:

    public async Task<firstContentJson> LoadContent()
    {
        string connectionUrl = "https://myurl.com/app/getcontent.php";
        object userInfos = new { };
        var jsonObj = JsonConvert.SerializeObject(userInfos);
        using (HttpClient client = new HttpClient())
        {
            StringContent content = new StringContent(jsonObj.ToString(), Encoding.UTF8, "application/json");
            var request = new HttpRequestMessage()
            {
                RequestUri = new Uri(connectionUrl),
                Method = HttpMethod.Post,
                Content = content
            };
            var response = await client.SendAsync(request);
            string dataResult = response.Content.ReadAsStringAsync().Result;
            firstContentJson result = JsonConvert.DeserializeObject<firstContentJson>(dataResult);
            try
            {

                string contentid = JObject.Parse(dataResult)["content_id"].ToString();
                string rep = JObject.Parse(dataResult)["rep"].ToString();
                string creator = JObject.Parse(dataResult)["creator"].ToString();
                string text = JObject.Parse(dataResult)["text"].ToString();
                string img = JObject.Parse(dataResult)["img"].ToString();
                string totalrate = JObject.Parse(dataResult)["totalrate"].ToString();
                //string vid = JObject.Parse(dataResult)["vid"].ToString();
                string date = JObject.Parse(dataResult)["date"].ToString();
                string name = JObject.Parse(dataResult)["name"].ToString();
                string pic = JObject.Parse(dataResult)["pic"].ToString();
                string visits = JObject.Parse(dataResult)["visits"].ToString();
                string lvl = JObject.Parse(dataResult)["lvl"].ToString();
                

                //inicio load first content
                imgTop.Source = "https://myurl.com/" + img;
                avUser.Source = "https://myurl.com/" + pic;
                txtName.Text = "@"+name;
                txtDesc.Text = text;
                imgContent.Source = "https://myurl.com/" + img;
                txtRate.Text = rep;
                if(Convert.ToInt32(totalrate)>0)
                {
                    txtInfos.Text = "Avaliado por " + totalrate + " pessoas";
                }
                else
                {
                    txtInfos.Text = "Seja o primeiro a avaliar esse conteúdo!";
                }
                indload.IsVisible = false;
                indload.IsRunning = false;
            }
            catch
            {
                indload.IsVisible = false;
                indload.IsRunning = false;
            }

            return result;
        }
    }

有人可以帮帮我吗?

编辑: Picture of the error when i click on the menu

【问题讨论】:

  • 尝试调试您的代码并查看对象被放置的位置。
  • 我刚刚添加了一张我点击菜单时得到的图片。它发生在我运行应用程序时,我可以毫无错误地编译应用程序
  • 可能是因为您试图访问在(使用 Http ...)块中定义的一些属性?我会尝试在 response.Content.ReadAsStringAsync().Result; 之后将代码移出 using 块或为了测试,不要处理 HttpClient 例如不要使用 using 块,只需执行 httpClient = new HttpClient()
  • 嗨@ihorbond,我试过了。我删除了“使用 http ...”并保留了您所说的内容,但我仍然遇到同样的错误

标签: c# visual-studio xamarin xamarin.forms xamarin.android


【解决方案1】:

解决方案:问题出在名为 Sharpnado.Shadows 的插件上。 我从我的母版页中删除了它,一切都恢复正常了。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-10-23
    • 2011-03-25
    • 1970-01-01
    相关资源
    最近更新 更多