UWP C#清理辅助页面

时间:2017-12-27 15:57:03

标签: c# uwp

我打开了一个辅助UWP页面,它基本上是在主页面上点击的项目的详细信息。但是,关闭辅助页面时,不会返回内存,也不会看到我可以使用的关闭事件以尝试处理或GC。每次打开辅助页面最多可能需要15MB,具体取决于详细程度。如果我打开/关闭20页,我已经浪费了250MB而我似乎无法收回。

打开新页面的代码是:

        CoreApplicationView newView = CoreApplication.CreateNewView();
        int newViewId = 0;
        await newView.Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
        {

            Frame frame = new Frame();
            frame.Navigate(typeof(Disk), null);                
            Window.Current.Content = frame;
            Window.Current.Activate();
            newViewId = ApplicationView.GetForCurrentView().Id;
        });
        bool viewShown = await ApplicationViewSwitcher.TryShowAsStandaloneAsync(newViewId);

我的主页框架中没有任何内容.BackStack。有没有办法使用" newViewId"找到框架并删除或销毁它以回收内存?

1 个答案:

答案 0 :(得分:1)

Windows 10SDK的多视图示例中有答案:https://github.com/Microsoft/Windows-universal-samples/tree/master/Samples/MultipleViews

必须进行一些编辑才能使所有内容在命名空间等内正常工作。

1)添加到App.cs

    partial void Construct();

    // Hook into OnLaunched here.
    partial void OverrideOnLaunched(LaunchActivatedEventArgs args, ref bool handled);

    // Hook into InitializeMainPage here.
    partial void InitializeRootFrame(Frame frame);

2)从SDK实现SampleConfigurations.cs类文件并更新到命名空间。您只需要实现class App,它实际上会向class App添加一些函数和对象,基本上它会创建dispatcherSecondaryViews的集合,以便稍后使用。

3)实现ViewLifetimeControls.cs,它将控制您的辅助视图(其命名空间为SecondaryViewsHelpers,并实现跟踪辅助视图和关闭时销毁对象所需的事件和函数。

4)使用using SecondaryViewsHelpers;添加到将启动辅助视图的页面。要启动辅助视图,请使用以下代码:

        ViewLifetimeControl viewControl = null;
        await CoreApplication.CreateNewView().Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
        {
            // This object is used to keep track of the views and important
            // details about the contents of those views across threads
            // In your app, you would probably want to track information
            // like the open document or page inside that window
            viewControl = ViewLifetimeControl.CreateForCurrentView();
            viewControl.Title = "";
            // Increment the ref count because we just created the view and we have a reference to it                
            viewControl.StartViewInUse();

            var frame = new Frame();
            frame.Navigate(typeof(SecondaryPage), viewControl);
            Window.Current.Content = frame;
            // This is a change from 8.1: In order for the view to be displayed later it needs to be activated.
            Window.Current.Activate();
            ApplicationView.GetForCurrentView().Title = viewControl.Title;
            ((App)App.Current).SecondaryViews.Add(viewControl);
        });
        bool viewShown = await ApplicationViewSwitcher.TryShowAsStandaloneAsync(viewControl.Id, ViewSizePreference.Default, ApplicationView.GetForCurrentView().Id, ViewSizePreference.Default);

5)在辅助页面上,您需要引用using SecondaryViewsHelpers;并声明以下对象/变量:

    ViewLifetimeControl thisViewControl;
    int mainViewId;
    CoreDispatcher mainDispatcher;

将以下代码添加到辅助页面以分配上述对象并注册事件

        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            thisViewControl = (ViewLifetimeControl)e.Parameter;
            mainViewId = ((App)App.Current).MainViewId;
            mainDispatcher = ((App)App.Current).MainDispatcher;
            // When this view is finally released, clean up state
            thisViewControl.Released += ViewLifetimeControl_Released;
        }

        private async void ViewLifetimeControl_Released(Object sender, EventArgs e)
        {
            ((ViewLifetimeControl)sender).Released -= ViewLifetimeControl_Released;
            // The ViewLifetimeControl object is bound to UI elements on the main thread
            // So, the object must be removed from that thread
            await mainDispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
            {
                ((App)App.Current).SecondaryViews.Remove(thisViewControl);
            });

            // The released event is fired on the thread of the window
            // it pertains to.
            //
            // It's important to make sure no work is scheduled on this thread
            // after it starts to close (no data binding changes, no changes to
            // XAML, creating new objects in destructors, etc.) since
            // that will throw exceptions
            Window.Current.Close();
        }

现在当关闭辅助窗口时,我可以看到被给予回复。我唯一担心的是,这将接管我将以其他方式将数据发送到新创建的视图的参数。我必须找到一种解决方法,将信息传递到辅助页面 - 可能暂时使用标题。