ApplicationSettings是否在模拟器上正常工作?

时间:2011-01-15 16:32:49

标签: c# silverlight windows-phone-7

我有这个简单的xaml页面来收集用户信息:

<Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
        <TextBlock Height="50" HorizontalAlignment="Left" Margin="6,36,0,0" Text="Enter your birth year :" VerticalAlignment="Top" Width="312" FontSize="32" />
        <TextBox FontSize="32"  Height="84" HorizontalAlignment="Left" Margin="310,19,0,0" Name="birthyear" Text="" VerticalAlignment="Top" Width="146" TextAlignment="Center" />
        <TextBlock Height="60" HorizontalAlignment="Left" Margin="12,112,0,0" Name="birthyear_message" Text="" VerticalAlignment="Top" Width="438" />            
        <TextBlock FontSize="32" Height="50" HorizontalAlignment="Left" Margin="6,283,0,0" Text="Enter your post code :" VerticalAlignment="Top" Width="312" />
        <TextBox FontSize="32" TextAlignment="Center" Height="82" HorizontalAlignment="Left" Margin="310,268,0,0" Name="postcode" Text="" VerticalAlignment="Top" Width="146" />
        <TextBlock Height="60" HorizontalAlignment="Left" Margin="12,356,0,0" Name="postcode_message" Text="" VerticalAlignment="Top" Width="438" />
        <Button Content="Cancel" Height="72" HorizontalAlignment="Left" Margin="31,441,0,0" Name="cancel" VerticalAlignment="Top" Width="160" Click="cancel_Click" />
        <Button Content="Save" Height="72" HorizontalAlignment="Left" Margin="247,441,0,0" Name="save" VerticalAlignment="Top" Width="160" Click="save_Click" />             
</Grid>

我的代码背后是这样的:

//Set and display user a set of default values if user is accessing page for first time else retrieve data from the settings
private void PhoneApplicationPage_Loaded(object sender, RoutedEventArgs e)
    {
        var settings = IsolatedStorageSettings.ApplicationSettings;

        if (settings.Contains("birthyear"))
            birthyear.Text = settings["birthyear"].ToString();
        else
        {                
            birthyear.Text = (DateTime.Now.Year - 25).ToString();
            settings.Add("birthyear", birthyear.Text);
        }

        if (settings.Contains("postcode"))
            birthyear.Text = settings["postcode"].ToString();
        else
        {
            postcode.Text = "10044";
            settings.Add("postcode", postcode.Text);
        }
    }

private void save_Click(object sender, RoutedEventArgs e)
{
    var settings = IsolatedStorageSettings.ApplicationSettings;

    //ignore error conditions for the time being!    
    settings.Remove("birthyear");
    settings.Add("birthyear", birthyear.Text);

    settings.Remove("postcode");
    settings.Add("postcode", postcode.Text);

    settings.Save();

    //Navigate to the same page to validate values entered by user is saved
    NavigationService.Navigate(new Uri("/MainPage.xaml", UriKind.Relative));
}

现在出现问题。当用户第一次导航时,会显示默认值1986和10044。现在我编辑值,比如1999和65222,然后选择保存。现在发生的事情是,出生年份显示65222,邮政编码显示空白。有什么问题?

更新: 当我尝试在VS中的中间窗口中检索值时,将正确显示新值。我猜它正在保存,但在检索/显示它时遇到了问题

1 个答案:

答案 0 :(得分:1)

您可以尝试更改值,然后执行

settings["birthyear"] = birthyear.Text;
settings["postcode"] = postcode.Text;

也许还可以尝试将您的设置转换为IsolatedStorage设置类型。编辑:

settings.Remove("birthyear");
settings.Add("birthyear", birthyear.Text);

settings.Remove("postcode");
settings.Add("postcode", postcode.Text);

settings.Save();

变为:

settings["birthyear"] = birthyear.Text;
settings["postcode"] = postcode.Text;