0

I’m trying to transfer string from

<Entry/>
on current to another page and save all strings when a button is pressed repeatedly.

My xaml on first page look like this:

<ImageButton Source="blackheart.png"
                    Grid.Row="1" 
                    Grid.Column="3"
                    WidthRequest="40"
                    HeightRequest="40"
                    Opacity="0.7"
                    Clicked="OnGetFavorites"/>

My .cs file on first page look like this:

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

On my second page on which I will display the xaml look like this:

<StackLayout Padding="0,30,0,0">

            <ListView x:Name="listView"
                      ItemsSource="{Binding CityData}"/>
</StackLayout>

My .cs file on second page look like this:

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

When I click the button on the first page to add the strings I get an error from Main.cs file:

Specified cast is not valid.

on this line:

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

Is there a way to fix this error ?

Anonymous Asked question May 14, 2021