C# 指定的强制转换无效。尝试使用Xamarin将ObservableCollection显示到另一个页面

C# 指定的强制转换无效。尝试使用Xamarin将ObservableCollection显示到另一个页面,c#,xamarin,C#,Xamarin,我正在尝试将字符串从当前页面上的传输到另一页,并在反复按下按钮时保存所有字符串 我在第一页上的xaml如下所示: <ImageButton Source="blackheart.png" Grid.Row="1" Grid.Column="3" WidthRequest="40"

我正在尝试将字符串从当前页面上的
传输到另一页,并在反复按下按钮时保存所有字符串

我在第一页上的xaml如下所示:

<ImageButton Source="blackheart.png"
                    Grid.Row="1" 
                    Grid.Column="3"
                    WidthRequest="40"
                    HeightRequest="40"
                    Opacity="0.7"
                    Clicked="OnGetFavorites"/>
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;
    }
<StackLayout Padding="0,30,0,0">

            <ListView x:Name="listView"
                      ItemsSource="{Binding CityData}"/>
</StackLayout>
 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);
    }
}
在显示xaml的第二页上,如下所示:

<ImageButton Source="blackheart.png"
                    Grid.Row="1" 
                    Grid.Column="3"
                    WidthRequest="40"
                    HeightRequest="40"
                    Opacity="0.7"
                    Clicked="OnGetFavorites"/>
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;
    }
<StackLayout Padding="0,30,0,0">

            <ListView x:Name="listView"
                      ItemsSource="{Binding CityData}"/>
</StackLayout>
 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);
    }
}
在这一行:

UIApplication.Main(args, null, "AppDelegate");

在此步骤中,调试器传递:
var page=(收藏夹)tabbedPage.Children[2]但在此步骤,调试器会出错:
page.AddCity(\u cityEntry.Text)


是否有办法修复此错误?

您需要查看堆栈跟踪或在调试器中逐步执行代码,以缩小错误的确切原因错误来自以下行:page.AddCity(\u cityEntry.Text);在此步骤中,调试器将传递:var page=(收藏夹)tabbedPage.Children[2];但在此步骤中,调试器会出错:page.AddCity(_cityEntry.Text);好啊该行上没有显式强制转换。
\u cityEntry.Text
是否包含有效字符串?
页面
是否为非空且类型为
收藏夹
?您需要继续挖掘以确定哪里出了问题我创建了一个字符串类型的变量:string city=\u cityEntry.Text;调试器显示其中有一个字符串。。