简体   繁体   中英

Why doesn't my changed string appear on the other page using tabbed forms xamarin

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? 截屏

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();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM