C# 为什么我从另一个页面调用的方法不';行不通
我在第一页中有一个方法,我想从另一页调用此方法:C# 为什么我从另一个页面调用的方法不';行不通,c#,xamarin,C#,Xamarin,我在第一页中有一个方法,我想从另一页调用此方法: public async Task FavoritesForecast() { string FavoriteName = Preferences.Get("FavoriteName", "default_value"); _cityEntry.Text = FavoriteName; } 在另一页中,我使用以下代码调用此方法: async void B
public async Task FavoritesForecast()
{
string FavoriteName = Preferences.Get("FavoriteName", "default_value");
_cityEntry.Text = FavoriteName;
}
在另一页中,我使用以下代码调用此方法:
async void BtnRead_Clicked(System.Object sender, System.EventArgs e)
{
if (!string.IsNullOrEmpty(txtPersonId.Text))
{
//Get Person
var person = await App.SQLiteDb.GetItemAsync(Convert.ToInt32(txtPersonId.Text));
if (person != null)
{
await DisplayAlert("Success", "City Name: " + person.Name, "OK");
Preferences.Set("FavoriteName", person.Name);
var forecast = new AboutPage();
_ = forecast.FavoritesForecast();
var tabbedPage = this.Parent.Parent as TabbedPage;
tabbedPage.CurrentPage = tabbedPage.Children[1];
}
}
else
{
await DisplayAlert("Required", "Please Enter ID in the field which is located under the name", "OK");
}
但是当我参考另一个页面时,搜索引擎中的名称不会改变?这是异步方法。Show将被称为wait forecast.FavoritesForecast();您正在创建一个新的AboutPage,使用该方法在那里设置一个字段,然后忘记itas@HansKesting指出,您正在创建一个
AboutPage
的新实例,而不是使用现有实例。