实现ISupportIncrementalLoading时出现System.AccessViolationException [英] System.AccessViolationException while implementing ISupportIncrementalLoading

查看:69
本文介绍了实现ISupportIncrementalLoading时出现System.AccessViolationException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我目前遇到的问题是抛出了AccessViolationException,我发现它很难解决,因为它是从非托管代码抛出的。  我目前要做的是实现ISupportIncremental加载界面
,以便我可以使用数据虚拟化。  我已将代码片段附加到相关部分。

I am currently experiencing a problem where an AccessViolationException is thrown and I'm finding it very difficult to solve since it's thrown from unmanaged code.  What I'm currently trying to do is implement the ISupportIncremental loading interface so I can use data virtualization.  I have attached code snippets to the relating pieces.

public class FeedList<T> : ObservableCollection<T>, ISupportIncrementalLoading
    {
        public Feed parent;

        public bool HasMoreItems
        {
            get { return parent.pagination.next_url != null; }
        }

        public IAsyncOperation<LoadMoreItemsResult> LoadMoreItemsAsync(uint count)
        {
            return new FeedLoader(this.parent);
        }
    }

public class FeedLoader : IAsyncOperation<LoadMoreItemsResult>
    {
        private AsyncStatus _asyncStatus = AsyncStatus.Started;
        private LoadMoreItemsResult _results;

        public FeedLoader(Feed feed)
        {
            this.LoadItems(feed);
        }

        private async void LoadItems(Feed feed)
        {
            Feed newFeed = await FeedCollection.GetFeedAsync("", "", feed.pagination.next_url);
            foreach (Item item in newFeed.data)
                feed.data.Add(item);
            feed.pagination = newFeed.pagination;

            _results.Count = (uint)feed.data.Count;
            _asyncStatus = AsyncStatus.Completed;
            if (Completed != null)
                 //This is where the exception is thrown
                 this.Completed(this, _asyncStatus);
        }

        public AsyncOperationCompletedHandler<LoadMoreItemsResult> Completed{get; set;}

        public LoadMoreItemsResult GetResults()
        { 
            return _results;
        }

        public void Cancel()
        {
            throw new NotImplementedException();
        }

        public void Close()
        {
        }

        public Exception ErrorCode
        {
            get { throw new NotImplementedException(); }
        }

        public uint Id
        {
            get { throw new NotImplementedException(); }
        }

        public AsyncStatus Status
        {
            get { return _asyncStatus; }
        }
    }

现在,我非常怀疑原因是因为删除原因实际上是以下功能这个调用(以及添加一些空对象)修复了这个问题。

Now, I am highly suspect the cause is actually in the following function since the removal of this call (and addition of adding some empty objects) fixes the problem.

await FeedCollection.GetFeedAsync("", "", feed.pagination.next_url);

以下是完整的函数:

static public async Task<PhotoFeed> GetFeedAsync(string title, string description, string url)
        {
            HttpClient client = new HttpClient { MaxResponseContentBufferSize = 1024 * 1024 };

            try
            {
                string content = await client.GetStringAsync(url);

                DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(Feed));

                MemoryStream stream = new MemoryStream(Encoding.Unicode.GetBytes(content));
                Feed feed = serializer.ReadObject(stream) as Feed;
                stream.Dispose();
                feed.data.parent = feed;
                feed.Title = title;
                feed.Description = description;
                feed.dataForHomeScreen = new ObservableCollection<Item>();
                for(int i = 0; i < feed.data.Count; i++)
                {
                    feed.data[i].feed = feed;
                }
                feed.GenerateTiles();
                client.Dispose();
                return feed;
            }
            catch (HttpRequestException e)
            {
                Debug.WriteLine(e.Message);
                return null;
            }
        }

任何帮助都会很精彩,我已经坚持了很长一段时间。  对不起代码转储,我只知道MSFT经常回答这些技术问题并要求提供一些代码。

Any help would be wonderful, I've been stuck on this for quite some time.  Sorry for the code dump, I just know MSFT often respond to these technical questions and ask for some code.

先谢谢了,

格兰特

推荐答案

检查出来。



- -------------------------------------------------- ----------------------------


--------------------------------------------------------------------------------

Alejandro Campos Magencio - 微软升级工程师 - 论坛版主

 如果我的回复回答了您的问题,请将此帖标记为已回答。

Alejandro Campos Magencio - Microsoft Escalation Engineer - Forum Moderator
 If my reply answers your question, please mark this post as answered.


这篇关于实现ISupportIncrementalLoading时出现System.AccessViolationException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆