This project is read-only.

Problem with user controls

Nov 3, 2011 at 2:52 PM
Edited Nov 3, 2011 at 2:53 PM

I have a problem with a pivot page that includes several copies of a user control in its xaml (one on each element of the pivot). On return to the page TombstoneHelper terminates the App with an exception as follows: $exception {"Value does not fall within the expected range."} System.Exception {System.ArgumentException}.

If I remove the SaveState/RestoreStare from the page being navigated to, everything functuions properly.

Can you take a look please?

Nov 14, 2011 at 9:54 AM
Edited Nov 14, 2011 at 9:55 AM

@pgmariotti can you share the XAML for the page?
What are you expecting to restore? Just the selected Pivot or anything else as well?
Are you specifying any type(s) when you call `SaveState`?

p.s. sorry for slow response. The notification of this only just got to me. :(

Nov 14, 2011 at 1:11 PM

This is the XAML for the control:

<UserControl x:Class="Stanzetta.AddressControl"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:toolkit="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone.Controls.Toolkit"
    mc:Ignorable="d"
    FontFamily="{StaticResource PhoneFontFamilyNormal}"
    FontSize="{StaticResource PhoneFontSizeNormal}"
    Foreground="{StaticResource PhoneForegroundBrush}"
    d:DesignHeight="648" d:DesignWidth="477" Loaded="UserControl_Loaded">
 
    <Grid x:Name="LayoutRoot" Height="645">
        <StackPanel VerticalAlignment="Top" Height="244" Name="stpLogin" Visibility="Collapsed">
            <TextBlock Text="{Binding Source={StaticResource ResKey},Path=Labels.String122, Mode=OneTime}"
                       TextAlignment="Center" FontSize="{StaticResource PhoneFontSizeMediumLarge}" FontWeight="Bold" />
            <TextBox Text="{Binding Email, Mode=TwoWay}" InputScope="EmailUserName" 
                     Name="tbEmail" FontSize="22" Height="Auto" Tag="5" GotFocus="tb_GotFocus" TextChanged="tbEmail_TextChanged"></TextBox>
            <TextBlock Text="{Binding Source={StaticResource ResKey},Path=Labels.String124, Mode=OneTime}" 
                       FontSize="{StaticResource PhoneFontSizeMediumLarge}" 
                       TextAlignment="Center" FontWeight="Bold" Margin="0,-10,0,0" />
            <PasswordBox Password="{Binding Password, Mode=TwoWay}" Name="pwPass" FontSize="22" Tag="6" GotFocus="tb_GotFocus" PasswordChanged="pwPass_PasswordChanged" />
        </StackPanel>
        <StackPanel Orientation="Vertical" Margin="0,0,0,1" VerticalAlignment="Bottom" Height="394">
            <TextBlock Text="{Binding Source={StaticResource ResKey},Path=Labels.String125, Mode=OneTime}" 
                       TextAlignment="Center" 
                       Height="40" FontSize="24" FontWeight="SemiBold" 
                       Margin="0,10,0,0"/>
            <CheckBox Content="{Binding Source={StaticResource ResKey},Path=Labels.String54, Mode=OneTime}" 
                          Height="72" HorizontalAlignment="Left" 
                          Margin="60,-5,0,0" Name="ckbWebAddress" VerticalAlignment="Top" 
                          Click="ckbWebAddress_Click" Width="370" FontSize="24" Foreground="{StaticResource PhoneForegroundBrush}" IsChecked="True" />
            <CheckBox Content="{Binding Source={StaticResource ResKey},Path=Labels.String53, Mode=OneTime}"
                          Height="70" Margin="60,-10,0,0" Name="ckbIPAddress" IsChecked="False" 
                          Click="ckbIPAddress_Click" FontSize="24" Foreground="{StaticResource PhoneForegroundBrush}" />
            <StackPanel Orientation="Horizontal" VerticalAlignment="Top"
                            Margin="30,0,30,0" Height="74" Name="stpIP" 
                            Visibility="Collapsed">
                <TextBox Height="73" HorizontalAlignment="Center" 
                         Margin="0,0,0,0" Name="tbAddress1" 
                         Text="" VerticalAlignment="Bottom" Width="90" 
                         BorderBrush="{StaticResource PhoneBorderBrush}" 
                         InputScope="CurrencyAmount"
                         HorizontalContentAlignment="Left" 
                         TextChanged="tbAddress_TextChanged" 
                         BorderThickness="3" 
                         FontSize="20"  GotFocus="tb_GotFocus" Tag="1" />
                <TextBox Height="73" HorizontalAlignment="Center" 
                         HorizontalContentAlignment="Left" 
                         Margin="15,0,0,0" Name="tbAddress2" 
                         InputScope="CurrencyAmount"
                         Text="" VerticalAlignment="Bottom" Width="90" 
                         TextChanged="tbAddress_TextChanged"  
                         BorderBrush="{StaticResource PhoneBorderBrush}" BorderThickness="3" 
                         FontSize="20" GotFocus="tb_GotFocus" Tag="2" />
                <TextBox Height="73" HorizontalAlignment="Center" 
                         Margin="15,0,0,0" Name="tbAddress3" Text="" 
                         VerticalAlignment="Bottom" Width="90" 
                         BorderBrush="{StaticResource PhoneBorderBrush}" 
                         InputScope="CurrencyAmount"
                         HorizontalContentAlignment="Left" 
                         TextChanged="tbAddress_TextChanged" 
                         BorderThickness="3" 
                         FontSize="20" SelectionForeground="Black" GotFocus="tb_GotFocus" Tag="3" />
                <TextBox Foreground="{StaticResource PhoneTextBoxForegroundBrush}"
                         Height="73" HorizontalAlignment="Center" 
                         HorizontalContentAlignment="Left" 
                         Margin="15,0,0,0" Name="tbAddress4" Text="" 
                         InputScope="CurrencyAmount"
                         VerticalAlignment="Bottom" Width="90"                      
                         TextChanged="tbAddress_TextChanged" 
                         BorderBrush="{StaticResource PhoneBorderBrush}" 
                         BorderThickness="3" FontSize="20" GotFocus="tb_GotFocus" Tag="4" />
            </StackPanel>
            <StackPanel Orientation="Horizontal">
                <TextBox Height="72" HorizontalAlignment="Left" Width="240" Name="tbApi" 
                         Text="http://api.dropbox.com/" VerticalAlignment="Top" Margin="10,0,0,0" 
                         FontSize="20" Visibility="Collapsed" IsReadOnly="True" />
                <TextBox Height="72" HorizontalAlignment="Center"
                     Text="" VerticalAlignment="Bottom" Width="425"
                     Margin="30,0,11,0" Name="tbWebAddress" GotFocus="tb_GotFocus" 
                     TextChanged="tbWebAddress_TextChanged" Tag="0"
                     HorizontalContentAlignment="Left"  
                     BorderThickness="3"
                     FontSize="20" Visibility="Visible" />
            </StackPanel>
            <TextBox Height="72" 
                     HorizontalContentAlignment="Left" Name="tbPort" 
                     Text="8080" 
                     InputScope="CurrencyAmount" Width="151" 
                     BorderBrush="{StaticResource PhoneBorderBrush}" BorderThickness="3" 
                     FontSize="20"  
                     GotFocus="tb_GotFocus" Tag="5" HorizontalAlignment="Left" Margin="303,0,0,0" TextChanged="tbPort_TextChanged" />
            <TextBlock Height="40" Name="tbkPort" 
                           Text="{Binding Source={StaticResource ResKey},Path=Labels.String55, Mode=OneTime}" 
                           Width="53" 
                           Foreground="{StaticResource PhoneForegroundBrush}" FontSize="22" 
                           HorizontalAlignment="Left" VerticalAlignment="Top" Margin="260,-55,0,0" IsHitTestVisible="False" />
            <CheckBox Content="  Needs login" Height="77" HorizontalAlignment="Left" Margin="32,-78,0,0" Name="ckbNeedsLogin" VerticalAlignment="Top" Width="218" Click="ckbNeedsLogin_Click" Visibility="Collapsed" IsChecked="True" />
            <Button Content="{Binding Source={StaticResource ResKey},Path=Labels.String56, Mode=OneTime}"
                        Height="71" HorizontalAlignment="Right" Name="btnValidate" VerticalAlignment="Top" 
                        Width="300" BorderBrush="Green" Background="White" FontSize="20" Foreground="Green" 
                        IsEnabled="False" Visibility="Visible" Margin="5,-5,23,0" Click="btnValidate_Click" />
            <Button Margin="20,-78,0,0" Width="100" HorizontalAlignment="Left" BorderBrush="Transparent" BorderThickness="0" Click="Button_Click">
                <Image Source="/Stanzetta;component/Resources/help.png" />
            </Button>            
        </StackPanel>
        <toolkit:PerformanceProgressBar Background="Transparent" 
                                        BorderBrush="White" 
                                        BorderThickness="2" 
                                        Height="32" 
                                        HorizontalAlignment="Center" 
                                        VerticalAlignment="Center" 
                                        Width="403" 
                                        Visibility="Visible" 
                                        Name="ppbValidate" Margin="42,212,42,401" />
    </Grid>
</UserControl>
and this is the XAML for the page where the control is used:
<phone:PhoneApplicationPage 
    x:Class="Stanzetta.SetupSourcesPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone"
    xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone"
    xmlns:controls="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone.Controls"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
    mc:Ignorable="d" d:DesignWidth="480" d:DesignHeight="768"
    FontFamily="{StaticResource PhoneFontFamilyNormal}"
    FontSize="{StaticResource PhoneFontSizeNormal}"
    Foreground="{StaticResource PhoneForegroundBrush}"
    SupportedOrientations="Portrait"  Orientation="Portrait"
    shell:SystemTray.IsVisible="True" xmlns:my="clr-namespace:Stanzetta" Loaded="PhoneApplicationPage_Loaded">
 
    <!--LayoutRoot is the root grid where all page content is placed-->
    <Grid x:Name="LayoutRoot" Background="Transparent">
        <!--Pivot Control-->
        <TextBlock Text="{Binding Source={StaticResource ResKey},Path=Labels.String137, Mode=OneTime}" 
                   FontSize="36" 
                    FontWeight="Bold" 
                    FontFamily="Segoe WP"
                    TextAlignment="Center" />
        <controls:Pivot Margin="0,50,0,0" Name="MainPivot" >
            <!--Pivot item one-->
            <controls:PivotItem >
                <controls:PivotItem.Header>
                    <StackPanel Orientation="Horizontal">
                        <Image Source="/Stanzetta;component/Resources/calibre.png" Height="48" Width="48"/>
                        <TextBlock Text="{Binding Source={StaticResource ResKey},Path=Labels.String138, Mode=OneTime}" 
                                   FontStyle="Italic" FontSize="30" VerticalAlignment="Center" Margin="10,0,0,0"/>
                    </StackPanel>
                </controls:PivotItem.Header>
                <Grid Height="640" VerticalAlignment="Top">
                    <my:AddressControl HorizontalAlignment="Left" x:Name="acCalibre" VerticalAlignment="Top" Width="485" Height="632" Margin="0,-6,0,0" />
                </Grid>
            </controls:PivotItem>
 
            <!--Pivot item two-->
            <controls:PivotItem>
                <controls:PivotItem.Header>
                    <StackPanel Orientation="Horizontal">
                        <Image Source="/Stanzetta;component/Resources/dropbox.png" Height="48" Width="48"/>
                        <TextBlock Text="{Binding Source={StaticResource ResKey},Path=Labels.String139, Mode=OneTime}" 
                                   FontStyle="Italic" FontSize="30" VerticalAlignment="Center" Margin="10,0,0,0"/>
                    </StackPanel>
                </controls:PivotItem.Header>
                <Grid Height="640" VerticalAlignment="Top">
                    <my:AddressControl HorizontalAlignment="Left" x:Name="acDropbox" VerticalAlignment="Top" Width="483" Height="632" />
                </Grid>
            </controls:PivotItem>
            <controls:PivotItem VerticalAlignment="Top">
                <controls:PivotItem.Header>
                    <StackPanel Orientation="Horizontal">
                        <Image Source="/Stanzetta;component/Resources/calibredropbox.png" Height="48" Width="120"/>
                        <TextBlock Text="{Binding Source={StaticResource ResKey},Path=Labels.String140, Mode=OneTime}" 
                                   FontStyle="Italic" FontSize="30" VerticalAlignment="Center" Margin="10,0,0,0"/>
                    </StackPanel>
                </controls:PivotItem.Header>
                <Grid Height="640" VerticalAlignment="Top">
                    <my:AddressControl HorizontalAlignment="Left" x:Name="acCalibreDropbox" VerticalAlignment="Top" Width="483" Height="628" />
                </Grid>
            </controls:PivotItem>
            <controls:PivotItem VerticalAlignment="Top">
                <controls:PivotItem.Header>
                    <StackPanel Orientation="Horizontal">
                        <Image Source="/Stanzetta;component/Resources/skydrive.png" Height="48" Width="48"/>
                        <TextBlock Text="{Binding Source={StaticResource ResKey},Path=Labels.String141, Mode=OneTime}" 
                                   FontStyle="Italic" FontSize="30" VerticalAlignment="Center" Margin="10,0,0,0"/>
                    </StackPanel>
                </controls:PivotItem.Header>
                <Grid Height="640" VerticalAlignment="Top">
                    <my:AddressControl HorizontalAlignment="Left" x:Name="acSkydrive" VerticalAlignment="Top" Width="483" Height="629" />
                </Grid>
            </controls:PivotItem>
            <controls:PivotItem VerticalAlignment="Top">
                <controls:PivotItem.Header>
                    <StackPanel Orientation="Horizontal">
                        <Image Source="/Stanzetta;component/Resources/cloud.png" Height="48" Width="48"/>
                        <TextBlock Text="{Binding Source={StaticResource ResKey},Path=Labels.String142, Mode=OneTime}" 
                                   FontStyle="Italic" FontSize="30" VerticalAlignment="Center" Margin="10,0,0,0" />
                    </StackPanel>
                </controls:PivotItem.Header>
                <Grid Height="640" VerticalAlignment="Top">
                    <my:AddressControl HorizontalAlignment="Left" x:Name="acCloud" VerticalAlignment="Top" Width="483" Height="633" />
                </Grid>
            </controls:PivotItem>
        </controls:Pivot>
    </Grid>
 
    <!--Sample code showing usage of ApplicationBar-->
    <!--<phone:PhoneApplicationPage.ApplicationBar>
        <shell:ApplicationBar IsVisible="True" IsMenuEnabled="True">
            <shell:ApplicationBarIconButton x:Name="appbar_button1" IconUri="/Images/appbar_button1.png" Text="Button 1"/>
            <shell:ApplicationBarIconButton x:Name="appbar_button2" IconUri="/Images/appbar_button2.png" Text="Button 2"/>
            <shell:ApplicationBar.MenuItems>
                <shell:ApplicationBarMenuItem x:Name="menuItem1" Text="MenuItem 1"/>
                <shell:ApplicationBarMenuItem x:Name="menuItem2" Text="MenuItem 2"/>
            </shell:ApplicationBar.MenuItems>
        </shell:ApplicationBar>
    </phone:PhoneApplicationPage.ApplicationBar>-->
    
</phone:PhoneApplicationPage>

In the code behind for the page I had tried to save/restore as follows:
        protected override void OnNavigatedFrom(System.Windows.Navigation.NavigationEventArgs e)
        {
            base.OnNavigatedFrom(e);
            //this.SaveState();
            PhoneApplicationService.Current.State["Stanzetta.AddLibraryFocus"] = ifocus;
            PhoneApplicationService.Current.State["Stanzetta.CurrentPivot"] = MainPivot.SelectedIndex;
        }
 
        protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
        {
            base.OnNavigatedTo(e);
            //this.RestoreState();
            if (!PhoneApplicationService.Current.State.ContainsKey("Stanzetta.AddLibraryFocus"))
                PhoneApplicationService.Current.State.Add("Stanzetta.AddLibraryFocus", ifocus.ToString());
            if (!PhoneApplicationService.Current.State.ContainsKey("Stanzetta.CurrentPivot"))
                PhoneApplicationService.Current.State.Add("Stanzetta.CurrentPivot", iIndex.ToString());
            string s = PhoneApplicationService.Current.State["Stanzetta.AddLibraryFocus"] as string;
            ifocus = Convert.ToInt32(s);
        }

I hope this is useful to you...
Paul Mariotti