2017-03-15 49 views
0

有了這個代碼,我創造新的觀點:C#UWP新視圖內存泄露

CoreApplicationView newView = CoreApplication.CreateNewView(); 
int newViewId = 0; 
await newView.Dispatcher.RunAsync(CoreDispatcherPriority.Normal,() => 
    { 
     Frame frame = new Frame(); 
     frame.Navigate(typeof(SecondaryPage), null); 
     Window.Current.Content = frame; 
     // You have to activate the window in order to show it later. 
     Window.Current.Activate(); 

     newViewId = ApplicationView.GetForCurrentView().Id; 
    }); 
bool viewShown = await ApplicationViewSwitcher.TryShowAsStandaloneAsync(newViewId); 

當我關閉新窗口,運行代碼再次,我再次關閉新的,我又打開另一個窗口在Windows內存使用率越來越大......所以,我認爲所有的窗戶都沒有關閉,只是實際上隱藏着......

所以,有2種方式我想:

  1. 嘗試實際「殺死」關閉的窗口
  2. 嘗試重新顯示隱藏窗口並使其內容不同...

我只是希望這是隻有2個窗口,主這個新創建的... 如何做到這一點的任何建議?

編輯:

這是什麼樣子時,我用這個例子https://github.com/Microsoft/Windows-universal-samples/tree/master/Samples/MultipleViews

這些都是3個截圖與此應用程序。

這是當應用程序被啓動的內存消耗:

這是後我加4次:

這是當我打開和關閉他們每個人:

因此,即使在它們的示例中,內存使用量也在上升...

回答

1

當您關閉窗口時t保存在最近使用過的窗口列表中,以備再次使用。如果您知道您不再需要它(或想要/需要回收內存),則可以在該視圖上調用ApplicationView.Consolidated事件後調用Window.Current.Close();來關閉它。

這裏的描述在this article on the "buildingapps" blog
還有一個(很複雜的IMO)example in the UWP samples repository

+0

我編輯了我的問題... – user3239349

+0

不管我做什麼,每次關閉並打開視圖時,內存使用量都在上升...... – user3239349