UWP C#清除第二页

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

打开新页面的代码是:

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

没有什么在我的主网页的frame.BackStack。有没有办法使用“newViewId”来查找框架并删除或销毁它以回收内存?

回答:

在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增加了一些功能和对象,实质上它创建了一个dispatcher和一个SecondaryViews的集合供以后使用。

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

}

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

以上是 UWP C#清除第二页 的全部内容, 来源链接: utcz.com/qa/266173.html

回到顶部