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

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

问题描述

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

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.

在我的大型应用程序中,我试图取消订阅所有事件,介绍了处置/最终化和调用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 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运行时仍将在内存中保留三页.这种行为的原因对我来说仍然不清楚,但是我发现了一个(丑陋的)解决方法:

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更新代码).由于事件处理程序现在是静态的,因此计时器将不会保存对popup实例的引用.运行事件探查器,您将看到垃圾回收器现在已按预期回收了页面实例.

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 Phone的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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