Question

I'm working on a WPF application that contains a wrapper UI that is actually the MainWindow.xaml and the content in it (ContentPresenter) gets changed each time the user chooses to move to a different section of the application.

When the user returns to the main content, I want the application not to initialize it each time, but to preserve it in the memory somehow and restore it when the user clicks on the "Home" button.

In its current state, the Home view gets initialized over and over again when moving back to the "Home" section, which causes the application to be kinda slow. My question is this: Is there a way to preserve that user control in the memory somehow so that I would be able to restore it fast?

Thanks!

Was it helpful?

Solution 2

Eventually I decided to move the ContentControl into an ItemsControl and then just hide all content controls except for the active one.

Here's a snippet of my code:

<ItemsControl ItemsSource="{Binding ViewModels}">
        <ItemsControl.ItemsPanel>
            <ItemsPanelTemplate>
                <Grid />
            </ItemsPanelTemplate>
        </ItemsControl.ItemsPanel>
        <ItemsControl.ItemTemplate>
            <DataTemplate>
                <ContentControl Visibility="{Binding Visibility}" Content="{Binding ViewModel}" />
            </DataTemplate>
        </ItemsControl.ItemTemplate>
    </ItemsControl>

In my ViewModel I have the actual content and another Visibility property that is set to false when the content is not the current one and true if it is.

Thanks to all helpers!

OTHER TIPS

Hiding is an option ofcourse as @Matt suggested. An other way is to explorer the wonderfull world of frameworks (Prism, Caliburn, Caliburn.Micro, ... )

Those deliver a great assistance in managing WPF applications.

In essence you need to keep a reference to the ViewModel, a collection in the mainviewmodel or mainview that keeps track of the loaded viewmodels.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top