从帧缓存中删除特定页面

时间:2015-11-18 09:04:56

标签: c# xaml uwp

我使用以下代码缓存Page

this.NavigationCacheMode = Windows.UI.Xaml.Navigation.NavigationCacheMode.Enabled;

但是当我从页面导航回来时,我想从缓存中删除特定页面

我使用以下代码从缓存中删除Page,但这会导致所有页面都从缓存中删除。

var frame = Window.Current.Content as Frame;
if (frame != null)
{
    var cacheSize = ((frame)).CacheSize;
    ((frame)).CacheSize = 0;
    ((frame)).CacheSize = cacheSize;
}

如何从Frame缓存中仅删除一个特定页面?

1 个答案:

答案 0 :(得分:0)

通常一个应用包含一个Window,而此window包含一个rootFrame,在此rootFrame中我们可以放置几个页面。当您创建UWP应用时,在OnLaunched方法中,您可以看到此rootFrame。

var frame = Window.Current.Content as Frame;

此代码表示获取此应用的rootFrame,因此我认为您已使用此代码从缓存中删除所有网页。

我已经创建了一个UWP应用,并在splitview中放置了MainPage,在此控件的Pane中,我放了一个Button来浏览此页面到Page1,在此控件的Content中,我放了一个Frame,在这个框架中,我放了一个Button来将此框架导航到Page2:

<Page
    x:Class="Cache.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:Cache"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d">
    <Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
        <VisualStateManager.VisualStateGroups>
            <VisualStateGroup>

                <VisualState>
                    <VisualState.StateTriggers>
                        <AdaptiveTrigger MinWindowWidth="450" />
                    </VisualState.StateTriggers>
                    <VisualState.Setters>
                        <Setter Target="RootSpiltView.DisplayMode" Value="CompactInline" />
                        <Setter Target="RootSpiltView.IsPaneOpen" Value="true" />
                    </VisualState.Setters>
                </VisualState>
                <VisualState>
                    <VisualState.StateTriggers>
                        <AdaptiveTrigger MinWindowWidth="0" />
                    </VisualState.StateTriggers>
                    <VisualState.Setters>
                        <Setter Target="RootSpiltView.DisplayMode" Value="CompactInline" />
                    </VisualState.Setters>
                </VisualState>
            </VisualStateGroup>
        </VisualStateManager.VisualStateGroups>
        <SplitView x:Name="RootSpiltView" OpenPaneLength="200" CompactPaneLength="50" DisplayMode="CompactOverlay" >

            <SplitView.Pane>
                <Grid>
                    <Grid.RowDefinitions>
                        <RowDefinition Height="Auto"/>
                        <RowDefinition Height="*"/>
                    </Grid.RowDefinitions>
                    <StackPanel Grid.Row="0" Height="auto">
                        <Button BorderThickness="0" Background="Transparent" Click="Button_Click_Pane">
                            <Button.Content>
                                <TextBlock Text="&#xE700;" FontFamily="Segoe MDL2 Assets" FontSize="24" />
                            </Button.Content>
                        </Button>
                    </StackPanel>
                    <TextBlock Text="MainPage_Splitview_Pane" Grid.Row="1" VerticalAlignment="Center" HorizontalAlignment="Center" />
                    <Button Grid.Row="1" HorizontalAlignment="Center" Content="MainPageToPage1" Margin="0,80,0,0" Click="MainPageToPage1"/>
                </Grid>
            </SplitView.Pane>
            <SplitView.Content>
                <Frame x:Name="frame" Background="Transparent" Navigating="OnFrameNavigating" Navigated="OnFrameNavigated">
                    <Button HorizontalAlignment="Center" VerticalAlignment="Center" Content="FrameToPage2" Click="FrameToPage2"/>
                </Frame>
            </SplitView.Content>
        </SplitView>
    </Grid>
</Page>  

的MainPage:

namespace Cache
{
    /// <summary>
    /// An empty page that can be used on its own or navigated to within a Frame.
    /// </summary>
    public sealed partial class MainPage : Page
    {
        public MainPage()
        {
            this.InitializeComponent();
        }

        private void Button_Click_Pane(object sender, RoutedEventArgs e)
        {
            this.RootSpiltView.IsPaneOpen = !this.RootSpiltView.IsPaneOpen; 
        }

        private void OnFrameNavigating(object sender, NavigatingCancelEventArgs e)
        {
            string msg = "Frame's Navigating event happening, SourcePageType = {0}";
            System.Diagnostics.Debug.WriteLine(msg, e.SourcePageType);
        }

        private void OnFrameNavigated(object sender, NavigationEventArgs e)
        {
            string msg = "Frame's Navigated event happening, SourcePageType = {0}";
            System.Diagnostics.Debug.WriteLine(msg, e.SourcePageType);
        }

        private void FrameToPage2(object sender, RoutedEventArgs e)
        {
            frame.Navigate(typeof(Page2));
        }

        private void MainPageToPage1(object sender, RoutedEventArgs e)
        {
            this.Frame.Navigate(typeof(Page1));
        }
    }
}

当我在第1页时,单击按钮导航到MainPage并从缓存中移动根页中的页面。当我在第2页时,单击按钮导航到框架中的page1,并从缓存中移动该框架的页面:

第1页:

namespace Cache
{
    /// <summary>
    /// An empty page that can be used on its own or navigated to within a Frame.
    /// </summary>
    public sealed partial class Page1 : Page
    {
        public Page1()
        {
            this.InitializeComponent();
            this.NavigationCacheMode = Windows.UI.Xaml.Navigation.NavigationCacheMode.Enabled;
        }

        private void NaviToMainPage(object sender, RoutedEventArgs e)
        {
            this.Frame.Navigate(typeof(MainPage));
            var rootFrame = Window.Current.Content as Frame;
            if (rootFrame != null)
            {
                var cacheSize = ((rootFrame)).CacheSize;
                ((rootFrame)).CacheSize = 0;
                ((rootFrame)).CacheSize = cacheSize;
            }
        }
    }
}

2页:

namespace Cache
{
    /// <summary>
    /// An empty page that can be used on its own or navigated to within a Frame.
    /// </summary>
    public sealed partial class Page2 : Page
    {
        public Page2()
        {
            this.InitializeComponent();
            this.NavigationCacheMode = Windows.UI.Xaml.Navigation.NavigationCacheMode.Enabled;
        }

        private void NaviToPage1(object sender, RoutedEventArgs e)
        {
            var frame = this.Frame;
            if (frame != null)
            {
                var cacheSize = ((frame)).CacheSize;
                ((frame)).CacheSize = 0;
                ((frame)).CacheSize = cacheSize;
            }
            this.Frame.Navigate(typeof(Page1));          
        }
    }
}

你可以尝试一下。