SuperJMN
SuperJMN

Reputation: 13972

Getting "Value does not fall withint the expected range" when calling to Frame.GoBack

I'm creating a Windows Universal Application that uses Navigation (via Frames).

I want to create a reusable Page to avoid creating multiple pages. The idea is to use the same Page over and over (the same type, not the same instance).

In order to get different content shown in each navigation, I just pass the content of the page inside the parameters of a Frame.Navigate call, like this:

Frame.Navigate(typeof(ReusablePage), myContent);

To illustrate my idea. I've created a very simple, minimal, reproducible example of my proof-of-concept UWP app. The problem is that it throws and I don't know why. This is the code:

(see the exact problem near the end of the post)

MainPage.xaml

<Grid>
    <Grid.RowDefinitions>
        <RowDefinition Height="Auto" />
        <RowDefinition></RowDefinition>
    </Grid.RowDefinitions>
    <StackPanel Orientation="Horizontal">
        <Button Content="Navigate" Tapped="Navigate" />
        <Button Content="Go back" Tapped="GoBack" IsEnabled="False" x:Name="BackButton" />
    </StackPanel>
    <Frame Grid.Row="1" x:Name="MyFrame" />
</Grid>

MainPage.xaml.cs

using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Input;

namespace App4
{
    public sealed partial class MainPage : Page
    {
        private int times = 0;

        public MainPage()
        {
            this.InitializeComponent();
        }

        private void GoBack(object sender, TappedRoutedEventArgs e)
        {
            if (MyFrame.CanGoBack)
            {
                MyFrame.GoBack();
            }
        }

        private void Navigate(object sender, TappedRoutedEventArgs e)
        {
            MyFrame.Navigate(typeof(ReusablePage), new Button() { Content = $"Navigated {++times} times"});
            BackButton.IsEnabled = MyFrame.CanGoBack;
        }
    }
}

ReusablePage.xaml

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

</Page>

ReusablePage.xaml.cs

using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Navigation;

namespace App4
{
    public sealed partial class ReusablePage : Page
    {
        public ReusablePage()
        {
            InitializeComponent();
        }

        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            Content = (UIElement) e.Parameter;
            base.OnNavigatedTo(e);
        }
    }
}

To reproduce the problem:

  1. Run the application
  2. click on the Navigate button until the Go back button becomes enabled
  3. Click on the "Go back" button (click twice, at least).
  4. You should get a System.ArgumentException

Value does not fall within the expected range

Upvotes: 0

Views: 110

Answers (1)

Anran Zhang
Anran Zhang

Reputation: 7727

You can try adding this code to the ReusablePage constructor.

public ReusablePage()
{
    InitializeComponent();
    NavigationCacheMode = NavigationCacheMode.Enabled;
}

At the same time, you can also use e.NavigationMode to determine the way to navigate to the page.

protected override void OnNavigatedTo(NavigationEventArgs e)
{
    if(e.NavigationMode == NavigationMode.Back)
    {
        // Do something.
    }
    base.OnNavigatedTo(e);
}

Your error occurred when rendering the visual tree. There is no more detailed explanation of the error, but the cache page can effectively solve this problem.

Best regards.

Upvotes: 1

Related Questions