Issue
I have <Entry/>
field with default set Text in my WeatherPage.xaml
<Entry x:Name="_cityEntry"
Grid.Row="1"
Grid.Column="1"
Margin="5,0"
VerticalOptions="Center"
BackgroundColor="Black"
TextColor="White"
Opacity="0.7"
Text="Moscow" />
I get the string from the <Entry/>
and set this string to another page.
My WeatherPage.xaml.cs
code:
void OnGetFavorites(System.Object sender, System.EventArgs e)
{
Preferences.Set("cityName", _cityEntry.Text);
var tabbedPage = this.Parent.Parent as TabbedPage;
tabbedPage.CurrentPage = tabbedPage.Children[2];
}
When I first load the application when I press the button, it redirects me to the appropriate page and I see the string I set for it by default.
When I write something else in the <Entry/>
field and pressed the button again, the logic redirects me back to the corresponding page, but the string does not change.
Is there an option when the string is changed in <Entry/>
field to be changed in the respective page as well ?
The page on which the string is printed looks like this:
public partial class Favorites : ContentPage
{
public Favorites()
{
InitializeComponent();
NavigationPage.SetHasNavigationBar(this, false);
FavoriteCities();
}
private void FavoriteCities()
{
string cityName = Preferences.Get("cityName", "default_value");
FavoriteCity.Text = cityName.ToString();
}
}
I attach a screenshot in which I press the button and "Moscow" is printed (as it is set by default) and then I change the string to "San Francisco" and press button again, but the string is not printed ?
Solution
you are not doing anything to refresh the text. You are just calling it in the constructor and never updating it
public Favorites()
{
InitializeComponent();
NavigationPage.SetHasNavigationBar(this, false);
FavoriteCities();
}
if you want to call it every time the page appears, try using OnAppearing
public void override OnAppearing()
{
FavoriteCities();
}
Answered By - Jason
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.