繁体   English   中英

为什么我更改的字符串没有出现在使用选项卡式 forms xamarin 的其他页面上

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

我的WeatherPage.xaml中有默认设置 Text 的<Entry/>字段

<Entry x:Name="_cityEntry"
       Grid.Row="1" 
       Grid.Column="1"
       Margin="5,0"
       VerticalOptions="Center"
       BackgroundColor="Black"
       TextColor="White"
       Opacity="0.7"
       Text="Moscow" />

我从<Entry/>获取字符串并将该字符串设置到另一个页面。

我的WeatherPage.xaml.cs代码:

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];
    }

当我按下按钮第一次加载应用程序时,它会将我重定向到相应的页面,并且我会看到默认情况下为它设置的字符串。

当我在<Entry/>字段中写入其他内容并再次按下按钮时,逻辑将我重定向回相应的页面,但字符串没有改变。

当字符串在<Entry/>字段中更改以在相应页面中更改时是否有选项?

打印字符串的页面如下所示:

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

我附上了一个屏幕截图,其中我按下按钮并打印“莫斯科”(默认设置),然后我将字符串更改为“旧金山”并再次按下按钮,但没有打印字符串? 截屏

你没有做任何事情来刷新文本。 您只是在构造函数中调用它并且从不更新它

public Favorites()
{
    InitializeComponent();
    NavigationPage.SetHasNavigationBar(this, false);

    FavoriteCities();
}

如果您想在每次页面出现时调用它,请尝试使用OnAppearing

public void override OnAppearing()
{
    FavoriteCities();
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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