删除页面 Windows 手机 [英] Remove Pages windows phone

查看:22
本文介绍了删除页面 Windows 手机的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个大项目,其中我的应用程序一直保留我导航离开的页面.该页面只使用最少,并且有很多图形,因此我希望将其从内存中完全删除.

I have a big project where my application keeps retaining a page which I navigated away from. The page is only used minimal, and have a lot of graphics, I therefore want it to be completely removed from memory.

因此我使用了以下内容

 NavigationService.RemoveBackEntry();

使用我看到的分析器,上面的代码片段确保我只有 1 个页面实例.但由于它包含大量图形,我仍然希望将其从内存中完全删除,即分析器中没有实例.

Using the profiler I saw that, the above snippet made sure that I would only have 1 instance of the page. But as it is heavy with graphics I still want it to be completely removed from memory, i.e. no instances in the profiler.

在我的大应用程序中,我尝试取消订阅所有事件,引入 dispose/finalize 和调用 GC,它帮助了一些但实例仍然存在.

In my big application I tried to unsubscribe to all events, introduce dispose/finalize and calling GC, it helped some but the instance still existed.

为了排除任何愚蠢的错误,我制作了这个小样本.仅使用内存弹出检查器在两个哑页面之间导航.但是仍然存在 1-2 个页面实例.无论如何要强制删除页面,以便在内存中不存储任何内容?

To exclude any stupid errors, I have made this small sample. Only Navigating between two dumb pages with a memory popup checker. But still 1-2 instances of the pages still exists. Is there anyway to force the removal of pages such that nothing of it is stored in memory?

我已添加:

            while (App.RootFrame.RemoveBackEntry() != null) ;

到 OnNavigated to,它会删除除我开始的第一页之外的所有页面.我使用了调试分析工具包,可以看到无论我开始的第一个页面是什么,当我离开它时都不会被删除.

To the OnNavigated to, and it removes all the pages except the first page I start on. I've used the debug analysis toolkit, and can see that no matter what the first page I start on does not get removed, when I navigate away from it.

推荐答案

WP Silverlight 运行时将在内存中保留最多三页,即使从后台堆栈中删除也是如此.我仍然不清楚这种行为的原因,但我找到了一个(丑陋的)解决方法:http://blogs.codes-sources.com/kookiz/archive/2013/11/11/wpdev-give-that-memory-back.aspx

The WP Silverlight runtime will keep up to three pages in memory, even after being removed from the backstack. The reason for this behavior is still unclear to me, but I've found a (ugly) workaround: http://blogs.codes-sources.com/kookiz/archive/2013/11/11/wpdev-give-that-memory-back.aspx

基本上,覆盖页面的 OnNavigatedTo 处理程序,并强制垃圾回收三次,通过调用调度程序分隔:

Basically, override the OnNavigatedTo handler of your page, and force a garbage collection three times, separated by calls to the dispatcher:

protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
{
    base.OnNavigatedTo(e);

    this.Dispatcher.BeginInvoke(() =>
    {
        GC.Collect();
        GC.WaitForPendingFinalizers();

        this.Dispatcher.BeginInvoke(() =>
        {
            GC.Collect();
            GC.WaitForPendingFinalizers();

            this.Dispatcher.BeginInvoke(() =>
            {
                GC.Collect();
                GC.WaitForPendingFinalizers();
            });
        });
    });
}

听起来很疯狂,但确实有效.

As crazy as it sounds, it works.

就您而言,您还有另一个问题.您通过弹出窗口使页面保持活动状态.让我解释一下:

In your case, you have another problem. You are keeping the page alive with your popup. Let me explain:

CreatePopups 方法中,您创建弹出窗口并将其添加到起始页面的网格中.在弹出窗口中,您启动一​​个计时器以定期调用 UpdateMemoryInfo.计时器由 .NET 运行时保持活动状态,直到它停止.由于您使用实例方法作为事件处理程序,因此计时器会在您的弹出窗口中保留一个引用.您的弹出窗口通过 Parent 属性保持对网格的引用.网格通过它自己的 Parent 属性保持对页面的引用.所以你只是让你的页面不朽,只要你的计时器在滴答作响.为了证明问题存在,只需将 UpdateMemoryInfo 方法设为静态(并删除里面的所有 UI 更新代码).由于事件处理程序现在是静态的,计时器不会持有对弹出窗口实例的引用.运行分析器,您将看到页面的实例现在按预期被垃圾收集器回收.

In the CreatePopups method, you create the popup and add it to the grid of the starting page. In the popup, you start a timer to call UpdateMemoryInfo at regular interval. The timer is kept alive by the .NET runtime until it's stopped. The timer keeps a reference on your popup because you're using an instance method as event handler. Your popup is keeping a reference to the grid through the Parent property. The grid is keeping a reference to the page through its own Parent property. So you just made your page immortal, for as long as your timer is ticking. To prove that the issue is there, just make the UpdateMemoryInfo method static (and remove all the UI updating code there's inside). Since the event handler is now static, the timer won't hold a reference to instance of popup. Run the profiler, and you'll see that the instance of the page is now reclaimed by the garbage collector as you expect.

当然,它假设您的页面已从后堆栈中删除.通过按后退键或调用 NavigationService.GoBack() 方法,或使用 NavigationService.RemoveBackEntry() 手动删除它们(以防您只使用前向导航)

Of course, it supposes that your pages have been removed from the back stack. Either by pressing the back key or calling the NavigationService.GoBack() method, or by manually removing them using NavigationService.RemoveBackEntry() (in case you only use forward navigation)

这篇关于删除页面 Windows 手机的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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