I have this simple xaml page to collect user information:
<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="T开发者_StackOverflow社区op" 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>
My code behind is like this:
//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));
}
Now coming to the problem. When the user navigates for the first time, the default values of 1986 and 10044 are shown. Now I edit the values, say 1999 and 65222 and select Save. What happens now is that the birthyear is showing 65222 and postcode is showing blank. What is the problem?
Update: When I try to retrieve the values in the Intermediate Window in VS, the new values are displayed properly. I am guessing it is saving properly but is having problems while retrieving/displaying it
You could try changeing the value instead, and do
settings["birthyear"] = birthyear.Text;
settings["postcode"] = postcode.Text;
And maybe also try casting your settings as type IsolatedStorageSettings.
EDIT:
settings.Remove("birthyear");
settings.Add("birthyear", birthyear.Text);
settings.Remove("postcode");
settings.Add("postcode", postcode.Text);
settings.Save();
Becomes:
settings["birthyear"] = birthyear.Text;
settings["postcode"] = postcode.Text;
精彩评论