简体   繁体   中英

Problem with page navigation C# WPF with obfuscation (Memory)

I'm making an app which includes pages with sizes around 200 mb each due to parsing a lot of information and UI thing.

I'm trying to reuse created page to not use a lot of memory.

For some reason garbage collector not releasing memory.

I could not find a way to navigate to a new page without creating new instance of page.

I've got a singleton NavigationManager.

internal class NavigationManager
    {
        private static NavigationManager _instance;
        public static NavigationManager Instance
        {
            get
            {
                if (_instance == null)
                {
                    _instance = new NavigationManager();
                }
                return _instance;
            }
        }

        private NavigationManager()
        {
            
        }

        private List<Page> pages = new();
        private Page temporaryPage;
        
        public void NavigateTo(string pageName, MainWindow mainWindow)
        {          
            if (pages.Any(p => p.Name == pageName))
            {
                mainWindow.MainFrame.NavigationService.Navigate(pages.First(p => p.Name == pageName));
            }
            else
            {
                temporaryPage = (Page)Activator.CreateInstance(Type.GetType($"MyApp.MVVM.View.{pageName}"));
                pages.Add(temporaryPage);
                mainWindow.MainFrame.NavigationService.Navigate(temporaryPage);
            }
        }
    }

And navigating just using:

NavigationManager.Instance.NavigateTo(@"MainPages.MainPage", this);

But here after obfuscation I can't use Type.GetType with strings because it won't just find it.Without obfuscation it works fine.

Is there a way to clear, delete, remove a page?

Or maybe use Activator.CreateInstance(Type.GetType()) more properly with obfuscation.

Thanks.

Assuming you need to keep the page type names obfuscated, as you note you can't use Type.GetType with a string. Instead you can do something even easier - just pass in the Type . Your NavigateTo method would look something like this:

    public void NavigateTo(Type pageType, MainWindow mainWindow)
    {          
        var existing = pages.FirstOrDefault(p=>p.GetType() == pageType);
        if (existing != null)
        {
            mainWindow.MainFrame.NavigationService.Navigate(existing);
        }
        else
        {
            temporaryPage = (Page)Activator.CreateInstance(pageType);
            pages.Add(temporaryPage);
            mainWindow.MainFrame.NavigationService.Navigate(temporaryPage);
        }
    }

To call it you'd say

 NavigationManager.Instance.NavigateTo(typeof(MainPages.MainPage), this);

By using typeof and passing around Type objects you're safe from obfuscation because your code no longer cares about what things are named. It's better practice even without obfuscation to be honest.

Unfortunately I don't see a way around some refactoring here but this should require the least amount of it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM