Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/323.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
UWP C#清理辅助页面_C#_Uwp - Fatal编程技术网

UWP C#清理辅助页面

UWP C#清理辅助页面,c#,uwp,C#,Uwp,我正在打开第二个UWP页面,它本质上是在主页上单击的项目的详细信息。但是,在关闭辅助页时,不会返回内存,我也看不到可用于尝试dispose或GC的关闭事件。根据详细程度的不同,每次打开一个辅助页面可能需要高达15MB的内存。如果我打开/关闭20页,我已经浪费了250MB,似乎无法回收 打开新页面的代码为: CoreApplicationView newView = CoreApplication.CreateNewView(); int newViewId = 0

我正在打开第二个UWP页面,它本质上是在主页上单击的项目的详细信息。但是,在关闭辅助页时,不会返回内存,我也看不到可用于尝试dispose或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);

我主页的框架中没有任何内容。backback。是否有任何方法可以使用“newViewId”查找帧并删除或销毁它以回收内存

Windows 10SDK在其多视图示例中给出了答案:

需要进行一些编辑,以使名称空间中的所有内容正常工作

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
类文件并更新到您的命名空间。您只需要实现
类应用程序
,它本质上向
类应用程序
添加了一些函数和对象。本质上,它创建了一个
调度程序
和一组
辅助视图
,供以后使用

3) 按原样实现
ViewLifetimeControls.cs
,该控件将控制次视图(其命名空间为
secondaryViewShellers
),并实现跟踪次视图和在关闭时销毁对象所需的事件和功能

4) 使用
使用第二个ViewShelks添加到将启动次视图的页面。要启动次视图,请使用以下代码:

        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) 在secondary页面上,您需要引用,
使用secondary ViewShelpers并声明以下对象/变量:

    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();
        }

现在,当关闭辅助窗口时,我可以看到正在返回。我唯一担心的是,这会接管参数,否则我会将数据发送到新创建的视图。我必须找到一个变通方法将信息传递到第二页-可能暂时使用标题。

您不需要创建新框架。只需在已经实例化的框架对象上调用navigate。查看“我不想离开初始视图”上的示例,这就是为什么我没有运行
Frame.navigate()
。我想要第二个弹出窗口,除了没有被回收的内存外,它工作得很好。那你为什么不使用它呢?请同时提交正在关闭/返回初始视图的代码。我曾考虑过一个弹出窗口,但它有一些缺点1)它覆盖了当前屏幕,我无法将其移动到侧面或其他显示器。2) 我可能想从ListView上的两个或多个项目中获取信息,这样我就可以接收多个独立的窗口。3) 我的主窗口将不断收到实时信息,更新我们需要查看的ListView。我没有关于关闭辅助视图/窗口的任何代码。我只需点击右上角的“X”按钮。这是问题的一部分,在辅助页面上没有关闭事件。您是否遵循了?