问题描述
我正在尝试将字符串从当前的 <Entry/>
传输到另一个页面,并在重复按下按钮时保存所有字符串。
我在第一页上的 xaml 如下所示:
<ImageButton Source="blackheart.png"
Grid.Row="1"
Grid.Column="3"
WidthRequest="40"
HeightRequest="40"
Opacity="0.7"
Clicked="OnGetFavorites"/>
我在第一页上的 .cs 文件如下所示:
void OnGetFavorites(System.Object sender,System.EventArgs e)
{
Preferences.Set("cityName",_cityEntry.Text);
var tabbedPage = this.Parent.Parent as TabbedPage;
var page = (Favorites)tabbedPage.Children[2];
page.AddCity(_cityEntry.Text);
tabbedPage.CurrentPage = page;
}
在我将显示 xaml 的第二页上,如下所示:
<StackLayout Padding="0,30,0">
<ListView x:Name="listView"
ItemsSource="{Binding CityData}"/>
</StackLayout>
我在第二页上的 .cs 文件如下所示:
public partial class Favorites : ContentPage
{
ObservableCollection<string> CityData { get; set; }
public Favorites()
{
InitializeComponent();
NavigationPage.SetHasNavigationBar(this,false);
CityData = new ObservableCollection<string>();
this.BindingContext = this;
}
protected override void OnAppearing()
{
base.OnAppearing();
}
public void AddCity(string city)
{
CityData.Add(city);
}
}
当我单击第一页上的按钮添加字符串时,我从 Main.cs 文件中收到错误消息:
Specified cast is not valid.
在这一行:
UIApplication.Main(args,null,"AppDelegate");
在这一步,调试器通过:var page = (Favorites)tabbedPage.Children[2];
但是在这一步调试器出错:page.AddCity(_cityEntry.Text);
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)